附件补充下载
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

873 lines
38 KiB

  1. package com.bw.fileDownload.service;
  2. import java.awt.image.BufferedImage;
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.math.BigDecimal;
  11. import java.security.MessageDigest;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.concurrent.TimeUnit;
  18. import java.util.regex.Matcher;
  19. import java.util.regex.Pattern;
  20. import org.springframework.beans.factory.annotation.Value;
  21. import org.springframework.stereotype.Component;
  22. import com.alibaba.fastjson.JSON;
  23. import com.alibaba.fastjson.JSONObject;
  24. import com.bw.fileDownload.entity.Constants;
  25. import com.bw.fileDownload.entity.FileInfoPo;
  26. import com.bw.fileDownload.utils.StringUtil;
  27. import lombok.extern.slf4j.Slf4j;
  28. import okhttp3.Call;
  29. import okhttp3.Headers;
  30. import okhttp3.MediaType;
  31. import okhttp3.MultipartBody;
  32. import okhttp3.OkHttpClient;
  33. import okhttp3.Request;
  34. import okhttp3.RequestBody;
  35. import okhttp3.Response;
  36. import okhttp3.ResponseBody;
  37. @Component
  38. @Slf4j
  39. public class DownloadExecService {
  40. @Value("${file.download.path}")
  41. private String downloadFilePath;
  42. @Value("${file.upload.url}")
  43. private String gofastUrl;
  44. private static final String IMAGE_TYPE_JPG = "jpg";
  45. private static final String IMAGE_TYPE_JPEG = "jpeg";
  46. private static final String IMAGE_TYPE_PNG = "png";
  47. private static OkHttpClient client = new OkHttpClient();
  48. public Map downloadAndUploadVidoAndAudio(List<String> videoList, Map<String, Object> resultData) {
  49. String header = "header";
  50. Map<String, String> hederMap = new HashMap<>();
  51. List<String> videoPath = new ArrayList<>();
  52. List<Map> delVideoAndAudioList = new ArrayList<>();
  53. Map<String, Object> returnMap = new HashMap<>(32);
  54. List<Map> videoUrl = new ArrayList<>();
  55. List<Map> videoPathSize = new ArrayList<>();
  56. Integer downloadStatusCode = 0;
  57. for (String url : videoList) {
  58. Map delVideoAndAudioMap = new HashMap(32);
  59. Map videoMap = new HashMap(32);
  60. Map videoPathMap = new HashMap(32);
  61. if (formatJudgment(url)) {
  62. downloadStatusCode = Constants.URL_FORMAT;
  63. continue;
  64. }
  65. String[] split = url.split("###");
  66. String videoType = split[1];
  67. String fileType = videoType;
  68. String m3u8Type = ".m3u8";
  69. String youtubeTypeFr = "youtube";
  70. String youtubeTypeSec = "youtu.be";
  71. String needDownloadVideoUrl = split[0];
  72. try {
  73. if (!StringUtil.hasValue(needDownloadVideoUrl)) {
  74. downloadStatusCode = Constants.VIDEO_LIST_NULL;
  75. continue;
  76. }
  77. if (m3u8Type.equals(fileType)) {
  78. downLoadVideoExeCommandForFfmpeg(needDownloadVideoUrl);
  79. } else if (url.contains(youtubeTypeFr) || url.contains(youtubeTypeSec)) {
  80. downLoadVideoExeCommandLp(needDownloadVideoUrl, videoType);
  81. } else {
  82. File file = new File(downloadFilePath + getMd5(needDownloadVideoUrl).concat(fileType));
  83. if (resultData.containsKey(header)) {
  84. hederMap = (Map<String, String>) resultData.get(header);
  85. downloadFile(needDownloadVideoUrl, downloadFilePath, getMd5(needDownloadVideoUrl).concat(fileType), hederMap);
  86. } else {
  87. downloadFile(needDownloadVideoUrl, downloadFilePath, getMd5(needDownloadVideoUrl).concat(fileType));
  88. if (!file.exists() || file.length() < 100) {
  89. downLoadFileExeCommandForWGet(needDownloadVideoUrl, fileType);
  90. }
  91. }
  92. }
  93. } catch (Exception e) {
  94. downloadStatusCode = Constants.DOWNLOAD_VIDEO_FAIL;
  95. log.error("Video or audio download failed url:{},Exception:",url,e);
  96. continue;
  97. }
  98. String delVideoAndAudioUrl = "";
  99. String uploadFileUrl = "";
  100. try {
  101. Map<String, String> videoTimeMap = getVideoTime(downloadFilePath + getMd5(needDownloadVideoUrl) + videoType);
  102. String videoTime = videoTimeMap.get("videoTime");
  103. String resolution = videoTimeMap.get("resolution");
  104. videoPathMap.put("videoTime", videoTime);
  105. videoPathMap.put("resolution", resolution);
  106. String uploadResultJson = "";
  107. if (m3u8Type.equals(fileType)) {
  108. uploadResultJson = upLoadFile(downloadFilePath + getMd5(needDownloadVideoUrl) + ".mp4");
  109. } else {
  110. uploadResultJson = upLoadFile(downloadFilePath + getMd5(needDownloadVideoUrl) + videoType);
  111. }
  112. JSONObject jsonObject = JSON.parseObject(uploadResultJson);
  113. String md5 = jsonObject.getString("md5");
  114. delVideoAndAudioUrl = gofastUrl.replace("/upload", "") + "/delete?md5=" + md5;
  115. uploadFileUrl = jsonObject.getString("url").replaceAll("\\?.*", "");
  116. String src = jsonObject.getString("src");
  117. Long size = jsonObject.getLong("size");
  118. String printSize = getPrintSize(size);
  119. videoPathMap.put("size", printSize);
  120. videoPathMap.put("url", src);
  121. } catch (Exception e) {
  122. downloadStatusCode = Constants.UPLOAD_VIDEO_FAIL;
  123. log.error("Video upload failed url:{},Exception:",url,e);
  124. continue;
  125. }
  126. videoPath.add(uploadFileUrl);
  127. delVideoAndAudioMap.put(uploadFileUrl, delVideoAndAudioUrl);
  128. delVideoAndAudioList.add(delVideoAndAudioMap);
  129. videoMap.put("originalUrl", needDownloadVideoUrl);
  130. videoMap.put("gofastUrl", uploadFileUrl);
  131. videoUrl.add(videoMap);
  132. videoPathSize.add(videoPathMap);
  133. }
  134. returnMap.put("videoUrl", videoUrl);
  135. returnMap.put("videoPath", videoPath);
  136. returnMap.put("delVideoAndAudioList", delVideoAndAudioList);
  137. returnMap.put("downloadStatusCode", downloadStatusCode);
  138. resultData.put("ErroeMessage", downloadStatusCode);
  139. returnMap.put("videoPathSize", videoPathSize);
  140. resultData.put("videoUrl", videoUrl);
  141. resultData.put("videoPath", videoPath);
  142. resultData.put("delVideoAndAudioList", delVideoAndAudioList);
  143. resultData.put("videoPathSize", videoPathSize);
  144. return returnMap;
  145. }
  146. private boolean formatJudgment(String url) {
  147. String formatTag = "###";
  148. if (!url.contains(formatTag)) {
  149. return true;
  150. }
  151. return false;
  152. }
  153. private void downLoadVideoExeCommandForFfmpeg(String videoUrl) {
  154. String news_id = getMd5(videoUrl);
  155. String command = "ffmpeg -i " + videoUrl + " " + downloadFilePath
  156. + news_id + ".mp4";
  157. InputStream in = null;
  158. BufferedReader read = null;
  159. String Qavg = "Qavg:";
  160. try {
  161. System.out.println("DownloadUtil" + command);
  162. Process process;
  163. try {
  164. String line = "";
  165. File file = new File(downloadFilePath);
  166. if (!file.exists()) {
  167. file.mkdirs();
  168. }
  169. process = Runtime.getRuntime().exec(command);
  170. in = process.getErrorStream();
  171. read = new BufferedReader(new InputStreamReader(in));
  172. while ((line = read.readLine()) != null) {
  173. System.out.println("DownloadUtil" + line);
  174. if (line.contains(Qavg)) {
  175. break;
  176. }
  177. }
  178. process.destroy();
  179. } catch (IOException var26) {
  180. var26.printStackTrace();
  181. }
  182. } finally {
  183. try {
  184. in.close();
  185. read.close();
  186. } catch (IOException var25) {
  187. var25.printStackTrace();
  188. }
  189. }
  190. }
  191. private void downLoadVideoExeCommandLp(String videoUrl, String videoType) {
  192. String command = "";
  193. List<String> stringList = doGetVideoIdCommand(videoUrl);
  194. for (String s : stringList) {
  195. String videoId = s.split(" ")[0];
  196. command = "yt_dlp --no-check-certificate -f " + videoId + " -o " + downloadFilePath + getMd5(videoUrl) + videoType + " " + videoUrl;
  197. log.info("视频下载命令:" + command);
  198. break;
  199. }
  200. InputStream in = null;
  201. BufferedReader read = null;
  202. try {
  203. Process process;
  204. try {
  205. String line = "";
  206. File file = new File(downloadFilePath);
  207. if (!file.exists()) {
  208. file.mkdirs();
  209. }
  210. process = Runtime.getRuntime().exec(command, (String[]) null, file);
  211. in = process.getInputStream();
  212. read = new BufferedReader(new InputStreamReader(in));
  213. while ((line = read.readLine()) != null) {
  214. System.out.println(line);
  215. }
  216. process.destroy();
  217. } catch (IOException var26) {
  218. var26.printStackTrace();
  219. }
  220. } finally {
  221. try {
  222. in.close();
  223. read.close();
  224. } catch (IOException var25) {
  225. var25.printStackTrace();
  226. }
  227. }
  228. }
  229. private void downLoadFileExeCommandForWGet(String fileUrl, String filetype) {
  230. String news_id = getMd5(fileUrl);
  231. String htpps = "https";
  232. String command = "wget -d --user-agent=\"Mozilla/5.0 (Windows NT xy; rv:10.0) Gecko/20100101 Firefox/10.0\" " + fileUrl + " -O " + downloadFilePath + news_id + filetype;
  233. if (!fileUrl.contains(htpps)) {
  234. command = "wget --no-check-certificate -d --user-agent=\"Mozilla/5.0 (Windows NT xy; rv:10.0) Gecko/20100101 Firefox/10.0\" " + fileUrl + " -O " + downloadFilePath + news_id + filetype;
  235. }
  236. InputStream in = null;
  237. BufferedReader read = null;
  238. try {
  239. log.info("wget 方法进入:" + command);
  240. Process process;
  241. try {
  242. String line = "";
  243. process = Runtime.getRuntime().exec(command);
  244. log.info("执行命令");
  245. log.info("视频下载命令:" + command);
  246. in = process.getErrorStream();
  247. read = new BufferedReader(new InputStreamReader(in));
  248. while ((line = read.readLine()) != null) {
  249. log.info("视频下载进度:" + line);
  250. }
  251. process.destroy();
  252. } catch (IOException var26) {
  253. var26.printStackTrace();
  254. log.info("下载视频异常", var26);
  255. }
  256. } finally {
  257. try {
  258. in.close();
  259. read.close();
  260. } catch (IOException var25) {
  261. var25.printStackTrace();
  262. }
  263. }
  264. }
  265. public String getMd5(String string) {
  266. try {
  267. MessageDigest md5 = MessageDigest.getInstance("MD5");
  268. byte[] bs = md5.digest(string.getBytes("UTF-8"));
  269. StringBuilder sb = new StringBuilder(40);
  270. for (byte x : bs) {
  271. if ((x & 0xff) >> 4 == 0) {
  272. sb.append("0").append(Integer.toHexString(x & 0xff));
  273. } else {
  274. sb.append(Integer.toHexString(x & 0xff));
  275. }
  276. }
  277. return sb.toString();
  278. } catch (Exception e) {
  279. log.info("get md 5 exception:", e);
  280. return "nceaform" + System.currentTimeMillis();
  281. }
  282. }
  283. private List<String> doGetVideoIdCommand(String videoUrl) {
  284. List resultList = new ArrayList();
  285. String command = "yt_dlp --no-check-certificate -F " + videoUrl;
  286. InputStream in = null;
  287. BufferedReader read = null;
  288. try {
  289. Process process;
  290. try {
  291. String line = "";
  292. process = Runtime.getRuntime().exec(command, (String[]) null);
  293. in = process.getInputStream();
  294. read = new BufferedReader(new InputStreamReader(in, "gbk"));
  295. while ((line = read.readLine()) != null) {
  296. System.out.println(line);
  297. if (line.contains("mp4")) {
  298. if (line.contains("256x144") && !line.contains("video only")) {
  299. resultList.add(line);
  300. break;
  301. } else if (line.contains("640x360") && !line.contains("video only")) {
  302. resultList.add(line);
  303. break;
  304. } else if (line.contains("854x480") && !line.contains("video only")) {
  305. resultList.add(line);
  306. break;
  307. } else if (line.contains("1280x720") && !line.contains("video only")) {
  308. resultList.add(line);
  309. break;
  310. }
  311. }
  312. }
  313. process.destroy();
  314. } catch (IOException var26) {
  315. var26.printStackTrace();
  316. }
  317. } finally {
  318. try {
  319. in.close();
  320. read.close();
  321. } catch (IOException var25) {
  322. var25.printStackTrace();
  323. }
  324. }
  325. return resultList;
  326. }
  327. public void downloadFile(String fileUrl, String savePath, String customFileName, Map<String, String>... headers) throws IOException {
  328. okhttp3.Request.Builder builder = new okhttp3.Request.Builder();
  329. if (headers != null && headers.length > 0) {
  330. Map<String, String> tempHeaders = headers[0];
  331. Iterator<String> iterator = tempHeaders.keySet().iterator();
  332. while (iterator.hasNext()) {
  333. String key = iterator.next();
  334. builder.addHeader(key, tempHeaders.get(key));
  335. }
  336. } else {
  337. builder.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7").addHeader("Accept-Language", "zh-CN,zh;q=0.9").addHeader("Cache-Control", "no-cache").addHeader("Connection", "keep-alive").addHeader("Pragma", "no-cache").addHeader("Sec-Fetch-Dest", "document").addHeader("Sec-Fetch-Mode", "navigate").addHeader("Sec-Fetch-Site", "none").addHeader("Sec-Fetch-User", "?1").addHeader("Upgrade-Insecure-Requests", "1").addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36").addHeader("sec-ch-ua", "\"Google Chrome\";v=\"111\", \"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"111\"").addHeader("sec-ch-ua-mobile", "?0").addHeader("sec-ch-ua-platform", "\"Windows\"");
  338. }
  339. Request request = builder.url(fileUrl).get().build();
  340. Call call = client.newCall(request);
  341. Response response = call.execute();
  342. if (!response.isSuccessful()) {
  343. throw new IOException("Failed to download file: " + response);
  344. }
  345. ResponseBody responseBody = response.body();
  346. if (responseBody != null) {
  347. InputStream inputStream = responseBody.byteStream();
  348. File fileDirectory = new File(savePath);
  349. if (!fileDirectory.exists()) {
  350. fileDirectory.mkdirs();
  351. }
  352. File file = new File(fileDirectory, customFileName);
  353. FileOutputStream outputStream = new FileOutputStream(file);
  354. byte[] buffer = new byte[4096];
  355. int bytesRead;
  356. while ((bytesRead = inputStream.read(buffer)) != -1) {
  357. outputStream.write(buffer, 0, bytesRead);
  358. }
  359. outputStream.flush();
  360. outputStream.close();
  361. inputStream.close();
  362. }
  363. }
  364. private Map<String, String> getVideoTime(String video_path) {
  365. Map infoMap = new HashMap(32);
  366. String videoTime = "";
  367. String resolution = "";
  368. String command = "ffmpeg -i " + video_path;
  369. InputStream in = null;
  370. BufferedReader read = null;
  371. try {
  372. String info = "";
  373. String line = "";
  374. System.out.println("视频时间解析命令" + command);
  375. Process process;
  376. process = Runtime.getRuntime().exec(command);
  377. in = process.getErrorStream();
  378. read = new BufferedReader(new InputStreamReader(in));
  379. while ((line = read.readLine()) != null) {
  380. info = info + line;
  381. }
  382. process.destroy();
  383. String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
  384. Pattern pattern = Pattern.compile(regexDuration);
  385. Matcher m = pattern.matcher(info);
  386. if (m.find()) {
  387. videoTime = getTimelen(m.group(1)) + "s";
  388. System.out.println("视频时长:" + videoTime + "s , 开始时间:" + m.group(2) + ", 比特率:" + m.group(3) + "kb/s");
  389. }
  390. String regexVideo = "Video: (.*?), (.*?), (\\d+x\\d+)[,\\s]";
  391. pattern = Pattern.compile(regexVideo);
  392. m = pattern.matcher(info);
  393. if (m.find()) {
  394. resolution = m.group(3);
  395. System.out.println("编码格式:" + m.group(1) + ", 视频格式:" + m.group(2) + ", 分辨率:" + resolution + "kb/s");
  396. }
  397. } catch (Exception e) {
  398. e.printStackTrace();
  399. }
  400. infoMap.put("videoTime", videoTime);
  401. infoMap.put("resolution", resolution);
  402. return infoMap;
  403. }
  404. private String upLoadFile(String filePath) {
  405. File file = new File(filePath);
  406. String realFilename = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
  407. MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
  408. builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"file\";filename=\"" + realFilename + "\""),
  409. RequestBody.create(MediaType.parse("image/png"), file)
  410. ).addFormDataPart("output", "json").build();
  411. RequestBody body = builder.build();
  412. Request request = new Request.Builder().url(gofastUrl).post(body).header("Expect", "100-continue").build();
  413. OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
  414. OkHttpClient client = okBuilder.connectTimeout(600, TimeUnit.MILLISECONDS)
  415. .readTimeout(600, TimeUnit.SECONDS).build();
  416. Call call = client.newCall(request);
  417. String html = "";
  418. Response response = null;
  419. try {
  420. response = call.execute();
  421. html = response.body().string();
  422. System.out.println("DownloadUtil" + html);
  423. } catch (IOException e) {
  424. log.info("upload fail:" + filePath);
  425. e.printStackTrace();
  426. } finally {
  427. response.close();
  428. }
  429. file.delete();
  430. return html;
  431. }
  432. public String getPrintSize(long size) {
  433. BigDecimal bigDecimal = new BigDecimal(size);
  434. BigDecimal bigDecimal1 = new BigDecimal(1024);
  435. BigDecimal divide = bigDecimal.divide(bigDecimal1, 2, BigDecimal.ROUND_HALF_UP);
  436. return divide.toString() + "KB";
  437. }
  438. private String getTimelen(String timelen) {
  439. int min = 0;
  440. String index = "0";
  441. int two = 2;
  442. String[] strs = timelen.split(":");
  443. if (strs[0].compareTo(index) > 0) {
  444. min += Integer.valueOf(strs[0]) * 60 * 60;
  445. }
  446. if (strs[1].compareTo(index) > 0) {
  447. min += Integer.valueOf(strs[1]) * 60;
  448. }
  449. if (strs[two].compareTo(index) > 0) {
  450. min += Math.round(Float.valueOf(strs[2]));
  451. }
  452. return String.valueOf(min);
  453. }
  454. public Map DownloadAndUploadImg(List<String> imgList, Map<String, Object> resultData) {
  455. String header = "header";
  456. Map<String, String> hederMap = new HashMap<>();
  457. String cid = (String) resultData.get("cid");
  458. List<String> imgPath = new ArrayList<>();
  459. List<Map> delimgList = new ArrayList<>();
  460. Map<String, Object> returnMap = new HashMap<>(32);
  461. List<Map> imagePathSize = new ArrayList<>();
  462. Map contentimgs = new HashMap<>(32);
  463. int imgTag = 1;
  464. Integer downloadStatusCode = 0;
  465. for (String url : imgList) {
  466. if (StringUtil.checkDoubleHttp(url)) {
  467. log.info("Download url have many http:" + url + ",cid:" + cid);
  468. continue;
  469. }
  470. Map delFileMap = new HashMap(32);
  471. Map imgMap = new HashMap(32);
  472. Map imagePathMap = new HashMap(32);
  473. if (formatJudgment(url)) {
  474. downloadStatusCode = Constants.URL_FORMAT;
  475. log.info("Download url splicing error url:" + url + ",cid:" + cid);
  476. continue;
  477. }
  478. String[] split = url.split("###");
  479. String fileUrl = split[0];
  480. String fileType = split[1];
  481. try {
  482. if (!StringUtil.hasValue(fileUrl)) {
  483. downloadStatusCode = Constants.IMAGE_LIST_NULL;
  484. continue;
  485. }
  486. File file = new File(downloadFilePath + getMd5(fileUrl).concat(fileType));
  487. if (resultData.containsKey(header)) {
  488. hederMap = (Map<String, String>) resultData.get(header);
  489. downloadFile(fileUrl, downloadFilePath, getMd5(fileUrl).concat(fileType), hederMap);
  490. } else {
  491. downloadFile(fileUrl, downloadFilePath, getMd5(fileUrl).concat(fileType));
  492. if (!file.exists() || file.length() < 100) {
  493. downLoadFileExeCommandForWGet(fileUrl, fileType);
  494. }
  495. }
  496. } catch (Exception e) {
  497. downloadStatusCode = Constants.DOWNLOAD_IMAGE_FAIL;
  498. log.info("Image download failed url:{},Exception:",url, e);
  499. e.printStackTrace();
  500. continue;
  501. }
  502. String delimgUrl = "";
  503. String uploadimgUrl = "";
  504. try {
  505. String uploadResultJson = upLoadFile(downloadFilePath + getMd5(fileUrl) + fileType);
  506. JSONObject jsonObject = JSON.parseObject(uploadResultJson);
  507. String md5 = jsonObject.getString("md5");
  508. delimgUrl = gofastUrl.replace("/upload", "") + "/delete?md5=" + md5;
  509. uploadimgUrl = jsonObject.getString("url").replaceAll("\\?.*", "");;
  510. Long size = jsonObject.getLong("size");
  511. imagePathMap.put("videoTime", "");
  512. try {
  513. String imgWidthAndHeight = getImgWidthAndHeight(downloadFilePath + getMd5(fileUrl) + fileType);
  514. String printSize = getPrintSize(size);
  515. String src = jsonObject.getString("src");
  516. imagePathMap.put("resolution", imgWidthAndHeight);
  517. imagePathMap.put("size", printSize);
  518. imagePathMap.put("url", src);
  519. } catch (Exception e) {
  520. String printSize = getPrintSize(size);
  521. String src = jsonObject.getString("src");
  522. imagePathMap.put("resolution", "100x100");
  523. imagePathMap.put("size", printSize);
  524. imagePathMap.put("url", src);
  525. log.error("Image size calculation failed url:{},Exception:",url, e);
  526. }
  527. } catch (Exception e) {
  528. downloadStatusCode = Constants.UPLOAD_IMAGE_FAIL;
  529. log.error("Image upload failed url:{},Exception:",url, e);
  530. continue;
  531. }
  532. imgPath.add(uploadimgUrl);
  533. delFileMap.put(uploadimgUrl, delimgUrl);
  534. delimgList.add(delFileMap);
  535. imgMap.put("img", fileUrl);
  536. imgMap.put("rawimg", fileUrl);
  537. imgMap.put("imgtag", "img" + imgTag);
  538. imgMap.put("uploadImg", uploadimgUrl);
  539. contentimgs.put("img" + imgTag, imgMap);
  540. imagePathSize.add(imagePathMap);
  541. imgTag++;
  542. }
  543. returnMap.put("contentimgs", contentimgs);
  544. returnMap.put("imagePath", imgPath);
  545. returnMap.put("delimgList", delimgList);
  546. returnMap.put("downloadStatusCode", downloadStatusCode);
  547. resultData.put("ErroeMessage", downloadStatusCode);
  548. returnMap.put("imagePathSize", imagePathSize);
  549. resultData.put("contentimgs", contentimgs);
  550. resultData.put("imagePath", imgPath);
  551. resultData.put("delimgList", delimgList);
  552. resultData.put("imagePathSize", imagePathSize);
  553. return returnMap;
  554. }
  555. public Map DownloadAndUploadImgNoUa(List<String> imgList, Map<String, Object> resultData) {
  556. String cid = (String) resultData.get("cid");
  557. List<String> imgPath = new ArrayList<>();
  558. List<Map> delimgList = new ArrayList<>();
  559. Map<String, Object> returnMap = new HashMap<>(32);
  560. List<Map> imagePathSize = new ArrayList<>();
  561. Map contentimgs = new HashMap<>(32);
  562. int imgTag = 1;
  563. Integer downloadStatusCode = 0;
  564. for (String url : imgList) {
  565. if (StringUtil.checkDoubleHttp(url)) {
  566. log.info("Download url have many http:" + url + ",cid:" + cid);
  567. continue;
  568. }
  569. Map delFileMap = new HashMap(32);
  570. Map imgMap = new HashMap(32);
  571. Map imagePathMap = new HashMap(32);
  572. if (formatJudgment(url)) {
  573. downloadStatusCode = Constants.URL_FORMAT;
  574. log.info("Download url splicing error url:" + url + ",cid:" + cid);
  575. continue;
  576. }
  577. String[] split = url.split("###");
  578. String fileUrl = split[0];
  579. String fileType = split[1];
  580. try {
  581. if (!StringUtil.hasValue(fileUrl)) {
  582. downloadStatusCode = Constants.IMAGE_LIST_NULL;
  583. continue;
  584. }
  585. downLoadFileExeCommandForWGetNoUa(fileUrl, fileType);
  586. } catch (Exception e) {
  587. downloadStatusCode = Constants.DOWNLOAD_IMAGE_FAIL;
  588. log.info("Image download failed url:{},Exception:",url, e);
  589. continue;
  590. }
  591. String delimgUrl = "";
  592. String uploadimgUrl = "";
  593. try {
  594. String uploadResultJson = upLoadFile(downloadFilePath + getMd5(fileUrl) + fileType);
  595. JSONObject jsonObject = JSON.parseObject(uploadResultJson);
  596. String md5 = jsonObject.getString("md5");
  597. delimgUrl = gofastUrl.replace("/upload", "") + "/delete?md5=" + md5;
  598. uploadimgUrl = jsonObject.getString("url").replaceAll("\\?.*", "");;
  599. Long size = jsonObject.getLong("size");
  600. imagePathMap.put("videoTime", "");
  601. try {
  602. String imgWidthAndHeight = getImgWidthAndHeight(downloadFilePath + getMd5(fileUrl) + fileType);
  603. String printSize = getPrintSize(size);
  604. String src = jsonObject.getString("src");
  605. imagePathMap.put("resolution", imgWidthAndHeight);
  606. imagePathMap.put("size", printSize);
  607. imagePathMap.put("url", src);
  608. } catch (Exception e) {
  609. String printSize = getPrintSize(size);
  610. String src = jsonObject.getString("src");
  611. imagePathMap.put("resolution", "100x100");
  612. imagePathMap.put("size", printSize);
  613. imagePathMap.put("url", src);
  614. log.info("Image size calculation failed url:" + url + ",Exception:" + e);
  615. }
  616. } catch (Exception e) {
  617. downloadStatusCode = Constants.UPLOAD_IMAGE_FAIL;
  618. log.error("Image upload failed url:{},Exception:",url, e);
  619. continue;
  620. }
  621. imgPath.add(uploadimgUrl);
  622. delFileMap.put(uploadimgUrl, delimgUrl);
  623. delimgList.add(delFileMap);
  624. imgMap.put("img", fileUrl);
  625. imgMap.put("rawimg", fileUrl);
  626. imgMap.put("imgtag", "img" + imgTag);
  627. imgMap.put("uploadImg", uploadimgUrl);
  628. contentimgs.put("img" + imgTag, imgMap);
  629. imagePathSize.add(imagePathMap);
  630. imgTag++;
  631. }
  632. returnMap.put("contentimgs", contentimgs);
  633. returnMap.put("imagePath", imgPath);
  634. returnMap.put("delimgList", delimgList);
  635. returnMap.put("downloadStatusCode", downloadStatusCode);
  636. resultData.put("ErroeMessage", downloadStatusCode);
  637. returnMap.put("imagePathSize", imagePathSize);
  638. resultData.put("contentimgs", contentimgs);
  639. resultData.put("imagePath", imgPath);
  640. resultData.put("delimgList", delimgList);
  641. resultData.put("imagePathSize", imagePathSize);
  642. return returnMap;
  643. }
  644. public String getImgWidthAndHeight(String filePath) {
  645. File file = new File(filePath);
  646. InputStream is = null;
  647. BufferedImage src = null;
  648. int width = -1;
  649. int height = -1;
  650. try {
  651. is = new FileInputStream(file);
  652. src = javax.imageio.ImageIO.read(is);
  653. width = src.getWidth(null);
  654. height = src.getHeight(null);
  655. is.close();
  656. } catch (IOException e) {
  657. }
  658. return width + "x" + height;
  659. }
  660. private void downLoadFileExeCommandForWGetNoUa(String fileUrl, String filetype) {
  661. String news_id = getMd5(fileUrl);
  662. //wget --no-check-certificate -d --user-agent="Mozilla/5.0 (Windows NT xy; rv:10.0) Gecko/20100101 Firefox/10.0" http://gxj.gz.gov.cn/attachment/7/7194/7194190/8648699.pdf -O 1f07794340006d986a7d87684d3ade03.pdf
  663. String command = "wget --no-check-certificate " + fileUrl + " -O " + downloadFilePath + news_id + filetype;
  664. InputStream in = null;
  665. BufferedReader read = null;
  666. try {
  667. log.info("wget 方法进入:" + command);
  668. Process process;
  669. try {
  670. String line = "";
  671. process = Runtime.getRuntime().exec(command);
  672. log.info("执行命令");
  673. log.info("视频下载命令:" + command);
  674. in = process.getErrorStream();
  675. read = new BufferedReader(new InputStreamReader(in));
  676. while ((line = read.readLine()) != null) {
  677. log.info("视频下载进度:" + line);
  678. }
  679. process.destroy();
  680. } catch (IOException var26) {
  681. var26.printStackTrace();
  682. log.info("下载视频异常", var26);
  683. }
  684. } finally {
  685. try {
  686. in.close();
  687. read.close();
  688. } catch (IOException var25) {
  689. var25.printStackTrace();
  690. }
  691. }
  692. }
  693. public Map DownloadAndUploadFile(List<String> fileList, Map<String, Object> resultData) {
  694. String header = "header";
  695. Map<String, String> hederMap = new HashMap<>();
  696. List<String> filePath = new ArrayList<>();
  697. List<Map> delFileList = new ArrayList<>();
  698. Map<String, Object> returnMap = new HashMap<>(32);
  699. List<Map> forwardUrl = new ArrayList<>();
  700. List<Map> filePathSize = new ArrayList<>();
  701. Integer downloadStatusCode = 0;
  702. for (String url : fileList) {
  703. Map fileInfoMap = new HashMap(32);
  704. Map delFileMap = new HashMap(32);
  705. Map fileMap = new HashMap(32);
  706. if (formatJudgment(url)) {
  707. downloadStatusCode = Constants.URL_FORMAT;
  708. continue;
  709. }
  710. String[] split = url.split("###");
  711. String fileUrl = split[0];
  712. String fileType = split[1];
  713. try {
  714. if (!StringUtil.hasValue(fileUrl)) {
  715. downloadStatusCode = Constants.IMAGE_LIST_NULL;
  716. continue;
  717. }
  718. File file = new File(downloadFilePath + getMd5(fileUrl).concat(fileType));
  719. if (resultData.containsKey(header)) {
  720. hederMap = (Map<String, String>) resultData.get(header);
  721. downloadFile(fileUrl, downloadFilePath, getMd5(fileUrl).concat(fileType), hederMap);
  722. } else {
  723. downloadFile(fileUrl, downloadFilePath, getMd5(fileUrl).concat(fileType));
  724. if (!file.exists() || file.length() < 100) {
  725. downLoadFileExeCommandForWGet(fileUrl, fileType);
  726. }
  727. }
  728. } catch (Exception e) {
  729. downloadStatusCode = Constants.DOWNLOAD_FILE_FAIL;
  730. log.error("File download failed url:{},Exception:",url,e);
  731. continue;
  732. }
  733. String delFileUrl = "";
  734. String uploadFileUrl = "";
  735. try {
  736. fileInfoMap.put("videoTime", "");
  737. fileInfoMap.put("resolution", "");
  738. String uploadResultJson = upLoadFile(downloadFilePath + getMd5(fileUrl) + fileType);
  739. JSONObject jsonObject = JSON.parseObject(uploadResultJson);
  740. String src = jsonObject.getString("src");
  741. String md5 = jsonObject.getString("md5");
  742. delFileUrl = gofastUrl.replace("/upload", "") + "/delete?md5=" + md5;
  743. uploadFileUrl = jsonObject.getString("url").replaceAll("\\?.*", "");;
  744. Long size = jsonObject.getLong("size");
  745. String printSize = getPrintSize(size);
  746. fileInfoMap.put("size", printSize);
  747. fileInfoMap.put("url", src);
  748. } catch (Exception e) {
  749. downloadStatusCode = Constants.UPLOAD_FILE_FAIL;
  750. log.info("File upload failed url:{},Exception:",url,e);
  751. continue;
  752. }
  753. filePath.add(uploadFileUrl);
  754. delFileMap.put(uploadFileUrl, delFileUrl);
  755. delFileList.add(delFileMap);
  756. fileMap.put("gofastUrl", uploadFileUrl);
  757. fileMap.put("originalUrl", fileUrl);
  758. forwardUrl.add(fileMap);
  759. filePathSize.add(fileInfoMap);
  760. }
  761. returnMap.put("forwardUrl", forwardUrl);
  762. returnMap.put("filePath", filePath);
  763. returnMap.put("delFileList", delFileList);
  764. returnMap.put("downloadStatusCode", downloadStatusCode);
  765. resultData.put("ErroeMessage", downloadStatusCode);
  766. returnMap.put("filePathSize", filePathSize);
  767. resultData.put("forwardUrl", forwardUrl);
  768. resultData.put("filePath", filePath);
  769. resultData.put("delFileList", delFileList);
  770. resultData.put("filePathSize", filePathSize);
  771. return returnMap;
  772. }
  773. public Map DownloadAndUploadFileNoUa(List<String> fileList, Map<String, Object> resultData) {
  774. List<String> filePath = new ArrayList<>();
  775. List<Map> delFileList = new ArrayList<>();
  776. Map<String, Object> returnMap = new HashMap<>(32);
  777. List<Map> forwardUrl = new ArrayList<>();
  778. List<Map> filePathSize = new ArrayList<>();
  779. Integer downloadStatusCode = 0;
  780. for (String url : fileList) {
  781. Map fileInfoMap = new HashMap(32);
  782. Map delFileMap = new HashMap(32);
  783. Map fileMap = new HashMap(32);
  784. if (formatJudgment(url)) {
  785. downloadStatusCode = Constants.URL_FORMAT;
  786. continue;
  787. }
  788. String[] split = url.split("###");
  789. String fileUrl = split[0];
  790. String fileType = split[1];
  791. try {
  792. if (!StringUtil.hasValue(fileUrl)) {
  793. downloadStatusCode = Constants.IMAGE_LIST_NULL;
  794. continue;
  795. }
  796. downLoadFileExeCommandForWGetNoUa(fileUrl, fileType);
  797. } catch (Exception e) {
  798. downloadStatusCode = Constants.DOWNLOAD_FILE_FAIL;
  799. log.error("File download failed url:{},Exception:",url,e);
  800. continue;
  801. }
  802. String delFileUrl = "";
  803. String uploadFileUrl = "";
  804. try {
  805. fileInfoMap.put("videoTime", "");
  806. fileInfoMap.put("resolution", "");
  807. String uploadResultJson = upLoadFile(downloadFilePath + getMd5(fileUrl) + fileType);
  808. JSONObject jsonObject = JSON.parseObject(uploadResultJson);
  809. String src = jsonObject.getString("src");
  810. String md5 = jsonObject.getString("md5");
  811. delFileUrl = gofastUrl.replace("/upload", "") + "/delete?md5=" + md5;
  812. uploadFileUrl = jsonObject.getString("url").replaceAll("\\?.*", "");;
  813. Long size = jsonObject.getLong("size");
  814. String printSize = getPrintSize(size);
  815. fileInfoMap.put("size", printSize);
  816. fileInfoMap.put("url", src);
  817. } catch (Exception e) {
  818. downloadStatusCode = Constants.UPLOAD_FILE_FAIL;
  819. log.error("File upload failed url:{},Exception:",url, e);
  820. e.printStackTrace();
  821. continue;
  822. }
  823. filePath.add(uploadFileUrl);
  824. delFileMap.put(uploadFileUrl, delFileUrl);
  825. delFileList.add(delFileMap);
  826. fileMap.put("gofastUrl", uploadFileUrl);
  827. fileMap.put("originalUrl", fileUrl);
  828. forwardUrl.add(fileMap);
  829. filePathSize.add(fileInfoMap);
  830. }
  831. returnMap.put("forwardUrl", forwardUrl);
  832. returnMap.put("filePath", filePath);
  833. returnMap.put("delFileList", delFileList);
  834. returnMap.put("downloadStatusCode", downloadStatusCode);
  835. resultData.put("ErroeMessage", downloadStatusCode);
  836. returnMap.put("filePathSize", filePathSize);
  837. resultData.put("forwardUrl", forwardUrl);
  838. resultData.put("filePath", filePath);
  839. resultData.put("delFileList", delFileList);
  840. resultData.put("filePathSize", filePathSize);
  841. return returnMap;
  842. }
  843. }