| At line 106 added 122 lines. |
| !!Create the formbean |
| Create a file {{< Your project folder >\src\web\org\appfuse\webapp\FileUploadForm.java}} and paste the code below into that file: |
| {{{ |
| package org.appfuse.webapp.action.fileupload; |
|
| import org.apache.struts.action.ActionForm; |
| import org.apache.struts.action.ActionForward; |
| import org.apache.struts.action.ActionMapping; |
| import org.appfuse.webapp.action.BaseAction; |
| import org.appfuse.webapp.action.fileupload.util.UploadUtil; |
| import org.appfuse.webapp.form.FileUploadForm; |
|
| import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| import java.io.File; |
| /** |
| * <p> |
| * Example action for reciving uploaded files and write them to disk |
| * <p/> |
| * Inspired by:<br /> |
| * "AJAX Upload progress monitor for Commons-FileUpload Example" http://www.telio.be/blog/2006/01/06/ajax-upload-progress-monitor-for-commons-fileupload-example/ |
| * "Struts & Fileupload not working" http://mail-archives.apache.org/mod_mbox/jakarta-commons-user/200602.mbox/%[email protected]%3E |
| * "AJAX Struts File Upload Progress Meter" http://kencochrane.blogspot.com/2006/03/ajax-struts-file-upload-progress-meter.html |
| * |
| * @author <a href="mailto:helge.tesgaard(at)gmail.com.com">Helge Tesgaard</a> |
| * @created 2006-04-24 01:03:53 |
| * @struts.action path="/saveUploadFile" scope="request" name="fileUploadForm" validate="true" |
| * @struts.action path="/editUploadFile" scope="request" name="fileUploadForm" validate="false" |
| * @struts.action-forward name="edit" path="/WEB-INF/pages/fileupload/fileUploadForm.jsp" |
| */ |
| public class FileUploadAction extends BaseAction { |
|
| /** |
| * The default threshold above which uploads will be stored on disk. |
| */ |
| static File uploadDir = null; |
| public static final String ADVERTISE_ID_FORMNAME = "advertiseId"; |
|
| static { |
| //Set uploadDir |
| if (uploadDir == null) { |
| uploadDir = new File(System.getProperty("java.io.tmpdir")); |
| } |
| if (uploadDir == null) throw new RuntimeException("System.getProperty(\"java.io.tmpdir\") returns null"); |
| } |
|
|
| public ActionForward save(ActionMapping mapping, ActionForm form, |
| HttpServletRequest request, |
| HttpServletResponse response) |
| throws Exception { |
| if (log.isDebugEnabled()) {log.debug("Entering 'save' method");} |
| try { |
| FileUploadForm uploadForm = (FileUploadForm) form; |
| log.debug("uploadForm = " + uploadForm); |
| StringBuffer ufMessage = new StringBuffer(); |
| //String uploadDir = "S:/temp/upload/"; |
| log.debug("uploadDir = " + uploadDir); |
|
| if (uploadForm != null) { |
| if (uploadForm.getFile1().getFileName().length() > 0) { |
| UploadUtil.writeToDisk(uploadForm.getFile1(), uploadForm.getFile1().getFileName(), uploadDir.getAbsolutePath()); |
| ufMessage.append("Uploaded File:<strong> " + uploadForm.getFile1().getFileName() + "</strong> is done.. <br />"); |
| } |
| if (uploadForm.getFile2().getFileName().length() > 0) { |
| UploadUtil.writeToDisk(uploadForm.getFile2(), uploadForm.getFile2().getFileName(), uploadDir.getAbsolutePath()); |
| ufMessage.append("Uploaded File:<strong> " + uploadForm.getFile2().getFileName() + "</strong> is done.. <br />"); |
| } |
| if (uploadForm.getFile3().getFileName().length() > 0) { |
| UploadUtil.writeToDisk(uploadForm.getFile3(), uploadForm.getFile3().getFileName(), uploadDir.getAbsolutePath()); |
| ufMessage.append("Uploaded File:<strong> " + uploadForm.getFile3().getFileName() + "</strong> is done.. <br />"); |
| } |
| if (uploadForm.getFile4().getFileName().length() > 0) { |
| UploadUtil.writeToDisk(uploadForm.getFile4(), uploadForm.getFile4().getFileName(), uploadDir.getAbsolutePath()); |
| ufMessage.append("Uploaded File:<strong> " + uploadForm.getFile4().getFileName() + "</strong> is done.. <br />"); |
| } |
| request.setAttribute("uploadLog", ufMessage.toString()); |
| log.debug("uploadLog = " + ufMessage.toString()); |
| } |
| } catch (Exception e) { |
| //TODO |
| log.error("error = " + e); |
| } |
| if (log.isDebugEnabled()) {log.debug("Leaving 'save' method");} |
| return new ActionForward("/", true); |
| } |
|
| public ActionForward edit(ActionMapping mapping, |
| ActionForm form, |
| HttpServletRequest request, |
| HttpServletResponse response) { |
| if (log.isDebugEnabled()) {log.debug("Entering 'edit' method");}; |
|
| ActionForward actionForward = null; |
| //do more here if you need to. |
| actionForward = mapping.findForward("edit"); |
|
| if (log.isDebugEnabled()) { log.debug("Leaving 'edit' method"); } |
| return actionForward; |
| } |
|
| /** |
| * |
| * @param mapping |
| * @param form |
| * @param request |
| * @param response |
| * @return |
| * @throws Exception |
| */ |
| public ActionForward unspecified(ActionMapping mapping, ActionForm form, |
| HttpServletRequest request, |
| HttpServletResponse response) |
| throws Exception { |
| if (log.isDebugEnabled()) { log.debug("Entering 'unspecified' method"); } |
|
| if (log.isDebugEnabled()) { log.debug("Leaving 'unspecified' method"); } |
| return new ActionForward("/", true); |
| } |
| } |
| }}} |
|
| At line 175 changed 3 lines. |
| !!Create the Struts Action |
|
| !!Create the Atruts JSP incliding the form |
| !!Create the Struts form JSP |