基于Struts2的文件上传机制

上传人:痛*** 文档编号:97281395 上传时间:2022-05-27 格式:DOC 页数:8 大小:258.50KB
返回 下载 相关 举报
基于Struts2的文件上传机制_第1页
第1页 / 共8页
基于Struts2的文件上传机制_第2页
第2页 / 共8页
基于Struts2的文件上传机制_第3页
第3页 / 共8页
点击查看更多>>
资源描述
Struts2本身并没提供上传的组件,我们可以通过调用上传框架来实现文件的上传。一、配置上传解析器首先要配置项目的框架,也就是倒导入struts2-core-2.2.1.jar库文件,找到org.apache.struts2包下的default.porperties资源文件。如下图;资源文件中给出了不同的strus2的默认配置,我们可看到struts2默认是jakarta作为其文件上传的解析器。jakarta是Commo-FileUpload的框架。如果要使用Commo-FileUpload框架来上传文件,只需将commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar两个jar复制到项目中的WEB-INF/lib目录下就可。如果想要使用COS框架来上传文件,只需将“cos.jar”复制到项目中就可以,然后在修改struts.multipart.parser常量值。修改常量值有两种方法,一是在struts.xml中修改,代码如下:二是在struts.properties中修改,代码如下:sruts.multipart.parser=cos二、实现文件上传的Action创建表单:upload.jsp Jsp代码 1. 5. 6. 7. 8. 9. basehref= 10. 11. Struts2文件上传 12. 13. 14. 15. 16. 17. 18. !- 19. 20. - 21. 22. 23. 24. 25. 26. Struts2完成上传 27. 28. 29. 30. 用户名: 31. 32. 33. 34. 上传文件: 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. base href= Struts2文件上传 !- Struts 2完成上传 用户名: 上传文件: 完成上传Actionpackage net.hncu.struts2.action;Java代码 1. importjava.io.File; 2. importjava.io.FileInputStream; 3. importjava.io.FileOutputStream; 4. importjava.io.InputStream; 5. importjava.io.OutputStream; 6. 7. importorg.apache.struts2.ServletActionContext; 8. 9. importcom.opensymphony.xwork2.ActionSupport; 10. 11. publicclassUploadActionextendsActionSupport 12. /username属性用来封装用户名 13. privateStringusername; 14. 15. /myFile属性用来封装上传的文件 16. privateFilemyFile; 17. 18. /myFileContentType属性用来封装上传文件的类型 19. privateStringmyFileContentType; 20. 21. /myFileFileName属性用来封装上传文件的文件名 22. privateStringmyFileFileName; 23. 24. 25. /获得username值 26. publicStringgetUsername() 27. returnusername; 28. 29. 30. /设置username值 31. publicvoidsetUsername(Stringusername) 32. this.username=username; 33. 34. 35. /获得myFile值 36. publicFilegetMyFile() 37. returnmyFile; 38. 39. 40. /设置myFile值 41. publicvoidsetMyFile(FilemyFile) 42. this.myFile=myFile; 43. 44. 45. /获得myFileContentType值 46. publicStringgetMyFileContentType() 47. returnmyFileContentType; 48. 49. 50. /设置myFileContentType值 51. publicvoidsetMyFileContentType(StringmyFileContentType) 52. this.myFileContentType=myFileContentType; 53. 54. 55. /获得myFileFileName值 56. publicStringgetMyFileFileName() 57. returnmyFileFileName; 58. 59. 60. /设置myFileFileName值 61. publicvoidsetMyFileFileName(StringmyFileFileName) 62. this.myFileFileName=myFileFileName; 63. 64. 65. publicStringexecute()throwsException 66. 67. /基于myFile创建一个文件输入流 68. InputStreamis=newFileInputStream(myFile); 69. 70. /设置上传文件目录 71. StringuploadPath=ServletActionContext.getServletContext() 72. .getRealPath(/upload); 73. 74. /设置目标文件 75. FiletoFile=newFile(uploadPath,this.getMyFileFileName(); 76. 77. /创建一个输出流 78. OutputStreamos=newFileOutputStream(toFile); 79. 80. /设置缓存 81. bytebuffer=newbyte1024; 82. 83. intlength=0; 84. 85. /读取myFile文件输出到toFile文件中 86. while(length=is.read(buffer)0) 87. os.write(buffer,0,length); 88. 89. System.out.println(上传用户+username); 90. System.out.println(上传文件名+myFileFileName); 91. System.out.println(上传文件类型+myFileContentType); 92. /关闭输入流 93. is.close(); 94. 95. /关闭输出流 96. os.close(); 97. 98. returnSUCCESS; 99. 100. 101. import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport / username属性用来封装用户名private String username;/ myFile属性用来封装上传的文件private File myFile;/ myFileContentType属性用来封装上传文件的类型private String myFileContentType;/ myFileFileName属性用来封装上传文件的文件名private String myFileFileName;/获得username值public String getUsername() return username;/设置username值public void setUsername(String username) this.username = username;/获得myFile值public File getMyFile() return myFile;/设置myFile值public void setMyFile(File myFile) this.myFile = myFile;/获得myFileContentType值public String getMyFileContentType() return myFileContentType;/设置myFileContentType值public void setMyFileContentType(String myFileContentType) this.myFileContentType = myFileContentType;/获得myFileFileName值public String getMyFileFileName() return myFileFileName;/设置myFileFileName值public void setMyFileFileName(String myFileFileName) this.myFileFileName = myFileFileName;public String execute() throws Exception /基于myFile创建一个文件输入流InputStream is = new FileInputStream(myFile);/ 设置上传文件目录String uploadPath = ServletActionContext.getServletContext().getRealPath(/upload);/ 设置目标文件File toFile = new File(uploadPath, this.getMyFileFileName();/ 创建一个输出流OutputStream os = new FileOutputStream(toFile);/设置缓存byte buffer = new byte1024;int length = 0;/读取myFile文件输出到toFile文件中while (length = is.read(buffer) 0) os.write(buffer, 0, length);System.out.println(上传用户+username);System.out.println(上传文件名+myFileFileName);System.out.println(上传文件类型+myFileContentType);/关闭输入流is.close();/关闭输出流os.close();return SUCCESS;配置上传ActionJava代码 1. 4. 5. 6. 7. 8. /result.jsp 9. /upload.jsp 10. 11. 12. 13. 14. 15. 测试页面:
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!