案例一

    @ApiOperation(value = "根据id 批量下载文件", notes = "根据id 批量下载文件")
@RequestMapping(value = "/downloadFiles/{id}", method = RequestMethod.GET)
@ApiImplicitParam(name = "sheetId", value = "工单ID", required = true, dataType = "String", paramType = "path")
public void downloadFiles(HttpServletRequest request, HttpServletResponse response, @PathVariable String id) throws Exception {
List<OssMediaFile> list = null;
try {
EntityWrapper<OssMediaFile> wrapper = new EntityWrapper<>();
wrapper.eq("id", id);
wrapper.eq("is_delete", 0);
list = this.baseService.selectList(wrapper);
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String header = request.getHeader("User-Agent").toUpperCase();
String fileName = "附件" + String.valueOf(System.currentTimeMillis()) + ".zip";
if (!header.contains("MSIE") && !header.contains("TRIDENT") && !header.contains("EDGE")) {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} else {
fileName = URLEncoder.encode(fileName, "utf-8");
fileName = fileName.replace("+", "%20");
}
response.setContentType("multipart/form-data");
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
try(ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
for (int i = 0; i < list.size(); i++) {
OssMediaFile mediaFile = list.get(i);
logger.info("ossMediaFile参数:{}", JSONObject.toJSONString(mediaFile));
InputStream is = null;
try {
zipOut.putNextEntry(new ZipEntry(mediaFile.getFileName()));
String urlName = mediaFile.getFileUrl();
URL url = new URL(urlName);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置是否向HttpURLConnection输出
conn.setDoOutput(false);
// 设置是否从httpUrlConnection读入
conn.setDoInput(true);
/** 设置连接方式:GET */
conn.setRequestMethod("GET");
/** 设置连接主机服务器超时时间:5000毫秒 */
conn.setConnectTimeout(5000);
/** 发送GET方式请求,使用connet方法建立和远程资源之间的实际连接即可 */
conn.connect();
int status = conn.getResponseCode();
logger.info("---status:" + status);
if(status!= HttpURLConnection.HTTP_OK){
is = conn.getErrorStream();
}else{
is = conn.getInputStream();
}
if (null == is) {
continue;
}
int temp = 0;
while ((temp = is.read()) != -1) {
zipOut.write(temp);
}
is.close();
/** 关闭远程连接 */
conn.disconnect();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
zipOut.closeEntry();
logger.info("一键下载结束");
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RuntimeException("附件下载失败");
}
}

最新文章

  1. 跟着老男孩教育学Python开发【第五篇】:模块
  2. 一切Web的基础----HTTP
  3. cocostudio 骨骼动画 setContentScaleFactor
  4. .net开发笔记(十六) 对前部分文章的一些补充和总结
  5. svn(http)
  6. 明天去FDUSC报道了,GOD BLESS ALL OF US
  7. Sql Server来龙去脉系列之二 框架和配置
  8. (VS TFS) Adding existing project to solution in TFS.
  9. iOS NSString的常用用法
  10. 自定义更新Hibernate Ehcache
  11. DRAM 内存介绍(一)
  12. weekend110(Hadoop)的 第一天笔记
  13. [Regular Expressions] Find a String that Precedes Another String ?= , ?!
  14. [C语言]贪吃蛇_结构数组实现
  15. Kubernetes 架构(上)- 每天5分钟玩转 Docker 容器技术(120)
  16. css3的transform,translate和transition之间的区别与作用
  17. 存储过程打印超过8000的VARCHAR字符的问题
  18. IEEE1588 verision 2 报文介绍
  19. 动态向SqlParameter 里添加相应参数
  20. LeetCode之数组处理题java

热门文章

  1. Curl 命令举例
  2. [ZZH]第一篇博客
  3. java-javaSE-异常机制
  4. GoLang之ACM控制台输入输出
  5. MySQL---MGR保姆版
  6. centos/rockylinux/proxmoxve重置root密码 以及 在#bash 下 重启
  7. jmeter安装配置
  8. watch监听中的deep以及immdiate
  9. 小程序调用OCR识别操作的过程,特此记录。
  10. macos下编译glfw