Java_文件上传与下载(三)

Struts2实现


1.原理

a.表单中设置为post提交,encytype属性设置为multipart/form-dat,这样数据就以二进制流的方式传输
b.Struts2默认采用Jakarta和Common-FileUpload框架,导入相应jar包即可
c.通过Struts2的拦截器设置上传文件的类型及大小等
d.在Struts2的action中设置上传文件的内容、类型及名称 

2.文件上传

(1)Action的写法:

public class FileUploadAction extends ActionSupport {

    private File upload;    //这里必须与表单中的input的Name一致
    private String uploadContentType;
    private String uploadFileName;

    private String result;

    //省略set/get方法
    。。。。。。
    。。。。。。

    //Struts2的action中excute方法的覆写
    @Override
    public String execute() throws Exception {

        String filePath = ServletActionContext.getServletContext().getRealPath("/images");
        File file = new File(filePath);
        if(!file.exists()){
            file.mkdir();
        }
        FileUtils.copyFile(upload, new File(file,uploadFileName));
        result = "上传成功!";

        return SUCCESS;
    }
}

(2)Struts2的xml配置:*

<package name="default" namespace="/" extends="struts-default">
    <action name="upload" class="com.mystudy.action.FileUploadAction">
        <result>/jsps/struts2upload.jsp</result>
        <result name="input">/jsps/error.jsp</result>
        <!-- 拦截器配置 -->
        <interceptor-ref name="fileUpload">
            <!-- 允许上传的文件类型 -->  
            <param name="allowedTypes">image/jpeg,image/gif,image/png</param>
            <!-- 允许上传的最大文件 -->
            <param name="maximumSize">10M</param>
        </interceptor-ref>

        <interceptor-ref name="defaultStack"></interceptor-ref>             
    </action>
</package>

(3)异常处理:

引入struts2的国际化的支持,在xml中添加
    <constant name="struts.custom.i18n.resources" value="resource"/>
资源文件:假设定义了resouce_zh_CN.properties的文件

(4)批量上传:

a.jsp中多加几个input

    上传文件1:<input type="file" name="upload"/><br>
    上传文件2:<input type="file" name="upload"/><br>
    上传文件3:<input type="file" name="upload"/><br>

b.Action中的成员定义为List

    private List<File> upload;
    private List<String> uploadContentType;
    private List<String> uploadFileName;

c.保存文件时循环逐个保存

(2)文件下载

(1)Struts2的xml配置:

    <!-- 文件下载的action配置 -->  
    <action name="download" class="com.mystudy.action.FileDownloadAction">
        <param name="inputPath">/images/image2.jpg</param>
        <result name="success" type="stream">
            <param name="contentType">application/octet-stream</param>
            <param name="inputName">inputStream</param>
            <param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
            <param name="bufferSize">8192</param>
        </result>
    </action>   

(2)Action的写法:

private String inputPath;
public String filename;

public String getInputPath() {
    return inputPath;
}

public void setInputPath(String inputPath) {
    this.inputPath = inputPath;
}

@Override
public String execute() throws Exception {

    return SUCCESS;
}


public InputStream getInputStream() throws IOException{

    String path =  ServletActionContext.getServletContext().getRealPath("/images");
    String filePath = path+"\\"+ filename;
    File file = new File(filePath);
    return FileUtils.openInputStream(file);
    //return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
}

public String getDownloadFileName(){
    String downloadFileName = "";
    try {
        downloadFileName=URLEncoder.encode("下载文件.jpg","UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return downloadFileName;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值