Spring Web MVC框架(五) 文件上传

32 篇文章 0 订阅

Spring同样支持文件上传功能,不过该功能默认未开启,因为可能有些开发者可能希望自己处理文件上传过程。Spring的文件上传功能在org.springframework.web.multipart包下,有两个MultipartResolver实现用来支持文件上传功能,一个是基于Commons FileUpload ,另一个基于Servlet 3.0 multipart请求解析功能。这两个MultipartResolver差不多,一个需要添加Commons FileUpload的依赖,另一个需要在Servlet 3.0容器上运行。大家可以根据需要选择。

定义MultipartResolver

使用Commons FileUpload MultipartResolver

在配置文件中添加如下一段,我们可以在Bean定义中配置上传文件大小等属性。

<bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>

</bean>

使用Servlet 3.0 MultipartResolver

由于使用的是Servlet API提供的文件上传功能,所以文件大小等配置需要在web.xml中进行配置。我们需要在dispathcer-servlet中添加<multipart-config>标签,它有四个子标签来设置文件上传的属性。

这四个属性如下:

  • location ,临时文件的存放位置,这个路径必须是绝对路径。
  • fileSizeThreshold,文件起始值,大于该值文件才会被临时保存,单位是字节。
  • MaxFileSize,单个文件的最大值,单位是字节,不管上传几个文件,只要有一个文件大小超过该值就会抛出IllegalStateException
  • maxRequestSize,文件上传请求的最大值,单位是字节,主要作用是当上传多个文件是配置整个请求的大小,当超出该值是抛出IllegalStateException
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
    <multipart-config>
        <max-file-size>100000</max-file-size>
    </multipart-config>
</servlet>

然后我们在Spring配置文件中添加Servlet 3.0 MultipartResolver。

<bean id="multipartResolver"
        class="org.springframework.web.multipart.support.StandardServletMultipartResolver">
</bean>

获取文件

配置好了Multipart解析器之后,我们就可以接收文件了。首先定义一个页面fileupload.jsp,用于上传文件并显示服务器中的文件。注意在表单中我们必须添加enctype="multipart/form-data"才能正确的上传文件。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传</title>
    <meta charset="utf-8">

</head>
<body>
<h2>文件上传</h2>
<form action="<c:url value="/fileupload"/>"
      method="post" enctype="multipart/form-data">
    <label for="file">文件</label>
    <input type="file" name="file" id="file"/>
    <br>
    <input type="submit" value="提交">
</form>

<h2>文件下载</h2>

<c:forEach var="file" items="${files}">
    <a href="<c:url value="/findFile?filename=${file}"/>">${file}</a>
    <br>
</c:forEach>

</body>
</html>

然后就可以在控制器中获取文件了。由于MultipartFile和它对应的临时文件会在方法结束之后被Spring清除,所以我们必须在方法中将文件保存到合适的地方。这里我定义了一个UserFile类将文件保存到Session中。

public class UserFile {
    private String filename;
    private byte[] bytes;
}

然后就是控制器了。在请求方法中,我们可以像普通参数那样获取上传的文件,只不过文件对应的类型是MultipartFile,如果使用的是Servlet 3.0标准的,那么类型还可以是javax.servlet.http.Part。我写了两个处理方法,第一个将MultipartFile转化为上面的类型,然后保存到Session中。第二个方法用于获取Session中的文件。

@Controller
public class FileUploadController {
    @RequestMapping("/fileupload")
    public String fileUpload(HttpSession session, @RequestParam(required = false) MultipartFile file, Model model) throws IOException {
        List<UserFile> files = (List<UserFile>) session.getAttribute("files");
        if (files == null)
            files = new ArrayList<>();
        if (file != null) {
            UserFile f = new UserFile();
            f.setFilename(file.getOriginalFilename());
            f.setBytes(file.getBytes());
            files.add(f);
        }
        session.setAttribute("files", files);
        List<String> filenames = files.stream()
                .map(UserFile::getFilename)
                .collect(Collectors.toList());
        model.addAttribute("files", filenames);

        return "fileupload";
    }

    @RequestMapping("/findFile")
    public void findFile(HttpSession session, @RequestParam String filename, HttpServletResponse response) throws IOException {
        List<UserFile> files = (List<UserFile>) session.getAttribute("files");
        Optional<UserFile> file = files.stream()
                .filter(o -> Objects.equals(o.getFilename(), filename))
                .findFirst();
        OutputStream out = response.getOutputStream();
        out.write(file.get().getBytes());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值