码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-09-28 18:24:17      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:apach   bsp   code   ted   value   size   cep   ati   static   

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

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

 

单张图片下载

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

 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请求,所以直接拼接接口路由+参数,用浏览器打开就能弹出下载。

 

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

标签:apach   bsp   code   ted   value   size   cep   ati   static   

原文地址:https://www.cnblogs.com/nemowang1996/p/11603848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!