• spring mvc文件上传下载


    转自:http://blog.csdn.net/huang_hws/article/details/6689111

    当然前提是已经设置好了spring mvc的配置。

    上传:

    前台:
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>hello</title> </head> <body> <div> <form enctype='multipart/form-data' method="post" action="upload.do"> <input type="file" name="uploadFile" /> <button>提交</button> </form> </div> </body> </html>

    调用后台的upload.do的请求

    spring mvc 只需在请求前注解@RequstMapping("your request name")便可对应

    后台
    package hope.cs.zhku.controller;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.multipart.MultipartHttpServletRequest;

    /******************************************************************************
     * 名称:UserBasicEditorController.java</br>
     * 日期:2011-8-15</br>
     * 功能:</br>
     * 编写:Willson Huang</br>
     * 复核:</br>
     * 其他:</br>
     * 历史:(说明,修改人,时间)</br>
     * 1.create ,Willson Huang ,2011-8-15
     *****************************************************************************/
    @Controller
    public class UploadController {

        @RequestMapping("upload.do")
        public String upload(HttpServletRequest request,HttpServletResponse response ){
            MultipartHttpServletRequest multipartHttpservletRequest=(MultipartHttpServletRequest) request;
            MultipartFile multipartFile = multipartHttpservletRequest.getFile("uploadFile");
            String originalFileName=multipartFile.getOriginalFilename();
            File file=new File("file");
            if(!file.exists()){
                file.mkdir();
            }
            System.out.println(file.getAbsolutePath());
            try {
                FileOutputStream fileOutputStream=new FileOutputStream(file+"/uploadFile"+originalFileName.substring(originalFileName.lastIndexOf('.'),
    originalFileName.length()));
                fileOutputStream.write(multipartFile.getBytes());
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return "error";
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return "error";
            }
            return "uploadSuccess";
        }
    }
    此时,配置文件如前一遍文章的配置所示,此处不再列出,控制器中有一句输出语句,输出文件储存的位置,可自行更改

    下载:

    前台:
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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">
        <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
    <title>Insert title here</title>
    </head>
    <body>
        <input id='fileName' type="text" name="fileName"/>
        <a href="download.do" target="blank"><button>下载</button></a>
    </body>
    <script type="text/javascript">
        $(function(){
            $('a').click(function(){
                var link=this.href+'?'+'fileName='+$('#fileName').val();
                window.open(link);
                return false;
            });
        });
    </script>
    </html>
    后台:
    前台填写要下载的文件,后台从文件夹里查找,如果没有文件则返回错误文件,否则则提供任意文件类型的下载(填写文件时必须写后缀)
    package hope.cs.zhku.controller;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;

    import javax.servlet.http.HttpServletResponse;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    /******************************************************************************
     * 名称:UserBasicEditorController.java</br>
     * 日期:2011-8-15</br>
     * 功能:</br>
     * 编写:Willson Huang</br>
     * 复核:</br>
     * 其他:</br>
     * 历史:(说明,修改人,时间)</br>
     * 1.create ,Willson Huang ,2011-8-15
     *****************************************************************************/
    @Controller
    public class DownloadController {

        @RequestMapping("download.do")
        public void downloadFile(String fileName,HttpServletResponse response){
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");

            response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
            try {
                File file=new File(fileName);
                System.out.println(file.getAbsolutePath());
                InputStream inputStream=new FileInputStream("file/"+file);
                OutputStream os=response.getOutputStream();
                byte[] b=new byte[1024];
                int length;
                while((length=inputStream.read(b))>0){
                    os.write(b,0,length);
                }
                inputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    java里如何使用输入流和输出流实现读取本地文件里内容和写出到本地文件里
    Windows 命令行基础(博主推荐)
    Python2.7编程基础(博主推荐)
    27 个Jupyter Notebook的小提示与技巧
    java里如何实现循环打印出字符或字符数组里的内容
    [转]angularjs之ui-grid 使用详解
    [转]AngularJS 实现 Table的一些操作(示例大于实际)
    [转]js 回车转成TAB(利用tabindex)
    [转] Entity Framework添加记录时获取自增ID值
    [转]使用依赖关系注入在 ASP.NET Core 中编写干净代码
  • 原文地址:https://www.cnblogs.com/tv151579/p/2878374.html
Copyright © 2020-2023  润新知