Struts2文件的上传和下载实现

<一>简述:

Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>

<二>指定用户上传文件的大小,有两种方式:

1)默认是在default.properties 文件的 struts.multipart.maxSize=2097152  键值指定为2097152 也就是2M,通过计算 2097152/(1024*1024) = 2 M

那我们可以改变其默认值,只要在src目录下,新建一个 struts.properties 文件,指定上传大小 如下:


一次上传只可以上传10M,不管一次上传多少个文件,按总和计算

 

2)在struts.xml文件中指定,如图:


其实name就对应struts.properties的键,value对应 值

注意:如果即在struts.properties设定文件上传大小,又在struts.xml 设定文件上传大小,则struts.properties的优先级高于struts.xml,一般在一处指定上传大小即可,推荐 struts.properties

 一、单文件上传

首先是一个jsp文件上传页面,这个比较简单,就是一个表单,里面有个文件上传框

<!--在进行文件上传时,表单提交方式一定要是post的方式,因为文件上传时二进制文件可能会很大,还有就是enctype属性,这个属性一定要写成multipart/form-data,
  不然就会以二进制文本上传到服务器端--> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>文件上传</h1>
<form action="Upload_upload" method="post" enctype="multipart/form-data">
    标题:<input type="text" name="title"><br>
    附件:<input type="file" name="file"><br>
    <input type="submit" value="submit">
</form>
</body>
</html>

接下来是UploadAction部分代码,因为struts2对上传和下载都提供了很好的实习机制,所以在action这段我们只需要写很少的代码就行:

public class UploadAction extends ActionSupport{
    
    private String title;
        //注意,file并不是指前端jsp上传过来的文件本身,而是文件上传过来存放在临时文件夹下面的文件
    private File file;
        //提交过来的file的名字
    private String fileFileName;
        //提交过来的file的MIME类型
    private String fileContentType;
    

    public String getFileContentType() {
        return fileContentType;
    }

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

    public String getFileFileName() {
        return fileFileName;
    }

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

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public File getFile() {
        return file;
    }

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

    public String input(){
        return SUCCESS;
    }
    
    public String upload() throws IOException{
        //System.out.println(file.length());
        //为了防止上传的文件重名,可以在文件前面加当前日期,随机数字,文件名称等
        Random rand = new Random();
        int n = rand.nextInt(9999);
        DecimalFormat format = new DecimalFormat("0000");
        String sss = format.format(n);
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        Date now = Calendar.getInstance().getTime();
        String prefix = sdf.format(now);
        String fileName = prefix +"_"+ sss+"_" + fileFileName;
//        System.out.println(fileFileName);
//        System.out.println(fileContentType );
        File destFile = new File("d:\"+fileName);
        FileUtils.copyFile(file, destFile);
        return SUCCESS;
    }
    
}    

首先我们要清楚一点,这里的file并不是真正指代jsp上传过来的文件,当文件上传过来时,struts2首先会寻找struts.multipart.saveDir(这个是在default.properties里面有)这个name所指定的存放位置,我们可以新建一个struts.properties属性文件来指定这个临时文件存放位置,如果没有指定,那么文件会存放在tomcat的apache-tomcat-7.0.29workCatalinalocalhost目录下,然后我们可以指定文件上传后的存放位置,通过输出流将其写到流里面就行了,这时我们就可以在文件夹里看到我们上传的文件了。

Strurts.XML文件的配置

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <constant name="struts.ui.theme" value="simple"></constant>
    <constant name="struts.multipart.maxSize" value="20971520"></constant>
    <package name="default" namespace="/" extends="struts-default">
    <!--通过一个通用的Action就可以实现功能。下面的上传多个文件同理这个Action-->
        <action name="*_*" class="com.itnba.maya.controller.{1}Action" method="{2}">
            
            <result>{1}/{2}.jsp</result>
        </action>
        
    </package>
 
</struts>

struts2多文件上传:

其实多文件上传和单文件上传原理一样,单文件上传过去的是单一的File,多文件上传过去的就是一个List<File>集合或者是一个File[]数组,首先我们来看一下前端jsp部分的代码,这里我用到了jquery来实现动态的添加文件下载框以及动态的删除下载框:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>多文件上传</h1>
<form action="UploadMany_upload" method="post" enctype="multipart/form-data">
    标题:<input type="text" name="title"><br>
    附件1:<input type="file" name="file"><br>
    附件2:<input type="file" name="file"><br>
    附件3:<input type="file" name="file"><br>
    附件4:<input type="file" name="file"><br>
    附件5:<input type="file" name="file"><br>
    <input type="submit" value="submit">
</form>
</body>
</html>

file的名字必须都命名成file才行,然后处理多文件上传的action代码如下:

public class UploadManyAction extends ActionSupport{
    private String title;
    private File[] file;//这个位置必须都改成数组的形式
    private String[] fileFileName;
    private String[] fileContentType;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public File[] getFile() {
        return file;
    }
    public void setFile(File[] file) {
        this.file = file;
    }
    public String[] getFileFileName() {
        return fileFileName;
    }
    public void setFileFileName(String[] fileFileName) {
        this.fileFileName = fileFileName;
    }
    public String[] getFileContentType() {
        return fileContentType;
    }
    public void setFileContentType(String[] fileContentType) {
        this.fileContentType = fileContentType;
    }
    
    public String input(){
        return SUCCESS;
    }
    
    public String upload() throws IOException{
        
        for(int i=0;i<file.length;i++){   //用循环把数组遍历出来然后上传进去,这里的名称修改就不再列出,同理单个文件上传。
            File item = file[i];
            if(item != null){
                File temp = new File("d:\"+fileFileName[i]);
                FileUtils.copyFile(item, temp);
            }
        }
        
        return SUCCESS;
    }
}

Sturts.XML文件和单个文件上传的一样。这样就可以通过Struts2上传文件了。

原文地址:https://www.cnblogs.com/kuangwong/p/6588925.html