SpringBoot下载文件资源代码


前言

下载文件资源很简单,一开始学习Java web的时候,相信我们绝大多数人都学习过吧,所以这里直接上代码了。

下载成功,主要是给response放入一个outputstream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

@GetMapping("downUrlImage")
public void downUrlImage(String url, HttpServletResponse response){
//设置文件路径
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName="+UUIDGenerator.generate() +"jpg");// 设置文件名
try {
URL urlStr = new URL(url);
BufferedImage bufferedImage = ImageIO.read(urlStr);

ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
} catch (MalformedURLException e) {
log.info(e.getMessage());
} catch (IOException e) {
log.info(e.getMessage());
}catch (Exception e){
log.info(e.getMessage());
}

}

测试 :
http://localhost:8080/duodian/coupon/downUrlImage?url=http%3A%2F%2Flocalhost%3A8080%2Fduodian%2Fcoupon%2FdownUrlImage%3Furl%3Dhttp%253A%252F%252Fimg2.imgtn.bdimg.com%252Fit%252Fu%253D3588772980%252C2454248748%2526fm%253D27%2526gp%253D0.jpg





如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,添加博主微信哦, 请下方留言吧。可与博主自由讨论哦

支付包 微信 微信公众号
支付宝 微信 微信公众号
 
   

Author: jony
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source jony !
 Previous
SpringBoot_jar方式启动并配置日志文件 SpringBoot_jar方式启动并配置日志文件
前言正常启动 ,下面会选择application.properties 中配置默认的启动文件进行启动,下面这种情况不能根据实际情况进行启动项目 12java jar admin-1.0-SNAPSHOT.jar 1、测试环境和生产
2018-04-08
Next 
IntelliJ IDEA配置与使用(图文教程) IntelliJ IDEA配置与使用(图文教程)
前言:之前一直使用的MyEclipse,现在转战IntelliJ IDEA.一、IntelliJ配置1.代码提示与补充main,syso等快捷补全代码设置 2.设置自动导包 3.设置字体 4.设置快捷键注意先选择Eclipse,当修改对应C
2018-04-08
  TOC