Struts2文件的上传的与下载

首先要引入:commons-fileupload-xxx.jar和commons-io-xxx.jar两个jar包

1.前台index.jsp页面:

<html>
  <head>
    <title>文件上传与下载</title>
    <script type="text/javascript" src="/js/jquery-1.3.js"></script>
  </head>

  <body>
    <form action="upload.action" method="post" enctype="multipart/form-data">
        <input type="file" name="uploadFile.upload"/>
        <input type="submit" value="提交"/>
    </form><br>

    <a href="download.action?fileName=${message}">下载${message}</a>

  </body>
</html>

2.struts.xml配置文件:

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

    <package name="FileUpload" extends="json-default">
        <!-- 文件上传 -->
        <action name="upload" class="com.fileupload.UploadAction">
            <result name="success">/index.jsp</result>
        </action>

        <!-- 文件下载 -->
        <action name="download" class="com.fileupload.DownLoadAction">
            <result name="succ" type="stream">
                <param name="contentType">application/octet-stream;charset=ISO8859-1</param>
                <param name="inputName">downloadFile</param>
                <param name="contentDisposition">attachment;filename=${fileName}</param>
                <param name="bufferSize">8192</param>
            </result>
        </action>
    </package>
</struts> 

3.上传的action:

public class UploadAction extends ActionSupport {

    private UploadFile uploadFile;
    private FileUploadUtil fileUploadUtil =new FileUploadUtil();

    public String execute(){
        Map session=ActionContext.getContext().getSession();
        System.out.println("文件:"+uploadFile.getFile());
        System.out.println("文件名称:"+uploadFile.getFileName());
        System.out.println("相应类型:"+uploadFile.getContentType());
        try {
//          String destPath = ServletActionContext.getServletContext().getRealPath("/uploads");    
//          File dest = new File(destPath, uploadFile.getFileName()); //服务器的文件    
//          FileUtils.copyFile(uploadFile.getFile(), dest);//完成了文件的拷贝工作 
            String fileName=fileUploadUtil.uploadFile(uploadFile);
            session.put("message",fileName);
        } catch (Exception e) {
            session.put("message","上传失败!");
            e.printStackTrace();
        }
        return "success";

    }

    public UploadFile getUploadFile() {
        return uploadFile;
    }

    public void setUploadFile(UploadFile uploadFile) {
        this.uploadFile = uploadFile;
    }   

}

4.下载的action:

public class DownLoadAction extends ActionSupport{

    private String basePath=ServletActionContext.getServletContext().getRealPath("/uploads/");
    private String fileName;

    public String execute(){
        return "succ";
    }

    public InputStream getDownloadFile(){
        return ServletActionContext.getServletContext().getResourceAsStream("/uploads/"+fileName);
    }

    public String getFileName() throws UnsupportedEncodingException{
        return new String(fileName.getBytes(),"ISO-8859-1");
    }

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


}

5.上传文件的工具类FileUploadUtil:

public class FileUploadUtil {

    private String filePath;

    public void setFilePath(String filePath){
        this.filePath=filePath;
    }

    //1.通过文件获取扩展名
    private String getFileExt(String fileName){
        return FilenameUtils.getExtension(fileName);
    }
    //2.生成UUID随机数,作为新的文件名
    private String newFileName(String fileName){
        String newName=UUID.randomUUID().toString() + "." +getFileExt(fileName);
        System.out.println("文件新名称:"+newName);
        return newName;
    }
    //3.实现文件上传的功能,返回上传后新的文件名称
    public String uploadFile(UploadFile uploadFile){
        //获取新的文件名
        String newFileName=newFileName(uploadFile.getFileName());
        try {
            this.setFilePath(ServletActionContext.getServletContext().getRealPath("/uploads"));
            System.out.println("文件上传的路径:"+filePath);
            FileUtils.copyFile(uploadFile.getFile(), new File(filePath, newFileName));
            return newFileName;
        } catch (Exception e) {
            throw new RuntimeException();
        } finally{
            uploadFile.getFile().delete();
        }
    }
}

6.文件的实体类UploadFile:

public class UploadFile {

    private File file;

    private String contentType;

    private String fileName;

    public File getFile() {
        return file;
    }

    public String getContentType() {
        return contentType;
    }

    public String getFileName() {
        return fileName;
    }


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

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

    public void setUploadFileName(String fileName) {
        this.fileName = fileName;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值