首页 > 代码库 > struts2学习(13)struts2文件上传和下载(1)

struts2学习(13)struts2文件上传和下载(1)

一、Struts2文件上传:

技术分享

二、配置文件的大小以及允许上传的文件类型:

技术分享

三、大文件上传:

技术分享

如果不配置上传文件的大小,struts2默认允许上传文件最大为2M; 2097152Byte;
 
例子实现:
技术分享

com.cy.action.FileUploadAction.java:

package com.cy.action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    
    private File struts;                //文件,对应fileupload.jsp中input type=file的name
    private String strutsFileName;        //文件名
    private String strutsContentType;    //文件类型
    
    public File getStruts() {
        return struts;
    }
    public void setStruts(File struts) {
        this.struts = struts;
    }
    public String getStrutsFileName() {
        return strutsFileName;
    }
    public void setStrutsFileName(String strutsFileName) {
        this.strutsFileName = strutsFileName;
    }
    public String getStrutsContentType() {
        return strutsContentType;
    }
    public void setStrutsContentType(String strutsContentType) {
        this.strutsContentType = strutsContentType;
    }
    
    @Override
    public String execute() throws Exception {
        System.out.println("文件名:" + strutsFileName);
        System.out.println("文件类型:" + strutsContentType);
        String filePath = "E:/" + strutsFileName;
        File savefile = new File(filePath);
        FileUtils.copyFile(struts, savefile);
        return SUCCESS;
    }
    
    
}

struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    
    <!-- 配置允许上传文件最大为20000000Byte,约20Mb -->
    <constant name="struts.multipart.maxSize" value="20000000"></constant>
    
    <package name="manage" extends="struts-default">
        <action name="upload" class="com.cy.action.FileUploadAction">
            <result name="input">fileupload.jsp</result>
            <result name="success">success.jsp</result>
            
            <!-- 配置允许上传的文件类型
                  配置允许上传的文件大小,最大81101byte,= 79.2KB
             -->
            <interceptor-ref name="fileUpload">
                <param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param>
                <param name="maximumSize">81101</param>
            </interceptor-ref>
            
            <interceptor-ref name="defaultStack"></interceptor-ref>
        </action>
        
    </package>
    
</struts>

fileupload.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <!-- 显示文件上传错误时,返回的失败信息 -->
    <s:fielderror></s:fielderror>
    <form action="upload" method="post" enctype="multipart/form-data">
        选择文件:<input type="file" name="struts" /><br/>
        <input type="submit" value="提交" />
    </form>
</body>
</html>

success.jsp:

技术分享
<body>
文件上传成功!
</body>
View Code

 

测试结果:

1)配置允许上传的文件类型,和文件大小;

技术分享

 

 

2)配置大文件上传,上面例子中配置了20M左右;

如果不配置大文件上传,上传大于2M的会报错:

技术分享

 

 

------------

struts2学习(13)struts2文件上传和下载(1)