ImageManagerController.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. package com.huimv.manager.controller;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.huimv.manager.dao.entity.ImagePathEntity;
  5. import com.huimv.manager.service.IImageService;
  6. import com.huimv.manager.utils.DateUtil;
  7. import com.huimv.manager.utils.ImageByteUtil;
  8. //import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
  9. import com.huimv.manager.utils.Result;
  10. import com.huimv.manager.utils.ResultCode;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.compress.archivers.ArchiveException;
  13. import org.apache.commons.compress.archivers.ArchiveOutputStream;
  14. import org.apache.commons.compress.archivers.ArchiveStreamFactory;
  15. import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
  16. import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
  17. //import org.apache.tomcat.util.http.fileupload.FileUtils;
  18. import org.apache.commons.io.FileUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.http.MediaType;
  21. import org.springframework.web.bind.annotation.*;
  22. import javax.imageio.ImageIO;
  23. import javax.servlet.ServletOutputStream;
  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import java.awt.image.BufferedImage;
  27. import java.io.*;
  28. //import java.sql.Timestamp;
  29. import java.util.Date;
  30. import java.util.List;
  31. /**
  32. * @Project : huimv.shiwan
  33. * @Package : com.huimv.biosafety.uface.controller
  34. * @Description : TODO
  35. * @Version : 1.0
  36. * @Author : ZhuoNing
  37. * @Create : 2020-12-25
  38. **/
  39. @Slf4j
  40. @CrossOrigin
  41. @RestController
  42. @RequestMapping("/imageManager")
  43. public class ImageManagerController {
  44. @Autowired
  45. private IImageService imageService;
  46. @Autowired
  47. private DateUtil dateUtil;
  48. protected HttpServletResponse response;
  49. /**
  50. * @Method : getLastImageAiResult
  51. * @Description : 读取最新的图像AI识别记录
  52. * @Params : []
  53. * @Return : java.lang.String
  54. * @Author : ZhuoNing
  55. * @Date : 2021/1/22
  56. * @Time : 10:00
  57. */
  58. @RequestMapping(value = "/getLastImageAiResult", method = RequestMethod.GET)
  59. public String getLastImageAiResult() {
  60. return JSONObject.toJSON(imageService.getLastImage()).toString();
  61. }
  62. /**
  63. * @Method : getNextImageAiResult
  64. * @Description : 读取下一条图像AI识别记录
  65. * @Params : []
  66. * @Return : java.lang.String
  67. * @Author : ZhuoNing
  68. * @Date : 2021/1/22
  69. * @Time : 10:01
  70. */
  71. @RequestMapping(value = "/getNextImageAiResult", method = RequestMethod.GET)
  72. public String getNextImageAiResult() {
  73. //
  74. return JSONObject.toJSON(imageService.getNextImage()).toString();
  75. }
  76. /**
  77. * @Method : listImage
  78. * @Description :
  79. * @Params : [startDate, endDate, pageNo, pageSize]
  80. * @Return : com.alibaba.fastjson.JSONArray
  81. * @Author : ZhuoNing
  82. * @Date : 2021/1/22
  83. * @Time : 16:28
  84. */
  85. @Deprecated
  86. @RequestMapping(value = "/listImage", method = RequestMethod.GET)
  87. public JSONObject listImage(@RequestParam(value = "startDate", required = true) String startDate, @RequestParam(value = "endDate", required = true) String endDate, @RequestParam(value = "catalog", required = false) String catalog, @RequestParam(value = "pageNo", required = true) int pageNo, @RequestParam(value = "pageSize", required = true) int pageSize) {
  88. log.info("startDate>>" + startDate);
  89. log.info("endDate>>" + endDate);
  90. log.info("catalog>>"+catalog);
  91. log.info("pageNo>>" + pageNo);
  92. log.info("pageSize>>" + pageSize);
  93. // 搜索记录时,catalog是非必要字段;
  94. // 导出图片时,catalog是必要字段;
  95. //
  96. List<ImagePathEntity> imageList = imageService.getImageList(startDate, endDate, pageNo, pageSize,catalog);
  97. JSONArray imageJa = new JSONArray();
  98. for (ImagePathEntity imageEntity : imageList) {
  99. // JSONObject dataJo = (JSONObject) JSONObject.toJSON(imageEntity);
  100. JSONObject imageJo = new JSONObject();
  101. imageJo.put("aiData", imageEntity.getAiData());
  102. imageJo.put("aiResult", imageEntity.getAiResult());
  103. imageJo.put("aiTimeUsed", imageEntity.getAiTimeUsed());
  104. imageJo.put("createDate", dateUtil.formatTimestamp(imageEntity.getCreateDate()));
  105. imageJo.put("effective", imageEntity.getEffective());
  106. imageJo.put("fileName", imageEntity.getFileName());
  107. imageJo.put("id", imageEntity.getId());
  108. imageJo.put("imgPath", imageEntity.getImgPath());
  109. imageJo.put("name", imageEntity.getName());
  110. imageJo.put("relativePath", imageEntity.getRelativePath());
  111. imageJo.put("pigQuantity", imageEntity.getPigQuantity());
  112. imageJa.add(imageJo);
  113. }
  114. // 读取记录数
  115. int total = imageService.getTotal(startDate, endDate);
  116. JSONObject outJo = new JSONObject();
  117. outJo.put("data", imageJa);
  118. outJo.put("total", total);
  119. return outJo;
  120. }
  121. /**
  122. * @Method : previousImage
  123. * @Description : 读取前一张
  124. * @Params : [id]
  125. * @Return : com.alibaba.fastjson.JSONObject
  126. * @Author : ZhuoNing
  127. * @Date : 2021/1/23
  128. * @Time : 19:09
  129. */
  130. @RequestMapping(value = "/getPreviousImage", method = RequestMethod.GET)
  131. public Result getPreviousImage(@RequestParam(value = "imageId", required = true) int imageId) {
  132. //
  133. ImagePathEntity imagePathEntity = imageService.getOneImage("previous", imageId);
  134. if(imagePathEntity == null){
  135. return new Result(10001,"未查询到任何记录.",false);
  136. }else{
  137. return new Result(ResultCode.SUCCESS,(JSONObject)JSONObject.toJSON(imagePathEntity));
  138. }
  139. }
  140. /**
  141. * @Method : nextImage
  142. * @Description : 读取下一张图片
  143. * @Params : [id]
  144. * @Return : com.alibaba.fastjson.JSONObject
  145. * @Author : ZhuoNing
  146. * @Date : 2021/1/23
  147. * @Time : 19:09
  148. */
  149. @RequestMapping(value = "/getNextImage", method = RequestMethod.GET)
  150. public Result getNextImage(@RequestParam(value = "imageId", required = true) int imageId) {
  151. //
  152. ImagePathEntity imagePathEntity = imageService.getOneImage("next", imageId);
  153. if(imagePathEntity == null){
  154. return new Result(10001,"未查询到任何记录.",false);
  155. }else{
  156. return new Result(ResultCode.SUCCESS,(JSONObject)JSONObject.toJSON(imagePathEntity));
  157. }
  158. }
  159. @Deprecated
  160. @RequestMapping("/exportPicture")
  161. public void exportPicture(HttpServletRequest request, HttpServletResponse response) throws Exception {
  162. //定义根路径
  163. String rootPath = request.getRealPath("/");
  164. //创建文件
  165. File file = new File(rootPath + "temp_download");
  166. //判断文件是否存在,如果不存在,则创建此文件夹
  167. if (!file.exists()) {
  168. file.mkdir();
  169. }
  170. String name = "图片压缩包下载";
  171. String fileName = name + new Date().getTime();
  172. String zipFileName = fileName + ".zip";
  173. File zipFile = null;
  174. String path = rootPath + "temp_download";
  175. //调用工具类获取图片
  176. byte[] data = ImageByteUtil.image2byte("F:\\blank.jpg");
  177. //new一个文件对象用来保存图片,默认保存当前工程根目录
  178. if (data != null) {
  179. File imageFile = new File(path + File.separator + fileName + ".jpg");
  180. //创建输出流
  181. FileOutputStream outStream = new FileOutputStream(imageFile);
  182. //写入数据
  183. outStream.write(data);
  184. //关闭输出流
  185. outStream.close();
  186. }
  187. try {
  188. /*
  189. * 用JDK自带的zipentry压缩后,压缩包中的文件名中文乱码,故放弃此种方法,使用commons-compress-1.6.jar包
  190. * ZipEntry zipEntry = new ZipEntry(new
  191. * String(txtFile.getName().getBytes("GB2312"),"ISO-8859-1"));
  192. * zos.putNextEntry(zipEntry); ZipOutputStream zos = new
  193. * ZipOutputStream(zipFos); ZipEntry zipEntry = new ZipEntry(new
  194. * String(txtFile.getName().getBytes("GB2312"),"ISO-8859-1")); zos.
  195. * putNextEntry(zipEntry);
  196. * zos.write(FileUtils.readFileToByteArray(txtFile)); zos.flush();
  197. * zos.close();
  198. */
  199. //获取创建好的图片文件
  200. File imageFile = new File(path + "/" + fileName + ".jpg");
  201. // 打成压缩包
  202. zipFile = new File(path + "/" + zipFileName);
  203. FileOutputStream zipFos = new FileOutputStream(zipFile);
  204. ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
  205. if (archOut instanceof ZipArchiveOutputStream) {
  206. ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut;
  207. ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName());
  208. zos.putArchiveEntry(zipEntry);
  209. zos.write(FileUtils.readFileToByteArray(imageFile));
  210. zos.closeArchiveEntry();
  211. zos.flush();
  212. zos.close();
  213. }
  214. // 压缩完删除txt文件
  215. if (imageFile.exists()) {
  216. imageFile.delete();
  217. }
  218. // 输出到客户端
  219. OutputStream out = null;
  220. out = response.getOutputStream();
  221. response.reset();
  222. response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1"));
  223. response.setContentType("application/octet-stream; charset=utf-8");
  224. response.setCharacterEncoding("UTF-8");
  225. out.write(FileUtils.readFileToByteArray(zipFile));
  226. out.flush();
  227. out.close();
  228. // 输出客户端结束后,删除压缩包
  229. if (zipFile.exists()) {
  230. zipFile.delete();
  231. }
  232. } catch (IOException e) {
  233. // TODO Auto-generated catch block
  234. e.printStackTrace();
  235. } catch (ArchiveException e) {
  236. // TODO Auto-generated catch block
  237. e.printStackTrace();
  238. }
  239. }
  240. /**
  241. * 压缩多个文件到一个zip压缩包中
  242. *
  243. * @param request
  244. * @param response
  245. * @throws Exception
  246. * @description :
  247. * @date 2016-11-4
  248. * @author liucong
  249. */
  250. @RequestMapping("/zipMoreFiles")
  251. public void zipMoreFiles(HttpServletRequest request, HttpServletResponse response) throws Exception {
  252. //定义根路径
  253. String rootPath = request.getRealPath("/");
  254. //创建文件
  255. File file = new File(rootPath + "temp_download");
  256. //判断文件是否存在,如果不存在,则创建此文件夹
  257. if (!file.exists()) {
  258. file.mkdir();
  259. }
  260. String name = "图片压缩包下载";
  261. String fileName = name + new Date().getTime();
  262. String zipFileName = fileName + ".zip";
  263. File zipFile = null;
  264. String path = rootPath + "temp_download";
  265. //获取本地文件夹中图片集合,事先准备好图片
  266. File files = new File("F:\\images\\");
  267. //获取该文件夹中的图片文件数组
  268. File[] filelist = files.listFiles();
  269. // 打成压缩包
  270. zipFile = new File(path + "/" + zipFileName);
  271. FileOutputStream zipFos = new FileOutputStream(zipFile);
  272. ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
  273. if (archOut instanceof ZipArchiveOutputStream) {
  274. ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut;
  275. for (File imageFile : filelist) {
  276. ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName());
  277. zos.putArchiveEntry(zipEntry);
  278. zos.write(FileUtils.readFileToByteArray(imageFile));
  279. zos.closeArchiveEntry();
  280. zos.flush();
  281. }
  282. zos.close();
  283. }
  284. // 输出到客户端
  285. OutputStream out = null;
  286. out = response.getOutputStream();
  287. response.reset();
  288. response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1"));
  289. response.setContentType("application/octet-stream; charset=utf-8");
  290. response.setCharacterEncoding("UTF-8");
  291. out.write(FileUtils.readFileToByteArray(zipFile));
  292. out.flush();
  293. out.close();
  294. // 输出客户端结束后,删除压缩包
  295. if (zipFile.exists()) {
  296. zipFile.delete();
  297. }
  298. }
  299. @RequestMapping("/exportImage")
  300. public void exportImage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "startDate", required = false) String startDate, @RequestParam(value = "endDate", required = false) String endDate, @RequestParam(value = "imageIds", required = false) String imageIds) throws Exception {
  301. // if(imageIds == null || imageIds.trim().isEmpty()){
  302. // log.error("输入参数[imageIds]不能为空.");
  303. // return ;
  304. // }
  305. log.info("输入参数[startDate]>>" + startDate);
  306. log.info("输入参数[endDate]>>" + endDate);
  307. log.info("输入参数[imageIds]>>" + imageIds);
  308. if (startDate == null && endDate == null && imageIds == null) {
  309. log.error("日期区间或指定图片导出二选一");
  310. return;
  311. }
  312. // 读取图片文件
  313. List<ImagePathEntity> imageList = imageService.getExportImageList(startDate, endDate, imageIds);
  314. if (imageList.size() == 0) {
  315. log.error("未读取到任何需要导出的图片.");
  316. return;
  317. }
  318. // System.out.println("imageList.size>>"+imageList.size());
  319. int i = 0;
  320. File[] fileList = new File[imageList.size()];
  321. for (ImagePathEntity imagePathEntity : imageList) {
  322. File imageFile = new File(imagePathEntity.getImgPath());
  323. fileList[i++] = imageFile;
  324. }
  325. //定义根路径
  326. String rootPath = request.getRealPath("/");
  327. //创建文件
  328. File file = new File(rootPath + "temp_download");
  329. //判断文件是否存在,如果不存在,则创建此文件夹
  330. if (!file.exists()) {
  331. file.mkdir();
  332. }
  333. String name = "图片压缩包下载";
  334. String fileName = name + new Date().getTime();
  335. String zipFileName = fileName + ".zip";
  336. File zipFile = null;
  337. String path = rootPath + "temp_download";
  338. // 打成压缩包
  339. zipFile = new File(path + "/" + zipFileName);
  340. FileOutputStream zipFos = new FileOutputStream(zipFile);
  341. ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
  342. if (archOut instanceof ZipArchiveOutputStream) {
  343. ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut;
  344. for (File imageFile : fileList) {
  345. ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName());
  346. zos.putArchiveEntry(zipEntry);
  347. zos.write(FileUtils.readFileToByteArray(imageFile));
  348. zos.closeArchiveEntry();
  349. zos.flush();
  350. }
  351. zos.close();
  352. }
  353. // 输出到客户端
  354. OutputStream out = null;
  355. out = response.getOutputStream();
  356. response.reset();
  357. response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1"));
  358. response.setContentType("application/octet-stream; charset=utf-8");
  359. response.setCharacterEncoding("UTF-8");
  360. out.write(FileUtils.readFileToByteArray(zipFile));
  361. out.flush();
  362. out.close();
  363. // 输出客户端结束后,删除压缩包
  364. if (zipFile.exists()) {
  365. zipFile.delete();
  366. }
  367. }
  368. /**
  369. * @Method : exportOneImage
  370. * @Description : 导出一张图片信息
  371. * @Params : [imageId]
  372. * @Return : byte[]
  373. * @Author : ZhuoNing
  374. * @Date : 2021/1/23
  375. * @Time : 19:53
  376. */
  377. @RequestMapping(value = "/exportOneImage", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
  378. // @ResponseBody
  379. public void exportOneImage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "imageId", required = true) String imageId) throws IOException {
  380. // 读取
  381. ImagePathEntity imagePathEntity = imageService.getOneImageDetail(imageId);
  382. if (imagePathEntity == null) {
  383. log.error("imagePathEntity[" + imagePathEntity.toString() + "]==null");
  384. }
  385. String imageName = imagePathEntity.getFileName();
  386. String fileUrl = imagePathEntity.getImgPath();
  387. File file = new File(fileUrl);
  388. if (file.exists()) {
  389. response.setContentType("application/force-download");// 设置强制下载不打开
  390. response.addHeader("Content-Disposition",
  391. "attachment;fileName=" + imageName);// 设置文件名
  392. byte[] buffer = new byte[1024];
  393. FileInputStream fis = null;
  394. BufferedInputStream bis = null;
  395. try {
  396. fis = new FileInputStream(file);
  397. bis = new BufferedInputStream(fis);
  398. OutputStream os = response.getOutputStream();
  399. int i = bis.read(buffer);
  400. while (i != -1) {
  401. os.write(buffer, 0, i);
  402. i = bis.read(buffer);
  403. }
  404. System.out.println("success");
  405. } catch (Exception e) {
  406. e.printStackTrace();
  407. } finally {
  408. if (bis != null) {
  409. try {
  410. bis.close();
  411. } catch (IOException e) {
  412. e.printStackTrace();
  413. }
  414. }
  415. if (fis != null) {
  416. try {
  417. fis.close();
  418. } catch (IOException e) {
  419. e.printStackTrace();
  420. }
  421. }
  422. }
  423. }
  424. }
  425. // @RequestMapping(value = "/exportOneImage",method = RequestMethod.GET,produces = MediaType.IMAGE_JPEG_VALUE)
  426. // @ResponseBody
  427. // public void exportOneImage(HttpServletRequest request,HttpServletResponse response,@RequestParam(value = "imageId",required = true) String imageId) throws IOException {
  428. // log.info("exportOneImage");
  429. // // 读取
  430. // ImagePathEntity imagePathEntity = imageService.getOneImageDetail(imageId);
  431. // System.out.println("imagePathEntity>>"+imagePathEntity);
  432. // if(imagePathEntity == null){
  433. // log.error("imagePathEntity["+imagePathEntity.toString()+"]==null");
  434. // }
  435. //// File file = new File(imagePathEntity.getImgPath());
  436. //// File fileBase = new File("test.txt");
  437. //// try {
  438. //// ServletOutputStream outputStream = response.getOutputStream();
  439. //// FileInputStream inputStream = new FileInputStream(fileBase);
  440. //// response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode("这里写用户下载时的文件名.txt", "UTF-8"));
  441. //// //注意, jdk8中还没有这个方法,使用jdk8的小伙伴要自己想办法复制流
  442. //// inputStream.transferTo(outputStream);
  443. //// outputStream.close();
  444. //// inputStream.close();
  445. //// } catch (IOException e) {
  446. //// e.printStackTrace();
  447. //// }
  448. // /////////////////////////////////////////////////////////////////
  449. //// String path=pathTool.getUserAvatarPath()+fileName;
  450. // String path = imagePathEntity.getImgPath();
  451. // System.out.println("path 2>>"+path);
  452. // String fileName = imagePathEntity.getFileName();
  453. // System.out.println("fileName>>"+fileName);
  454. // OutputStream os = null;
  455. // InputStream is = null;
  456. // try {
  457. // String contentType="";
  458. // String ext="";
  459. // if(fileName.endsWith("jpg")) {
  460. // contentType="image/jpeg";
  461. // ext="jpg";
  462. // }else if(fileName.endsWith("png")) {
  463. // contentType="image/png";
  464. // ext="png";
  465. // }else if(fileName.endsWith("gif")) {
  466. // contentType="image/gif";
  467. // ext="gif";
  468. // }
  469. // System.out.println(" path>>"+path);
  470. // is=new FileInputStream(new File(path));
  471. // BufferedImage image = ImageIO.read(is);
  472. // System.out.println(" contentType>>"+contentType);
  473. // response.setContentType(contentType);
  474. // os = response.getOutputStream();
  475. // if (image != null) {
  476. // ImageIO.write(image,ext, os);
  477. // }
  478. // } catch (IOException e) {
  479. // e.printStackTrace();
  480. // } finally {
  481. // if (is != null) {
  482. // is.close();
  483. // }
  484. // if (os != null) {
  485. // os.flush();
  486. // os.close();
  487. // }
  488. // }
  489. // }
  490. /**
  491. * @Method : exportOneImage2
  492. * @Description : 程序出错,不可用
  493. * @Params : [imageId]
  494. * @Return : java.awt.image.BufferedImage
  495. * @Author : ZhuoNing
  496. * @Date : 2021/1/23
  497. * @Time : 19:54
  498. */
  499. @Deprecated
  500. @RequestMapping(value = "/exportOneImage2", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
  501. @ResponseBody
  502. public BufferedImage exportOneImage2(@RequestParam(value = "imageId", required = true) String imageId) throws IOException {
  503. // 读取
  504. ImagePathEntity imagePathEntity = imageService.getOneImageDetail(imageId);
  505. if (imagePathEntity == null) {
  506. log.error("imagePathEntity[" + imagePathEntity.toString() + "]==null");
  507. }
  508. return ImageIO.read(new FileInputStream(new File(imagePathEntity.getImgPath())));
  509. }
  510. @Deprecated
  511. @RequestMapping(value = "/exportOneImage3", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
  512. @ResponseBody
  513. public byte[] exportOneImage3(@RequestParam(value = "imageId", required = true) String imageId) throws IOException {
  514. // 读取
  515. ImagePathEntity imagePathEntity = imageService.getOneImageDetail(imageId);
  516. if (imagePathEntity == null) {
  517. log.error("imagePathEntity[" + imagePathEntity.toString() + "]==null");
  518. }
  519. File file = new File(imagePathEntity.getImgPath());
  520. FileInputStream inputStream = new FileInputStream(file);
  521. byte[] bytes = new byte[inputStream.available()];
  522. inputStream.read(bytes, 0, inputStream.available());
  523. return bytes;
  524. }
  525. @Deprecated
  526. @RequestMapping(value = "/downloadPicture", method = RequestMethod.GET)
  527. public void downloadPicture(@RequestParam(value = "imageId", required = true) String imageId) {
  528. // 读取
  529. ImagePathEntity imagePathEntity = imageService.getOneImageDetail(imageId);
  530. if (imagePathEntity == null) {
  531. log.error("imagePathEntity[" + imagePathEntity.toString() + "]==null");
  532. }
  533. // 拼接完整图片路径。这里填写图片链接
  534. // String urlPath = "";
  535. // 获取图片文件后缀名
  536. // String postfix = "." + StringUtils.substringAfterLast(url, ".");
  537. // 获取当前类的所在项目路径
  538. // File directory = new File("");
  539. // String courseFile;
  540. String srcPath;
  541. // File srcFile = null;
  542. FileInputStream fileInputStream = null;
  543. InputStream fis = null;
  544. OutputStream out = null;
  545. try {
  546. // courseFile = directory.getCanonicalPath();
  547. // String fileName = "\\" + StringUtil.getUUID() + postfix;
  548. String fileName = imagePathEntity.getFileName();
  549. System.out.println("fileName>>" + fileName);
  550. // 下载文件
  551. // FileDownloadUtil.downloadHttpUrl(urlPath, courseFile, fileName);
  552. // srcPath = courseFile + fileName;
  553. // srcFile = new File(srcPath);
  554. srcPath = imagePathEntity.getImgPath();
  555. System.out.println("srcPath>>" + srcPath);
  556. fileInputStream = new FileInputStream(srcPath);
  557. fis = new BufferedInputStream(fileInputStream);
  558. byte[] buffer = new byte[fis.available()];
  559. fis.read(buffer);
  560. response.setContentType("application/octet-stream");
  561. response.setHeader("Content-disposition", "attachment;filename=" + fileName);
  562. out = response.getOutputStream();
  563. out.write(buffer);
  564. out.flush();
  565. out.close();
  566. } catch (Exception e) {
  567. e.printStackTrace();
  568. } finally {
  569. try {
  570. if (fileInputStream != null) {
  571. fileInputStream.close();
  572. }
  573. if (fis != null) {
  574. fis.close();
  575. }
  576. if (out != null) {
  577. out.close();
  578. }
  579. } catch (IOException e) {
  580. e.printStackTrace();
  581. }
  582. }
  583. // 删除中间文件
  584. // if (srcFile != null) {
  585. // System.out.println(FileDownloadUtil.deleteFile(srcFile));
  586. // }
  587. }
  588. }