Struts2中文件上传下载实例

1.单文件上传

 1 jsp页面:
 2 
 3 <!-- 单文件上传 -->
 4         <form action="Fileupload.action" method="post"
 5             enctype="multipart/form-data">
 6             username:
 7             <input type="text" name="username" />
 8             <br />
 9             file:
10             <input type="file" name="file" />
11             <br />
12             <input type="submit" name="提交" />
13         </form>
14 <!--注意:一定要写enctype="multipart/form-data"-->
 1 action页面:
 2 
 3 package com.Struts2.action;
 4 
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.FileOutputStream;
 8 import java.io.InputStream;
 9 import java.io.OutputStream;
10 
11 import org.apache.struts2.ServletActionContext;
12 
13 import com.opensymphony.xwork2.ActionSupport;
14 
15 public class Fileupload extends ActionSupport {
16 
17     /**
18      * 
19      */
20     private static final long serialVersionUID = 4038542394937191173L;
21 
22     private String username;
23     private File file;
24     private String fileFileName;// 提交过来的file的名字,必须为FileName后缀
25     private String fileContentType;// 提交过来的file的MIME类型,必须以ContentType为后缀
26 
27     public String getUsername() {
28         return username;
29     }
30 
31     public void setUsername(String username) {
32         this.username = username;
33     }
34 
35     public File getFile() {
36         return file;
37     }
38 
39     public void setFile(File file) {
40         this.file = file;
41     }
42 
43     public String getFileFileName() {
44         return fileFileName;
45     }
46 
47     public void setFileFileName(String fileFileName) {
48         this.fileFileName = fileFileName;
49     }
50 
51     public String getFileContentType() {
52         return fileContentType;
53     }
54 
55     public void setFileContentType(String fileContentType) {
56         this.fileContentType = fileContentType;
57     }
58 
59     @Override
60     public String execute() throws Exception {
61         String root = ServletActionContext.getServletContext().getRealPath(
62                 "/upload");
63         InputStream is = new FileInputStream(file);
64         OutputStream os = new FileOutputStream(new File(root, fileFileName));
65 
66         System.out.println("fileFileName:" + fileFileName);
67         // 因为file是存放在临时文件夹的文件,我们可以将其文件名和文件路径打印出来,看和之前的fileFileName是否相同
68         System.out.println("file:" + file.getName());
69         System.out.println("file:" + file.getPath());
70 
71         byte[] buffer = new byte[500];// 用缓冲
72         int length = 0;
73         while (-1 != (length = is.read(buffer, 0, buffer.length))) {
74             os.write(buffer);
75         }
76         os.close();
77         is.close();
78 
79         return SUCCESS;
80     }
81 }

 

2.多文件上传

 1 jsp页面:
 2 
 3 <script type="text/javascript">
 4             $(function() {
 5             $("#button").click(function (){
 6                 var html = $("<input type='file' name='file'/><br/>");
 7                 var button = $("<input type='button' name='button' value='删除'/><br/>");
 8                 $("#body div").append(html).append(button);
 9                     button.click(function (){
10                         html.remove();
11                         button.remove();            
12                         });
13             });
14         });
15         </script>
16 <body id="body">
17 <!-- 多文件上传 -->
18         <form action="manyFileupload.action" method="post"
19             enctype="multipart/form-data">
20             username:
21             <input type="text" name="username" />
22             <br />
23             file:
24             <input type="file" name="file" />
25             <br />
26             <input type="button" value="添加" id="button" />
27             <br />
28             <div></div>
29             <input type="submit" value="提交" />
30         </form>
31     </body>
action页面:

package com.Struts2.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 多文件上传
 * 
 * @author admin
 * 
 */
public class ManyFileupload extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = -1060365794705605882L;

    private String username;
    private List<File> file;// 因为有多个文件,所以用list集合,file属性指上传到的临时空间文件夹(upload)中的绝对路径
    private List<String> fileFileName;// 上传的文件名称,自动会加载,因为是以FileName为后缀,会自动识别文件名称
    private List<String> fileContentType;// 上传过来的文件类型,以ContentType后缀,自动识别类型(如:图片类型就是image/jepg)

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public List<File> getFile() {
        return file;
    }

    public void setFile(List<File> file) {
        this.file = file;
    }

    public List<String> getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(List<String> fileFileName) {
        this.fileFileName = fileFileName;
    }

    public List<String> getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(List<String> fileContentType) {
        this.fileContentType = fileContentType;
    }

    @Override
    public String execute() throws Exception {
        String root = ServletActionContext.getServletContext().getRealPath(
                "/upload");
        for (int i = 0; i < file.size(); i++) {
            InputStream is = new FileInputStream(file.get(i));
            OutputStream os = new FileOutputStream(new File(root, fileFileName
                    .get(i)));

            byte[] buffer = new byte[500];
            int length = 0;
            while (-1 != (length = is.read(buffer, 0, buffer.length))) {
                os.write(buffer);
            }
            os.close();
            is.close();
        }
        return super.execute();
    }

}

3.文件下载

1 jsp页面
2 
3         <!-- 下载 -->
4         <form action="fileDownload.action">
5             <input type="submit" value="下载" />
6         </form>
7     
action页面

package com.Struts2.action;

import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 文件下载
 * 
 * @author admin
 * 
 */
public class FileDownload extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 509550154000070698L;

    public InputStream getDownloadFile() {
        return ServletActionContext.getServletContext().getResourceAsStream(
                "upload/psb.jpg");// 从upload中选择一张图片进行下载
    }

    @Override
    public String execute() throws Exception {
        return super.execute();
    }
}

4.struts.xml

 1     <!-- 定义全局结果:必须写在action上面 -->    
 2     <global-results>
 3         <result name="success">/success.jsp</result>
 4     </global-results>
 5     
 6     <!-- 单文件上传 -->
 7     <action name="Fileupload" class="com.Struts2.action.Fileupload"></action>
 8     <!-- 多文件上传-->
 9     <action name="manyFileupload" class="com.Struts2.action.ManyFileupload"></action>
10     <!-- 文件下载 -->
11     <action name="fileDownload" class="com.Struts2.action.FileDownload">
12         <result name="success" type="stream">
13              <param name="contentDisposition">attachment;filename="psb.jpg"</param><!-- attachment表示弹出下载提示框 -->
14              <param name="inputName">downloadFile</param><!-- downloadFile对应action中的getDownloadFile方法 -->
15         </result>
16     </action>    

 

 

本博客源码出自:http://www.cnblogs.com/xiaoluo501395377/archive/2012/10/26/2740882.html。谢谢他的分享!

 

posted @ 2016-08-08 14:57  一个java小生的奋斗史  阅读(291)  评论(0编辑  收藏  举报