一、配置上传解析器
首先要配置项目的框架,也就是倒导入\"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\"中修改,代码如下:
sruts.multipart.parser=cos
二、实现文件上传的Action
创建表单:upload.jsp
<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"UTF-8\"%> Jsp代码
1. <%
2. String path = request.getContextPath();
3. String basePath = request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\"; 4. %> 5.
6.
7. 8.
9.
11.
13.
14. 15.
16.
17.
18. 21.
22. 23.
24.
25.26.
27.
43.package net.hncu.struts2.action;
Java代码
1. import java.io.File;
2. import java.io.FileInputStream; 3. import java.io.FileOutputStream; 4. import java.io.InputStream; 5. import java.io.OutputStream; 6.
7. import org.apache.struts2.ServletActionContext; 8.
9. import com.opensymphony.xwork2.ActionSupport; 10.
11.public class UploadAction extends ActionSupport { 12. // username属性用来封装用户名 13. private String username; 14.
15. // myFile属性用来封装上传的文件 16. private File myFile; 17.
18. // myFileContentType属性用来封装上传文件的类型 19. private String myFileContentType; 20.
21. // myFileFileName属性用来封装上传文件的文件名 22. private String myFileFileName; 23.
24.
25. //获得username值
26. public String getUsername() { 27. return username; 28. } 29.
30. //设置username值
31. public void setUsername(String username) { 32. this.username = username;
33. } 34.
35. //获得myFile值
36. public File getMyFile() { 37. return myFile; 38. } 39.
40. //设置myFile值
41. public void setMyFile(File myFile) { 42. this.myFile = myFile; 43. } 44.
45. //获得myFileContentType值
46. public String getMyFileContentType() { 47. return myFileContentType; 48. } 49.
50. //设置myFileContentType值
51. public void setMyFileContentType(String myFileContentType) {
52. this.myFileContentType = myFileContentType; 53. } 54.
55. //获得myFileFileName值
56. public String getMyFileFileName() { 57. return myFileFileName; 58. } 59.
60. //设置myFileFileName值
61. public void setMyFileFileName(String myFileFileName) { 62. this.myFileFileName = myFileFileName; 63. } 64.
65. public String execute() throws Exception { 66.
67. //基于myFile创建一个文件输入流
68. InputStream is = new FileInputStream(myFile); 69.
70. // 设置上传文件目录
71. String uploadPath = ServletActionContext.getServletContext()
72. .getRealPath(\"/upload\"); 73.
74. // 设置目标文件
75. File toFile = new File(uploadPath, this.getMyFileFileName()); 76.
77. // 创建一个输出流
78. OutputStream os = new FileOutputStream(toFile); 79.
80. //设置缓存
81. byte[] buffer = new byte[1024]; 82.
83. int length = 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. return SUCCESS; 99. } 100. 101. } 配置上传Action
Java代码
1. 2. \"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN\"
3. \"http://struts.apache.org/dtds/struts-2.0.dtd\"> 4. 6. 7. 8. 13. 14. 15.
测试页面:
因篇幅问题不能全部显示,请点此查看更多更全内容