Spring MVC 入门指南(十一):文件上传和下载

使用对象接收上传文件

上一章我们通过案例演示了Spring MVC上传文件,接下来,我们演示使用对象接收上传文件。 
在实际项目的开发中,很多时候上传的文件会作为对象的属性被保存。SpringMVC的处理也非常的简单。


下面我们在views文件夹创建registerForm.jsp文件,演示接收文件上传:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
    <title>Insert title here</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
</head>
<body>
    <h2>用户注册</h2>
    <form action="file/register" enctype="multipart/form-data" method="post">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="username"></td>
            </tr>
            <tr>
                <td>请上传头像:</td>
                <td><input type="file" name="image"></td>
            </tr>
            <tr>
                <td><input type="submit" value="注册"></td>
            </tr>
        </table>
    </form>
</body>
</html>

接着, 我们创建一个名叫“com.ray.entity”包,然后再创建一个User类,必须要实现序列化接口,如下案例代码:

package com.ray.entity;

import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;

/**
 * @author Ray
 * @date 2018/4/22 0022
 * 实现序列化接口
 */
public class User implements Serializable {

    private String username;
    private MultipartFile image;

    public User() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public MultipartFile getImage() {
        return image;
    }

    public void setImage(MultipartFile image) {
        this.image = image;
    }
}

我们在之前创建的FileController类继续写用于接收文件的上传和下载功能。

以下是负责接收文件的表单功能代码----上传文件():

说明: 上传文件路径

    1.String path = servletContext.getRealPath("/upload") + "/"; //项目路径/upload文件夹下

    2.String path = "F:\\test\\"; //本地路径/test文件夹下

/**
 * @author Ray
 * @date 2018/4/21 0021
 */
@Controller
@RequestMapping("/file")
public class FileController {

    /**
     * 上传文件(一)
     * @return
     */
    @RequestMapping("/toFile")
    public String toFileUpload(){
        return "fileUpload";
    }

    /**
     * 上传文件(二)
     * @return
     */
    @RequestMapping("/toFile2")
    public String toFileUpload2(){
        return "registerForm";
    }

