SpringMVC——文件上传与下载 ,rest解决.问题

一,文件上传

(1)导入包

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

(2)设置xml文件

 <!--配置上传组件-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

(3)handler

首先需要在 target/工程/下新建file文件夹

@Controller
@RequestMapping("/file")
public class FileHandler {
    @PostMapping("/upload")
    public String upload(MultipartFile picture, HttpServletRequest request) {
        if (picture.getSize() > 0) {
            // 获取保存的路径(绝对路径)
            String path = request.getSession().getServletContext().getRealPath("file");
            // 获取文件名
            String filename = picture.getOriginalFilename();
            File file = new File(path, filename);
            try {
                picture.transferTo(file);
                // 上传后的图片地址
                request.setAttribute("img", "/file/" + filename);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "upload";
    }

(4)jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/file/upload" enctype="multipart/form-data" method="post">
        <input type="file" name="picture">
        <input type="submit" value="上传">
    </form>
<img src="${img}">
</body>
</html>

二,文件下载

(1)handler

@GetMapping("/download/{name:.+}")
    public void download(@PathVariable("name") String name, HttpServletRequest request, HttpServletResponse response) {
        if (name != null) {
//            name = name + ".jpg";
            File file = new File(request.getSession().getServletContext().getRealPath("file"), name);
            if (file.exists()) {
                response.setContentType("application/forc-download");
                response.setHeader("Content-Disposition", "attachment;filename=" + name);
                try {
                    ServletOutputStream outputStream = response.getOutputStream();
                    outputStream.write(FileUtils.readFileToByteArray(file));
                    outputStream.flush();
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

(2)jsp

<body>
	<!--不加最后的/会报404-->
    <a href="/file/download/1.jpg/">1.jpg</a>
</body>

(3)解决rest无法识别.

spring会将rest中的点,识别为/分隔符,所以 .jpg等会出现识别错误的情况。可以这样写:{name:.+} 解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值