Java Springboot 根据图片链接生成图片下载链接 及 多个图片打包zip下载链接

现有一些图片在服务器上的链接,在浏览器中打开这些链接是直接显示在浏览器页面的形式。

现在需要生成这些图片的单独下载以及打包下载链接,即在浏览器中打开下载链接后弹出下载框提示下载。由于前端存在跨域问题,所以图片下载由后台接口完成。

 

单张图片下载

首先编写文件下载工具类:

 1 import java.net.URL;
 2 import java.net.MalformedURLException;
 3 import org.apache.commons.io.FileUtils;
 4 
 5 public class FileDownloadUtil {
 6 /**
 7      * 下载文件---返回下载后的文件存储路径
 8      *
 9      * @param url 文件路径
10      * @param dir 目标存储目录
11      * @param fileName 存储文件名
12      * @return
13      */
14     public static void downloadHttpUrl(String url, String dir, String fileName) throws BusinessException {
15         try {
16         URL httpurl = new URL(url);
17         File dirfile = new File(dir);
18             if (!dirfile.exists()) {
19                 dirfile.mkdirs();
20             }
21             FileUtils.copyURLToFile(httpurl, new File(dir+fileName));
22         } catch (MalformedURLException e) {
23             e.printStackTrace();
24         } catch (IOException e) {
25             e.printStackTrace();26         }
27     }
28 }

 

Controller层接口:

 1 import org.apache.commons.lang.StringUtils;
 2 import java.io.*;
 3 
 4 
 5 protected HttpServletResponse response;
 6 
 7 /**
 8      * 单张图片下载
 9      *
10      * @param url 要下载的图片url
11      * @author: nemowang
12      */
13     @ApiImplicitParams({
14             @ApiImplicitParam(name = "url", value = "图片url", required = true, dataType = "String", paramType = "query"),
15     })
16     @ApiOperation(value = "单张图片下载", notes = "单张图片下载")
17     @RequestMapping(value = "/downloadPicture", method = RequestMethod.GET)
18     public void downloadPicture(String url) {
19         
20         // 拼接完整图片路径。这里填写图片链接
21         String urlPath = "";
22 
23         // 获取图片文件后缀名
24         String postfix = "." + StringUtils.substringAfterLast(url, ".");
25 
26         // 获取当前类的所在项目路径
27         File directory = new File("");
28         String courseFile;
29 
30         String srcPath;
31         File srcFile = null;
32         FileInputStream fileInputStream = null;
33         InputStream fis = null;
34         OutputStream out = null;
35         try {
36             courseFile = directory.getCanonicalPath();
37             String fileName = "\\" + StringUtil.getUUID() + postfix;
38             // 下载文件
39             FileDownloadUtil.downloadHttpUrl(urlPath, courseFile, fileName);
40 
41             srcPath = courseFile + fileName;
42             srcFile = new File(srcPath);
43 
44             fileInputStream = new FileInputStream(srcPath);
45             fis = new BufferedInputStream(fileInputStream);
46             byte[] buffer = new byte[fis.available()];
47             fis.read(buffer);
48 
49             response.setContentType("application/octet-stream");
50             response.setHeader("Content-disposition", "attachment;filename=" + fileName);
51             out = response.getOutputStream();
52             out.write(buffer);
53             out.flush();
54             out.close();
55         } catch (Exception e) {
56             e.printStackTrace();
57         } finally {
58             try {
59                 if (fileInputStream != null) {
60                     fileInputStream.close();
61                 }
62                 if (fis != null) {
63                     fis.close();
64                 }
65                 if (out != null) {
66                     out.close();
67                 }
68             } catch (IOException e) {
69                 e.printStackTrace();
70             }
71         }
72 
73         // 删除中间文件
74         if (srcFile != null) {
75             System.out.println(WordExportUtil.deleteFile(srcFile));
76         }
77     }

至此单张图片下载接口结束。

因为是GET请求,所以直接拼接接口路由+参数,用浏览器打开就能弹出下载。

 

转载于:https://www.cnblogs.com/nemowang1996/p/11603848.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值