upload.jsp


   
  1. <%@ page language="java" pageEncoding="GB18030"%>  
  2. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>   
  3. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>  
  4. <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>  
  5. <%  
  6. String path = request.getContextPath();  
  7. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  8. %>  
  9. <html>   
  10. <head>  
  11. <title>JSP for UploadForm form</title>  
  12. </head>  
  13. <body>  
  14. <html:form action="/upload" enctype="multipart/form-data">  
  15. file : <html:file property="file"/><html:errors property="file"/><br/>  
  16. <html:submit value="上传"/>  
  17. </html:form>  
  18. <hr>  
  19. <logic:present name="fileName">  
  20. <html:link href="<%=basePath+"myupload/"+request.getAttribute("fileName") %>" 
  21. target="_blank" useLocalEncoding="gb2312">  
  22. <bean:write name="fileName"/>  
  23. </html:link>  
  24. <bean:write name="size"/>  
  25. </logic:present>  
  26. </body>  
  27. </html> 

struts-config.xml


   
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">  
  3.  
  4. <struts-config>  
  5. <form-beans >  
  6. <form-bean name="uploadForm" type="com.huqianhao.struts.form.UploadForm" />  
  7. </form-beans>  
  8.  
  9. <global-exceptions />  
  10. <global-forwards />  
  11. <action-mappings >  
  12. <action 
  13. attribute="uploadForm" 
  14. input="/upload.jsp" 
  15. name="uploadForm" 
  16. path="/upload" 
  17. scope="request" 
  18. type="com.huqianhao.struts.action.UploadAction">  
  19. <forward name="upload" path="/upload.jsp" />  
  20. </action>  
  21.  
  22. </action-mappings>  
  23.  
  24. <message-resources parameter="com.huqianhao.struts.ApplicationResources" />  
  25. </struts-config> 

UploadAction.java


   
  1. /*  
  2. * Generated by MyEclipse Struts  
  3. * Template path: templates/java/JavaClass.vtl  
  4. */  
  5. package com.huqianhao.struts.action;  
  6.  
  7. import java.io.FileNotFoundException;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import java.io.InputStream;  
  11. import java.io.OutputStream;  
  12.  
  13. import javax.servlet.http.HttpServletRequest;  
  14. import javax.servlet.http.HttpServletResponse;  
  15.  
  16. import org.apache.struts.action.Action;  
  17. import org.apache.struts.action.ActionForm;  
  18. import org.apache.struts.action.ActionForward;  
  19. import org.apache.struts.action.ActionMapping;  
  20. import org.apache.struts.upload.FormFile;  
  21.  
  22. import com.huqianhao.struts.form.UploadForm;  
  23.  
  24. /**  
  25. * MyEclipse Struts Creation date: 01-12-2008  
  26. *   
  27. * XDoclet definition:  
  28. *   
  29. * @struts.action path="/upload" name="uploadForm" input="/upload.jsp" 
  30. * scope="request" validate="true" 
  31. */  
  32. public class UploadAction extends Action {  
  33. /*  
  34. * Generated Methods  
  35. */  
  36.  
  37. /**  
  38. * Method execute 
  39. *   
  40. * @param mapping  
  41. * @param form  
  42. * @param request  
  43. * @param response  
  44. * @return ActionForward  
  45. */  
  46. public ActionForward execute(ActionMapping mapping, ActionForm form,  
  47. HttpServletRequest request, HttpServletResponse response) {  
  48. UploadForm uploadForm = (UploadForm) form;  
  49. FormFile file = uploadForm.getFile();  
  50. String fileName = file.getFileName();  
  51. String contentType = file.getContentType();  
  52. String size = (file.getFileSize() + " bytes");  
  53. try {  
  54. InputStream stream = file.getInputStream();  
  55. // 把内容写到指定的文件中  
  56. OutputStream bos = new FileOutputStream(request.getRealPath("")  
  57. "/myupload/" + fileName);  
  58. int bytesRead = 0;  
  59. byte[] buffer = new byte[8192];  
  60. while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {  
  61. bos.write(buffer, 0, bytesRead);  
  62. }  
  63. bos.close();  
  64. stream.close();  
  65. } catch (FileNotFoundException e) {  
  66. e.printStackTrace();  
  67. } catch (IOException e) {  
  68. e.printStackTrace();  
  69. }  
  70. request.setAttribute("fileName", fileName);  
  71. request.setAttribute("size"size);  
  72. request.setAttribute("contentType", contentType);  
  73. return mapping.findForward("upload");  
  74. }  

UploadForm.java


   
  1. /*  
  2. * Generated by MyEclipse Struts  
  3. * Template path: templates/java/JavaClass.vtl  
  4. */  
  5. package com.huqianhao.struts.form;  
  6.  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import org.apache.struts.action.ActionErrors;  
  9. import org.apache.struts.action.ActionForm;  
  10. import org.apache.struts.action.ActionMapping;  
  11. import org.apache.struts.upload.FormFile;  
  12.  
  13. /**   
  14. * MyEclipse Struts  
  15. * Creation date: 01-12-2008  
  16. *   
  17. * XDoclet definition:  
  18. * @struts.form name="uploadForm" 
  19. */  
  20. public class UploadForm extends ActionForm {  
  21. /*  
  22. * Generated fields  
  23. */  
  24.  
  25. /** file property */  
  26. private FormFile file;  
  27.  
  28. /*  
  29. * Generated Methods  
  30. */  
  31.  
  32. /**   
  33. * Method validate  
  34. * @param mapping  
  35. * @param request  
  36. * @return ActionErrors  
  37. */  
  38. public ActionErrors validate(ActionMapping mapping,  
  39. HttpServletRequest request) {  
  40. // TODO Auto-generated method stub  
  41. return null;  
  42. }  
  43.  
  44. /**   
  45. * Method reset  
  46. * @param mapping  
  47. * @param request  
  48. */  
  49. public void reset(ActionMapping mapping, HttpServletRequest request) {  
  50. // TODO Auto-generated method stub  
  51. }  
  52.  
  53. public FormFile getFile() {  
  54. return file;  
  55. }  
  56.  
  57. public void setFile(FormFile file) {  
  58. this.file = file;  
  59. }