技术要点
本节代码详细说明文件上传功能的开发流程,介绍知识点如下:
本节代码详细说明文件上传功能的开发流程,介绍知识点如下:
1. 文件上传页面和显示上传成功页面代码内容。
2. UploadAction类中实现上传功能方法和上传文件属性介绍。
3. struts.xml中UploadAction配置,以及字符编码、文件临时存放路径配置。
4. 上传后所处路径和最终上传成功后效果展示。
2. UploadAction类中实现上传功能方法和上传文件属性介绍。
3. struts.xml中UploadAction配置,以及字符编码、文件临时存放路径配置。
4. 上传后所处路径和最终上传成功后效果展示。
1. [代码][Java]代码
01 | <!---------------------文件名:upload.jsp-----------------> |
02 | <% @taglib prefix= "s" uri= "/struts-tags" %> |
03 | <html> |
04 | <head> |
05 | <meta http-equiv= "Content-Type" content= "text/html; charset=gb2312" > |
06 | <title>上传文件</title> |
07 | </head> |
08 | <body> |
09 | <!-- 上传文件表单定义 --> |
10 | <s:form action= "upload" method= "post" enctype= "multipart/form-data" > |
11 | <tr> |
12 | <!-- 上传文件标签定义 --> |
13 | <td>上传文件:<s:file name= "file" ></s:file></td> |
14 | </tr> |
15 | <tr> |
16 | <td>再次上传文件:<s:file name= "file" ></s:file></td> |
17 | </tr> |
18 | <tr> |
19 | <td align= "left" ><s:submit name= "submit" value= "提交" ></s:submit></td> |
20 | </tr> |
21 | </s:form> |
22 | </body> |
23 | </html> |
2. [代码][Java]代码
01 | <!-------------------上传文件成功后结果页面文件名:result.jsp -----------------> |
02 | <% @taglib prefix= "s" uri= "/struts-tags" %> |
03 | <html> |
04 | <head> |
05 | <meta http-equiv= "Content-Type" content= "text/html; charset=gb2312" > |
06 | <title>上传结果</title> |
07 | </head> |
08 | <body> |
09 | 上传文件: |
10 | <!-- 显示上传成功文件名 --> |
11 | <s:property value= "fileFileName" /> |
12 | </body> |
13 | </html> |
3. [代码][Java]代码
01 | <!------------------文件名:UploadAction.java ------------------> |
02 | import java.io.File; |
03 | import java.io.FileInputStream; |
04 | import java.io.FileNotFoundException; |
05 | import java.io.FileOutputStream; |
06 | import java.io.IOException; |
07 | import java.io.InputStream; |
08 | import java.io.OutputStream; |
09 | import java.util.List; |
10 |
11 | import org.apache.struts2.ServletActionContext; |
12 | import com.opensymphony.xwork2.ActionSupport; |
13 |
14 | //文件上传Action |
15 | public class UploadAction extends ActionSupport { |
16 | //上传文件存放路径 |
17 | private final static String UPLOADDIR = "/upload" ; |
18 | //上传文件集合 |
19 | private List<File> file; |
20 | //上传文件名集合 |
21 | private List<String> fileFileName; |
22 | //上传文件内容类型集合 |
23 | private List<String> fileContentType; |
24 |
25 | public List<File> getFile() { |
26 | return file; |
27 | } |
28 |
29 | public void setFile(List<File> file) { |
30 | this .file = file; |
31 | } |
32 |
33 | public List<String> getFileFileName() { |
34 | return fileFileName; |
35 | } |
36 |
37 | public void setFileFileName(List<String> fileFileName) { |
38 | this .fileFileName = fileFileName; |
39 | } |
40 |
41 | public List<String> getFileContentType() { |
42 | return fileContentType; |
43 | } |
44 |
45 | public void setFileContentType(List<String> fileContentType) { |
46 | this .fileContentType = fileContentType; |
47 | } |
48 |
49 | public String execute() throws Exception { |
50 | for ( int i = 0 ; i < file.size(); i++) { |
51 | //循环上传每个文件 |
52 | uploadFile(i); |
53 | } |
54 | return "success" ; |
55 | } |
56 |
57 | //执行上传功能 |
58 | private void uploadFile( int i) throws FileNotFoundException, IOException { |
59 | try { |
60 | InputStream in = new FileInputStream(file.get(i)); |
61 | String dir = ServletActionContext.getRequest().getRealPath(UPLOADDIR); |
62 | File uploadFile = new File(dir, this .getFileFileName().get(i)); |
63 | OutputStream out = new FileOutputStream(uploadFile); |
64 | byte [] buffer = new byte [ 1024 * 1024 ]; |
65 | int length; |
66 | while ((length = in.read(buffer)) > 0 ) { |
67 | out.write(buffer, 0 , length); |
68 | } |
69 |
70 | in.close(); |
71 | out.close(); |
72 | } catch (FileNotFoundException ex) { |
73 | ex.printStackTrace(); |
74 | } catch (IOException ex) { |
75 | ex.printStackTrace(); |
76 | } |
77 | } |
78 | } |
4. [代码][Java]代码
01 | struts.xml配置文件中有关文件上传的配置: |
02 | <!--------------------文件名:struts.xml-------------------> |
03 | <struts> |
04 | <!-- 系统常量定义,定义上传文件字符集编码 --> |
05 | <constant name= "struts.i18n.encoding" value= "gb2312" ></constant> |
06 | <!-- 系统常量定义,定义上传文件临时存放路径 --> |
07 | <constant name= "struts.multipart.saveDir" value="c:\"></constant> |
08 | <!-- Action所在包定义 --> |
09 | < package name= "C04.4" extends = "struts-default" > |
10 | <!-- Action名字,类以及导航页面定义 --> |
11 | <!-- 通过Action类处理才导航的的Action定义 --> |
12 | <action name= "upload" class = "action.UploadAction" > |
13 | <result name= "input" >/jsp/upload.jsp</result> |
14 | <result name= "success" >/jsp/result.jsp</result> |
15 | </action> |
16 | </ package > |
17 | </struts> |