SpringMVC实现文件上传与下载

单文件上传:

pom.xml:


    commons-io
    commons-io
    1.3.2


    commons-fileupload
    commons-fileupload
    1.2.1


    jstl
    jstl
    1.2


    taglibs
    standard
    1.1.2

upload.jsp:

1.input的type设置为file。

2.form表单的method设置为post。(get请求只会将文件名传给后台)

3.form表单的enctype设置为multipart/form-data,以二进制的形式传输数据。

<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
" method="post" enctype="multipart/form-data">

上传的图片


FileController.java:

@RequestMapping(value="upload", method = RequestMethod.POST)
    public String upload(@RequestParam(value="img")MultipartFile img, HttpServletRequest request)
            throws Exception {
        //getSize()方法获取文件的大小来判断是否有上传文件
        if (img.getSize() > 0) {
            //获取保存上传文件的file文件夹绝对路径
            String path = request.getSession().getServletContext().getRealPath("file");
            //获取上传文件名
            String fileName = img.getOriginalFilename();
            File file = new File(path, fileName);
            img.transferTo(file);
            //保存上传之后的文件路径
            request.setAttribute("filePath", "file/"+fileName);
            System.out.println("file/"+fileName);
            return "upload";
        }
        return "error";
    }

springMVC.xml:



   
   
   
   
   
   



    

注意:这里需要在webapp文件下手动创建一个file文件夹。

多文件上传:

uploads.jsp:

<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

file1:
file2:
file3:

上传的图片


FileController.java:

@RequestMapping(value="/uploads", method = RequestMethod.POST)
    public String uploads(@RequestParam MultipartFile[] imgs, HttpServletRequest request)throws Exception {
        //创建集合,保存上传后的文件路径
        List filePaths = new ArrayList();
        for (MultipartFile img : imgs) {
            if (img.getSize() > 0) {
                String path = request.getSession().getServletContext().getRealPath("file");
                String fileName = img.getOriginalFilename();
                File file = new File(path, fileName);
                filePaths.add("file/"+fileName);
                img.transferTo(file);
            }
        }
        request.setAttribute("filePaths", filePaths);
        return "uploads";
}

文件下载:

download.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>



    
    Insert title here


下载图片

FileController.java:

@RequestMapping("/download")
    public void downloadFile(String fileName,HttpServletRequest request,
                             HttpServletResponse response){
        if(fileName!=null){
            //获取file绝对路径
            String realPath = request.getServletContext().getRealPath("file/");
            File file = new File(realPath,fileName);
            OutputStream out = null;
            if(file.exists()){
                //设置下载完毕不打开文件
                response.setContentType("application/force-download");
                //设置文件名
                response.setHeader("Content-Disposition", "attachment;filename="+fileName);
                try {
                    out = response.getOutputStream();
                    out.write(FileUtils.readFileToByteArray(file));
                    out.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally{
                    if(out != null){
                        try {
                            out.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

 

你可能感兴趣的:(spring)