Struts 2 文件上传

1.单文件上传

2.多文件上传

 

   ======================  华丽丽的分割线  ====================== 

 

1.文件上传 --  基本四步走:

 

第一步,确认是否有加入commons-fileupload-x.x.x.jar, 与commons-io-x.x.x.jar包(名字记不牢,可能有误),在struts-2-2-x中,直接把app中的struts-blank的包拷进去即可。

 

第二步,在html中写一个提交文件的表单。如下:

<form enctype="multipart/form-data" action="uploadfile" method="post">
文件上传:
<input type="file" name="upload">
<input type="submit" value="上传">
</form>

唯一要注意的:是enctype="multipart/form-data",这个别漏写。

 

第三步,编写配置文件,指明要处理上传的action类(uploadFile):

<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="cn.hasone.test.Upload">
<result>/index.jsp</result>
</action>
<action name="uploadfile" class="cn.hasone.test.UploadFile">
<result>/index.jsp</result>
</action>
</package>

 

第四步,编写处理上传文件的action类(本人没域名,就用“有个”将就)

1 package cn.hasone.test;
2
3  import java.io.File;
4
5  import org.apache.commons.io.FileUtils;
6  import org.apache.struts2.ServletActionContext;
7
8  import com.opensymphony.xwork2.ActionSupport;
9
10  public class UploadFile extends ActionSupport {
11
12 private File upload;
13
14 private String uploadFileName;
15
16 @Override
17 public String execute() throws Exception {
18 uploadFile();
19 return SUCCESS;
20 }
21
22 public void uploadFile() throws Exception{
23 String realPath = ServletActionContext.getServletContext().getRealPath("/upload");
24 File saveFile = new File(realPath,uploadFileName);
25 if (upload != null) {
26 if (!saveFile.getParentFile().exists()) {
27 saveFile.getParentFile().mkdirs();
28 }
29 System.out.println("文件保存的目录为:" + realPath);
30 FileUtils.copyFile(upload, saveFile);
31 }
32 }
33
34 public File getUpload() throws Exception{
35 return upload;
36 }
37
38 public void setUpload(File upload) {
39 this.upload = upload;
40 }
41
42 public String getUploadFileName() {
43 return uploadFileName;
44 }
45
46 public void setUploadFileName(String uploadFileName) {
47 this.uploadFileName = uploadFileName;
48 }
49
50 }

OK,这样就可以了,但须说明三点:

1.命名:文件命名须与上传表格的name一致,文件名字的命名须前部分与name一致,后部分为FileName。(在本例中,上传的name为upload,故程序中File名字为upload,上传文件名为uploadFileName,且须编写set/get方法)

2.上传的大小是有限制的,可以通过指明struts.xml的 struts.multipart.maxSize 的值做改变,具体查看:http://www.cnblogs.com/loveis/articles/2005034.html

3.上传的东西若过大,多数失败,若要处理大文件,如视频等,须编写个socket插件来处理上传业务,而不能依赖html。

  

   ======================  华丽丽的分割线  ======================

     

2.多文件上传:

 

处理多文件上传,有了上面的基础后,多文件上传也变得很简单~!

  

第一步,和单文件上传的第一步一样。

  

第二步,在html中写一个具有多文件提交的表单。如下:

<form enctype="multipart/form-data" action="uploadfiles" method="post">
文件上传:
<input type="file" name="upload">
文件上传:
<input type="file" name="upload">
文件上传:
<input type="file" name="upload">
<input type="submit" value="上传">
</form>

注意,name属性的值须一致。。。

  

第三步,编写配置文件,指明要处理上传的action类(uploadFiles):

<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="cn.hasone.test.Upload">
<result>/index.jsp</result>
</action>
<action name="uploadfiles" class="cn.hasone.test.UploadFiles">
<result>/index.jsp</result>
</action>
</package>

  

第四步,编写处理多文件上传的action类

1 package cn.hasone.test;
2
3 import java.io.File;
4
5 import org.apache.commons.io.FileUtils;
6 import org.apache.struts2.ServletActionContext;
7
8 import com.opensymphony.xwork2.ActionSupport;
9
10 public class UploadFiles extends ActionSupport {
11 private File[] upload;
12
13 private String[] uploadFileName;
14
15 @Override
16 public String execute() throws Exception {
17 uploadFile();
18 return SUCCESS;
19 }
20
21 public void uploadFile() throws Exception{
22 String realPath = ServletActionContext.getServletContext().getRealPath("/upload");
23 File saveDir = new File(realPath);
24 if(!saveDir.getParentFile().exists()){
25 saveDir.getParentFile().mkdirs();
26 }
27 for(int i = 0;i < upload.length;i++){
28 File saveFile = new File(saveDir,uploadFileName[i]);
29 FileUtils.copyFile(upload[i], saveFile);
30 }
31 System.out.println("文件保存的目录为:" + realPath);
32
33 }
34
35 public File[] getUpload() throws Exception{
36 return upload;
37 }
38
39 public void setUpload(File[] upload) {
40 this.upload = upload;
41 }
42
43 public String[] getUploadFileName() {
44 return uploadFileName;
45 }
46
47 public void setUploadFileName(String[] uploadFileName) {
48 this.uploadFileName = uploadFileName;
49 }
50
51 }

posted on 2011-04-04 09:59  五月十七  阅读(265)  评论(0)    收藏  举报

导航