    /**
     * 上传文件(一)
     * 普通上传
     */
    @RequestMapping("/onefile")
    public String oneFileUpload(@RequestParam("file")CommonsMultipartFile file,
                                HttpServletRequest request, Model model){
        //获取原始文件名
        String fileName = file.getOriginalFilename();
        System.out.println("原始文件名:" + fileName);

        //新文件名
        String newFileName = UUID.randomUUID() + fileName;

        //获取项目路径
//        ServletContext servletContext = request.getSession().getServletContext();

        //上传位置
//        String path = servletContext.getRealPath("/upload") + "/"; //项目路径/upload文件夹
        String path = "F:\\test\\"; //本地路径/test


        File f = new File(path);
        if(!f.exists()){
            f.mkdirs();
        }
        if(!file.isEmpty()){
            try{
                FileOutputStream fileOutputStream = new FileOutputStream(path + newFileName);
                InputStream inputStream = file.getInputStream();
                int b = 0;
                while ((b = inputStream.read()) != -1){
                    fileOutputStream.write(b);
                }
                fileOutputStream.close();
                inputStream.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        //控制台输出
        System.out.println("上传图片到:" + path + newFileName);
        //保存文件地址,用于JSP页面回显
        model.addAttribute("fileUrl",newFileName);
        return "fileUpload";
    }

    /**
     * 上传文件(二)
     * 使用对象
     */
    @RequestMapping(value = "/register")
    public String register(HttpServletRequest request, @ModelAttribute User user, Model model) throws IOException {
        System.out.println(user.getUsername());
        //如果文件不为空,写入上传路径
        if(!user.getImage().isEmpty()){
            //上传文件路径
//            String path = request.getSession().getServletContext().getRealPath("/upload/");
            String path = "F:\\test\\"; //本地路径/test
            //上传文件名
            String filename = user.getImage().getOriginalFilename();
            File filepath = new File(path, filename);
            //判断路径是否存在,不存在则创建
            if(!filepath.getParentFile().exists()){
                filepath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件当中
            user.getImage().transferTo(new File(path + File.separator + filename));
            //将用户添加到model
            model.addAttribute("user",user);
            return "userInfo";
        }else {
            return "error";
        }
    }
}

在views文件夹下创建userInfo.jsp文件,该页面主要是文件的下载页面,如下jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">
    <title>Insert title here</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
</head>
<body>
    <h3>文件下载</h3>
    <a href="file/download?filename=${requestScope.user.image.originalFilename}">${requestScope.user.image.originalFilename}</a>
</body>
</html>

在浏览器中输入如下URL来测试应用: 

http://localhost:8080/SpringMVC/file/toFile2

输入用户名并上传刚才上传的文件: 


单击“注册”按钮上传文件,然后就会跳转到下载页面。如下图: 




文件下载

上面我们通过案例演示了使用对象接收上传文件,接下来,我们演示Spring MVC的下载文件。 
文件下载比较简单,直接在页面给出了一个超链接,该链接href的属性等于要下载文件的文件名,就可以实现文件下载了。但是如果该文件的文件名为中文文件名,在某些早起的浏览器上就会导致下载失败;如果使用最新的Firefox、Chrome、Opera、Safari则都可以正常下载文件名为中文的文件了。 
SpringMVC提供了一个ResponseEntity类型,使用它可以很方便地定义返回的HttpHeaders和HttpStatus。以下代码演示文件的下载功能:

package com.ray.controllers;

import com.ray.entity.User;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author Ray
 * @date 2018/4/21 0021
 */
@Controller
@RequestMapping("/file")
public class FileController {

    /**
     * 上传文件(一)
     * @return
     */
    @RequestMapping("/toFile")
    public String toFileUpload(){
        return "fileUpload";
    }

    /**
     * 上传文件(二)
     * @return
     */
    @RequestMapping("/toFile2")
    public String toFileUpload2(){
        return "registerForm";
    }

    /**
     * 上传文件(一)
     * 普通上传
     */
    @RequestMapping("/onefile")
    public String oneFileUpload(@RequestParam("file")CommonsMultipartFile file,
                                HttpServletRequest request, Model model){
        //获取原始文件名
        String fileName = file.getOriginalFilename();
        System.out.println("原始文件名:" + fileName);

        //新文件名
        String newFileName = UUID.randomUUID() + fileName;

        //获取项目路径
//        ServletContext servletContext = request.getSession().getServletContext();

        //上传位置
//        String path = servletContext.getRealPath("/upload") + "/"; //项目路径/upload文件夹
        String path = "F:\\test\\"; //本地路径/test


        File f = new File(path);
        if(!f.exists()){
            f.mkdirs();
        }
        if(!file.isEmpty()){
            try{
                FileOutputStream fileOutputStream = new FileOutputStream(path + newFileName);
                InputStream inputStream = file.getInputStream();
                int b = 0;
                while ((b = inputStream.read()) != -1){
                    fileOutputStream.write(b);
                }
                fileOutputStream.close();
                inputStream.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        //控制台输出
        System.out.println("上传图片到:" + path + newFileName);
        //保存文件地址,用于JSP页面回显
        model.addAttribute("fileUrl",newFileName);
        return "fileUpload";
    }

    /**
     * 上传文件(二)
     * 使用对象
     */
    @RequestMapping(value = "/register")
    public String register(HttpServletRequest request, @ModelAttribute User user, Model model) throws IOException {
        System.out.println(user.getUsername());
        //如果文件不为空,写入上传路径
        if(!user.getImage().isEmpty()){
            //上传文件路径
//            String path = request.getSession().getServletContext().getRealPath("/upload/");
            String path = "F:\\test\\"; //本地路径/test
            //上传文件名
            String filename = user.getImage().getOriginalFilename();
            File filepath = new File(path, filename);
            //判断路径是否存在,不存在则创建
            if(!filepath.getParentFile().exists()){
                filepath.getParentFile().mkdirs();
            }
            //将上传文件保存到一个目标文件当中
            user.getImage().transferTo(new File(path + File.separator + filename));
            //将用户添加到model
            model.addAttribute("user",user);
            return "userInfo";
        }else {
            return "error";
        }
    }


    /**
     * 文件下载
     */
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(HttpServletRequest request,
                                           @RequestParam("filename") String filename,
                                           Model model) throws IOException {
        //文件所在路径
//        String path = request.getSession().getServletContext().getRealPath("/upload/");
        String path = "F:\\test\\"; //本地路径/test
        File file = new File(path + File.separator + filename);
        HttpHeaders headers = new HttpHeaders();
        //下载显示的文件名,解决中文名称乱码问题
        String downloadFileName = new String(filename.getBytes("UTF-8"),"iso-8859-1");
        //通知浏览器以attachment(下载方式)打开图片
        headers.setContentDispositionFormData("attachment", downloadFileName);
        //application_octet_stream : 二进制流数据(最常见的文件下载)
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
    }
}
download处理方法接收页面传递的文件名filename后,使用Apache Commons FileUpload组件的FileUtils读取项目的上传文件,并将其构建成ResponseEntity对象返回客户端下载。 
使用ResponseEntity对象,可以很方便的定义返回的HttpHeaders和HttpStatus。上面代码中的MediaType,代表的是Internet Media Type,即互联网媒体类型,也叫做MIME类型。在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。HttpStatus类型代表的是Http协议中的状态。有关MediaType和HttpStatus类可以参考Spring MVC的API文档。 

点击下载页面的超链接,显示文件正在下载,如下图所示: 



彩蛋


猜你喜欢

转载自blog.csdn.net/q343509740/article/details/80036735
今日推荐