struts2文件上传和下载

Struts2文件上传和下载

1. 文件上传

1.1单文件上传

Action


public class FileUploadAction extends ActionSupport {
    //具体上传文件的 引用 , 指向临时目录中的临时文件
    
private File upload;
    //上传文件的类型, ContentType 固定的写法
    
private String uploadContentType;
    // 上传文件的名字 ,FileName 固定的写法
    
private String uploadFileName;
    //上传文件的路径
    
private String savePath;
    @Override
    public String execute() throws Exception {
        byte[] buffer=new byte[1024];
        FileInputStream fis=new FileInputStream(getUpload());
        FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
        int length=fis.read(buffer);
        while (length>0){
            fos.write(buffer,0,length);
            length=fis.read(buffer);
        }
        fos.flush();
        fos.close();
        fis.close();

        return SUCCESS;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getSavePath() {
        //getRealPath()把相对路径转化为绝对路径
        
return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

}

Struts.xml配置

<action name="uploadAction" class="cn.bdqn.file.FileUploadAction">
    <param name="savePath">/upload</param>
    <result name="success">/stuts2Upload/strutsupload.jsp</result>
</action>

 

fileupload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Struts2文件上传</title>
</head>
<body>
<s:form action="uploadAction" enctype="multipart/form-data" method="post">
<s:file name="upload" label="选择文件"/><br/>
<s:submit name="submit" value="文件上传"/>
</s:form>
</body>
</html>

strutsupload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Struts2文件上传页面</title>
</head>
<body>
文件是: <s:property value="uploadFileName"></s:property><br/>
文件类型:<s:property value="uploadContentType"></s:property><br/>
</body>
</html>

 

错误问题

 

 

1.2多文件上传

Action


public class MoreFileUploadAction extends ActionSupport {
    private List<File> upload;
    private List<String> uploadContentType;
    private List<String> uploadFileName;
    private String savePath;

    @Override
    public String execute() throws Exception {
        byte[] buffer=new byte[1024];
        File file=new File(getSavePath());
        if(!file.exists()){
            file.mkdirs();
        }
        int index=0;
        for (File item:upload) {
            FileInputStream fis=new FileInputStream(item);
            FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName().get(index++));
            int length=fis.read(buffer);
            while (length>0){
                fos.write(buffer,0,length);
                length=fis.read(buffer);
            }
            fos.flush();
            fos.close();
            fis.close();
        }
        return SUCCESS;
    }



    public List<File> getUpload() {
        return upload;
    }

    public void setUpload(List<File> upload) {
        this.upload = upload;
    }

    public List<String> getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(List<String> uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public List<String> getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(List<String> uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
}

 

Struts.xml配置

<action name="moreuploadAction" class="cn.bdqn.file.MoreFileUploadAction">
    <param name="savePath">/upload</param>
    <result name="success">/moreupload/morestrutsupload.jsp</result>
</action>

 

morestrutsfileupload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Struts2文件上传</title>
</head>
<body>
<s:form action="moreuploadAction" enctype="multipart/form-data" method="post">
    <s:file name="upload" label="选择文件"/><br/>
    <s:file name="upload" label="选择文件"/><br/>
    <s:submit name="submit" value="文件上传"/>
</s:form>
</body>
</html>

 

morestrutsupload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Struts2多文件上传</title>
</head>
<body>
<s:iterator value="uploadFileName">
    文件:<s:property/> <br/>
</s:iterator>
<s:iterator value="uploadContentType">
    文件类型:<s:property/> <br/>
</s:iterator>
</body>
</html>

 

2. 文件下载

Action


public class FileDownAction extends ActionSupport {
    //文件路径
    
private String inputPath;
    //提交过来的name
    
private String fileName;
    private InputStream inputStream;
    //提交过来的file的MIME类型
    
private String contentType;

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



    public String getInputPath() {
        return inputPath;
    }

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

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    //返回inputstream,文件下载关键方法
    
public InputStream getInputStream() throws Exception {
        String path= ServletActionContext.getServletContext().getRealPath(inputPath);
        return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
}

 

Struts.xml配置

<action name="filedownAction" class="cn.bdqn.file.FileDownAction">
    <!-- 设置文件路径的参数,传到action类文件中去 -->
    
<param name="inputPath">/upload</param>
    <!-- 下载文件类型定义,即定义为“stream” -->
    
<result name="success" type="stream">

        <param name="contentType">application/octet-stream</param>
        <!-- 下载文件输出流定义 -->
        <!-- 这里的inputName元素所对应的value值downloadFile,在action中一定要有对应的getDownloadFile()方法 -->
        
<param name="inputName">inputStream</param>
        <!-- 下载文件处理方法 -->
        
<param name="contentDisposition">attachment;filename="${fileName}"</param><!--设置响应的HTTP头信息中的Content-Disposition参数的值-->
        <!-- 下载缓冲区的大小 -->
        
<param name="bufferSize">4096</param>
    </result>
</action>

 

strutsfiledown.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>Struts2文件下载</title>
</head>
<body>
<a href="filedownAction.action?fileName=1.jpg">文件下载</a></body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值