SpringMVC 文件上传下载

Maven 导包

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.3</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

SpringMVC 配置

<!-- CommonsMultipartResolver的id值固定为 multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--设置解析请求的字符编码,若请求本身指定了字符编码,则覆盖此设置。默认是ISO-8859-1-->
    <property name="defaultEncoding" value="UTF-8"/>
    <!--设置上传文件时的临时目录,默认是Servlet容器的临时目录-->
    <property name="uploadTempDir" value="imgTemp"/>
    <!--设置在文件上传时允许写到内存中的最大值,以byte为单位。默认是10240-->
    <property name="maxInMemorySize" value="#{10*1024}"/>
    <!--设置允许上传的总的最大文件大小,以byte为单位。当设为-1时表示无限制,默认是-1-->
    <property name="maxUploadSize" value="#{10*1024}"/>
    <!--设置允许上传的单个文件最大值,以byte为单位。当设为-1时表示无限制,默认是-1-->
    <property name="maxUploadSizePerFile" value="#{30*1024}"/>
    <!--为true时,启用推迟文件解析,以便在UploadAction中捕获文件大小异常。默认是false-->
    <property name="resolveLazily" value="false"/>
</bean>

Controller

@RestController
@RequestMapping(value = "file")
public class FileController {

    /**
     * 单个文件上传
     * <p>
     * 这里的MultipartFile对象变量名跟表单中的file类型的input标签的name相同,
     * 所以框架会自动用MultipartFile对象来接收上传过来的文件,也可以使用@RequestParam("img")指定其对应的参数名称
     *
     * @param image
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "upload/file", method = RequestMethod.POST)
    public String uploadFile(MultipartFile image, HttpServletRequest request) throws IOException {
        // 如果没有文件上传,MultipartFile也不会为null,也可通过getSize()方法获取文件的字节大小判断是否有上传文件
        if (!image.isEmpty()) {
            String path = request.getSession().getServletContext().getRealPath("images");
            String fileName = image.getOriginalFilename();
            File file = new File(path, fileName);
            image.transferTo(file);
            return "true";
        }
        return "false";
    }

    /**
     * 多个文件上传
     *
     * @param images
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "upload/files", method = RequestMethod.POST)
    public void uploadFiles(MultipartFile[] images, HttpServletRequest request) throws IOException {
        for (MultipartFile image : images) {
            if (!image.isEmpty()) {
                String path = request.getSession().getServletContext().getRealPath("images");
                String fileName = image.getOriginalFilename();
                File file = new File(path, fileName);
                image.transferTo(file);
            }
        }
    }

    /**
     * 文件下载
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "download/file", method = RequestMethod.POST)
    public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String path = request.getSession().getServletContext().getRealPath("images");
        String fileName = request.getParameter("fileName");
        File file = new File(path + "/" + fileName);
        if (file.exists()) {
            response.setContentType("application/octet-stream");
            response.addHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1"));

            InputStream inputStream = new FileInputStream(file);
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] buffer = new byte[1024];
            while (inputStream.read(buffer) > 0) {
                outputStream.write(buffer);
            }
            outputStream.close();
            inputStream.close();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值