附件补充下载
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

package com.bw.fileDownload.service;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.fileDownload.entity.Constants;
import com.bw.fileDownload.entity.FileInfoPo;
import com.bw.fileDownload.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
@Component
@Slf4j
public class DownloadExecService {
@Value("${file.download.path}")
private String downloadFilePath;
@Value("${file.upload.url}")
private String gofastUrl;
private static final String IMAGE_TYPE_JPG = "jpg";
private static final String IMAGE_TYPE_JPEG = "jpeg";
private static final String IMAGE_TYPE_PNG = "png";
private static OkHttpClient client = new OkHttpClient();
public Map downloadAndUploadVidoAndAudio(List<String> videoList, Map<String, Object> resultData) {
String header = "header";
Map<String, String> hederMap = new HashMap<>();
List<String> videoPath = new ArrayList<>();
List<Map> delVideoAndAudioList = new ArrayList<>();
Map<String, Object> returnMap = new HashMap<>(32);
List<Map> videoUrl = new ArrayList<>();
List<Map> videoPathSize = new ArrayList<>();
Integer downloadStatusCode = 0;
for (String url : videoList) {
Map delVideoAndAudioMap = new HashMap(32);
Map videoMap = new HashMap(32);
Map videoPathMap = new HashMap(32);
if (formatJudgment(url)) {
downloadStatusCode = Constants.URL_FORMAT;
continue;
}
String[] split = url.split("###");
String videoType = split[1];
String fileType = videoType;
String m3u8Type = ".m3u8";
String youtubeTypeFr = "youtube";
String youtubeTypeSec = "youtu.be";
String needDownloadVideoUrl = split[0];
try {
if (!StringUtil.hasValue(needDownloadVideoUrl)) {
downloadStatusCode = Constants.VIDEO_LIST_NULL;
continue;
}
if (m3u8Type.equals(fileType)) {
downLoadVideoExeCommandForFfmpeg(needDownloadVideoUrl);
} else if (url.contains(youtubeTypeFr) || url.contains(youtubeTypeSec)) {
downLoadVideoExeCommandLp(needDownloadVideoUrl, videoType);
} else {
File file = new File(downloadFilePath + getMd5(needDownloadVideoUrl).concat(fileType));
if (resultData.containsKey(header)) {
hederMap = (Map<String, String>) resultData.get(header);
downloadFile(needDownloadVideoUrl, downloadFilePath, getMd5(needDownloadVideoUrl).concat(fileType), hederMap);
} else {
downloadFile(needDownloadVideoUrl, downloadFilePath, getMd5(needDownloadVideoUrl).concat(fileType));
if (!file.exists() || file.length() < 100) {
downLoadFileExeCommandForWGet(needDownloadVideoUrl, fileType);
}
}
}
} catch (Exception e) {
downloadStatusCode = Constants.DOWNLOAD_VIDEO_FAIL;
log.error("Video or audio download failed url:{},Exception:",url,e);
continue;
}
String delVideoAndAudioUrl = "";
String uploadFileUrl = "";
try {
Map<String, String> videoTimeMap = getVideoTime(downloadFilePath + getMd5(needDownloadVideoUrl) + videoType);
String videoTime = videoTimeMap.get("videoTime");
String resolution = videoTimeMap.get("resolution");
videoPathMap.put("videoTime", videoTime);
videoPathMap.put("resolution", resolution);
String uploadResultJson = "";
if (m3u8Type.equals(fileType)) {
uploadResultJson = upLoadFile(downloadFilePath + getMd5(needDownloadVideoUrl) + ".mp4");
} else {
uploadResultJson = upLoadFile(downloadFilePath + getMd5(needDownloadVideoUrl) + videoType);
}
JSONObject jsonObject = JSON.parseObject(uploadResultJson);
String md5 = jsonObject.getString("md5");
delVideoAndAudioUrl = gofastUrl.replace("/upload", "") + "/delete?md5=" + md5;
uploadFileUrl = jsonObject.getString("url").replaceAll("\\?.*", "");
String src = jsonObject.getString("src");
Long size = jsonObject.getLong("size");
String printSize = getPrintSize(size);
videoPathMap.put("size", printSize);
videoPathMap.put("url", src);
} catch (Exception e) {
downloadStatusCode = Constants.UPLOAD_VIDEO_FAIL;
log.error("Video upload failed url:{},Exception:",url,e);
continue;
}
videoPath.add(uploadFileUrl);
delVideoAndAudioMap.put(uploadFileUrl, delVideoAndAudioUrl);
delVideoAndAudioList.add(delVideoAndAudioMap);
videoMap.put("originalUrl", needDownloadVideoUrl);
videoMap.put("gofastUrl", uploadFileUrl);
videoUrl.add(videoMap);
videoPathSize.add(videoPathMap);
}
returnMap.put("videoUrl", videoUrl);
returnMap.put("videoPath", videoPath);
returnMap.put("delVideoAndAudioList", delVideoAndAudioList);
returnMap.put("downloadStatusCode", downloadStatusCode);
resultData.put("ErroeMessage", downloadStatusCode);
returnMap.put("videoPathSize", videoPathSize);
resultData.put("videoUrl", videoUrl);
resultData.put("videoPath", videoPath);
resultData.put("delVideoAndAudioList", delVideoAndAudioList);
resultData.put("videoPathSize", videoPathSize);
return returnMap;
}
private boolean formatJudgment(String url) {
String formatTag = "###";
if (!url.contains(formatTag)) {
return true;
}
return false;
}
private void downLoadVideoExeCommandForFfmpeg(String videoUrl) {
String news_id = getMd5(videoUrl);
String command = "ffmpeg -i " + videoUrl + " " + downloadFilePath
+ news_id + ".mp4";
InputStream in = null;
BufferedReader read = null;
String Qavg = "Qavg:";
try {
System.out.println("DownloadUtil" + command);
Process process;
try {
String line = "";
File file = new File(downloadFilePath);
if (!file.exists()) {
file.mkdirs();
}
process = Runtime.getRuntime().exec(command);
in = process.getErrorStream();
read = new BufferedReader(new InputStreamReader(in));
while ((line = read.readLine()) != null) {
System.out.println("DownloadUtil" + line);
if (line.contains(Qavg)) {
break;
}
}
process.destroy();
} catch (IOException var26) {
var26.printStackTrace();
}
} finally {
try {
in.close();
read.close();
} catch (IOException var25) {
var25.printStackTrace();
}
}
}
private void downLoadVideoExeCommandLp(String videoUrl, String videoType) {
String command = "";
List<String> stringList = doGetVideoIdCommand(videoUrl);
for (String s : stringList) {
String videoId = s.split(" ")[0];
command = "yt_dlp --no-check-certificate -f " + videoId + " -o " + downloadFilePath + getMd5(videoUrl) + videoType + " " + videoUrl;
log.info("视频下载命令:" + command);
break;
}
InputStream in = null;
BufferedReader read = null;
try {
Process process;
try {
String line = "";
File file = new File(downloadFilePath);
if (!file.exists()) {
file.mkdirs();
}
process = Runtime.getRuntime().exec(command, (String[]) null, file);
in = process.getInputStream();
read = new BufferedReader(new InputStreamReader(in));
while ((line = read.readLine()) != null) {
System.out.println(line);
}
process.destroy();
} catch (IOException var26) {
var26.printStackTrace();
}
} finally {
try {
in.close();
read.close();
} catch (IOException var25) {
var25.printStackTrace();
}
}
}
private void downLoadFileExeCommandForWGet(String fileUrl, String filetype) {
String news_id = getMd5(fileUrl);
String htpps = "https";
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;
if (!fileUrl.contains(htpps)) {
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;
}
InputStream in = null;
BufferedReader read = null;
try {
log.info("wget 方法进入:" + command);
Process process;
try {
String line = "";
process = Runtime.getRuntime().exec(command);
log.info("执行命令");
log.info("视频下载命令:" + command);
in = process.getErrorStream();
read = new BufferedReader(new InputStreamReader(in));
while ((line = read.readLine()) != null) {
log.info("视频下载进度:" + line);
}
process.destroy();
} catch (IOException var26) {
var26.printStackTrace();
log.info("下载视频异常", var26);
}
} finally {
try {
in.close();
read.close();
} catch (IOException var25) {
var25.printStackTrace();
}
}
}
public String getMd5(String string) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bs = md5.digest(string.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder(40);
for (byte x : bs) {
if ((x & 0xff) >> 4 == 0) {
sb.append("0").append(Integer.toHexString(x & 0xff));
} else {
sb.append(Integer.toHexString(x & 0xff));
}
}
return sb.toString();
} catch (Exception e) {
log.info("get md 5 exception:", e);
return "nceaform" + System.currentTimeMillis();
}
}
private List<String> doGetVideoIdCommand(String videoUrl) {
List resultList = new ArrayList();
String command = "yt_dlp --no-check-certificate -F " + videoUrl;
InputStream in = null;
BufferedReader read = null;
try {
Process process;
try {
String line = "";
process = Runtime.getRuntime().exec(command, (String[]) null);
in = process.getInputStream();
read = new BufferedReader(new InputStreamReader(in, "gbk"));
while ((line = read.readLine()) != null) {
System.out.println(line);
if (line.contains("mp4")) {
if (line.contains("256x144") && !line.contains("video only")) {
resultList.add(line);
break;
} else if (line.contains("640x360") && !line.contains("video only")) {
resultList.add(line);
break;
} else if (line.contains("854x480") && !line.contains("video only")) {
resultList.add(line);
break;
} else if (line.contains("1280x720") && !line.contains("video only")) {
resultList.add(line);
break;
}
}
}
process.destroy();
} catch (IOException var26) {
var26.printStackTrace();
}
} finally {
try {
in.close();
read.close();
} catch (IOException var25) {
var25.printStackTrace();
}
}
return resultList;
}
public void downloadFile(String fileUrl, String savePath, String customFileName, Map<String, String>... headers) throws IOException {
okhttp3.Request.Builder builder = new okhttp3.Request.Builder();
if (headers != null && headers.length > 0) {
Map<String, String> tempHeaders = headers[0];
Iterator<String> iterator = tempHeaders.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
builder.addHeader(key, tempHeaders.get(key));
}
} else {
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\"");
}
Request request = builder.url(fileUrl).get().build();
Call call = client.newCall(request);
Response response = call.execute();
if (!response.isSuccessful()) {
throw new IOException("Failed to download file: " + response);
}
ResponseBody responseBody = response.body();
if (responseBody != null) {
InputStream inputStream = responseBody.byteStream();
File fileDirectory = new File(savePath);
if (!fileDirectory.exists()) {
fileDirectory.mkdirs();
}
File file = new File(fileDirectory, customFileName);
FileOutputStream outputStream = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
}
private Map<String, String> getVideoTime(String video_path) {
Map infoMap = new HashMap(32);
String videoTime = "";
String resolution = "";
String command = "ffmpeg -i " + video_path;
InputStream in = null;
BufferedReader read = null;
try {
String info = "";
String line = "";
System.out.println("视频时间解析命令" + command);
Process process;
process = Runtime.getRuntime().exec(command);
in = process.getErrorStream();
read = new BufferedReader(new InputStreamReader(in));
while ((line = read.readLine()) != null) {
info = info + line;
}
process.destroy();
String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
Pattern pattern = Pattern.compile(regexDuration);
Matcher m = pattern.matcher(info);
if (m.find()) {
videoTime = getTimelen(m.group(1)) + "s";
System.out.println("视频时长:" + videoTime + "s , 开始时间:" + m.group(2) + ", 比特率:" + m.group(3) + "kb/s");
}
String regexVideo = "Video: (.*?), (.*?), (\\d+x\\d+)[,\\s]";
pattern = Pattern.compile(regexVideo);
m = pattern.matcher(info);
if (m.find()) {
resolution = m.group(3);
System.out.println("编码格式:" + m.group(1) + ", 视频格式:" + m.group(2) + ", 分辨率:" + resolution + "kb/s");
}
} catch (Exception e) {
e.printStackTrace();
}
infoMap.put("videoTime", videoTime);
infoMap.put("resolution", resolution);
return infoMap;
}
private String upLoadFile(String filePath) {
File file = new File(filePath);
String realFilename = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"file\";filename=\"" + realFilename + "\""),
RequestBody.create(MediaType.parse("image/png"), file)
).addFormDataPart("output", "json").build();
RequestBody body = builder.build();
Request request = new Request.Builder().url(gofastUrl).post(body).header("Expect", "100-continue").build();
OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
OkHttpClient client = okBuilder.connectTimeout(600, TimeUnit.MILLISECONDS)
.readTimeout(600, TimeUnit.SECONDS).build();
Call call = client.newCall(request);
String html = "";
Response response = null;
try {
response = call.execute();
html = response.body().string();
System.out.println("DownloadUtil" + html);
} catch (IOException e) {
log.info("upload fail:" + filePath);
e.printStackTrace();
} finally {
response.close();
}
file.delete();
return html;
}
public String getPrintSize(long size) {
BigDecimal bigDecimal = new BigDecimal(size);
BigDecimal bigDecimal1 = new BigDecimal(1024);
BigDecimal divide = bigDecimal.divide(bigDecimal1, 2, BigDecimal.ROUND_HALF_UP);
return divide.toString() + "KB";
}
private String getTimelen(String timelen) {
int min = 0;
String index = "0";
int two = 2;
String[] strs = timelen.split(":");
if (strs[0].compareTo(index) > 0) {
min += Integer.valueOf(strs[0]) * 60 * 60;
}
if (strs[1].compareTo(index) > 0) {
min += Integer.valueOf(strs[1]) * 60;
}
if (strs[two].compareTo(index) > 0) {
min += Math.round(Float.valueOf(strs[2]));
}
return String.valueOf(min);
}
public Map DownloadAndUploadImg(List<String> imgList, Map<String, Object> resultData) {
String header = "header";
Map<String, String> hederMap = new HashMap<>();
String cid = (String) resultData.get("cid");
List<String> imgPath = new ArrayList<>();
List<Map> delimgList = new ArrayList<>();
Map<String, Object> returnMap = new HashMap<>(32);
List<Map> imagePathSize = new ArrayList<>();
Map contentimgs = new HashMap<>(32);
int imgTag = 1;
Integer downloadStatusCode = 0;
for (String url : imgList) {
if (StringUtil.checkDoubleHttp(url)) {
log.info("Download url have many http:" + url + ",cid:" + cid);
continue;
}
Map delFileMap = new HashMap(32);
Map imgMap = new HashMap(32);
Map imagePathMap = new HashMap(32);
if (formatJudgment(url)) {
downloadStatusCode = Constants.URL_FORMAT;
log.info("Download url splicing error url:" + url + ",cid:" + cid);
continue;
}
String[] split = url.split("###");
String fileUrl = split[0];
String fileType = split[1];
try {
if (!StringUtil.hasValue(fileUrl)) {
downloadStatusCode = Constants.IMAGE_LIST_NULL;
continue;
}
File file = new File(downloadFilePath + getMd5(fileUrl).concat(fileType));
if (resultData.containsKey(header)) {
hederMap = (Map<String, String>) resultData.get(header);
downloadFile(fileUrl, downloadFilePath, getMd5(fileUrl).concat(fileType), hederMap);
} else {
downloadFile(fileUrl, downloadFilePath, getMd5(fileUrl).concat(fileType));
if (!file.exists() || file.length() < 100) {
downLoadFileExeCommandForWGet(fileUrl, fileType);
}
}
} catch (Exception e) {
downloadStatusCode = Constants.DOWNLOAD_IMAGE_FAIL;
log.info("Image download failed url:{},Exception:",url, e);
e.printStackTrace();
continue;
}
String delimgUrl = "";
String uploadimgUrl = "";
try {
String uploadResultJson = upLoadFile(downloadFilePath + getMd5(fileUrl) + fileType);
JSONObject jsonObject = JSON.parseObject(uploadResultJson);
String md5 = jsonObject.getString("md5");
delimgUrl = gofastUrl.replace("/upload", "") + "/delete?md5=" + md5;
uploadimgUrl = jsonObject.getString("url").replaceAll("\\?.*", "");;
Long size = jsonObject.getLong("size");
imagePathMap.put("videoTime", "");
try {
String imgWidthAndHeight = getImgWidthAndHeight(downloadFilePath + getMd5(fileUrl) + fileType);
String printSize = getPrintSize(size);
String src = jsonObject.getString("src");
imagePathMap.put("resolution", imgWidthAndHeight);
imagePathMap.put("size", printSize);
imagePathMap.put("url", src);
} catch (Exception e) {
String printSize = getPrintSize(size);
String src = jsonObject.getString("src");
imagePathMap.put("resolution", "100x100");
imagePathMap.put("size", printSize);
imagePathMap.put("url", src);
log.error("Image size calculation failed url:{},Exception:",url, e);
}
} catch (Exception e) {
downloadStatusCode = Constants.UPLOAD_IMAGE_FAIL;
log.error("Image upload failed url:{},Exception:",url, e);
continue;
}
imgPath.add(uploadimgUrl);
delFileMap.put(uploadimgUrl, delimgUrl);
delimgList.add(delFileMap);
imgMap.put("img", fileUrl);
imgMap.put("rawimg", fileUrl);
imgMap.put("imgtag", "img" + imgTag);
imgMap.put("uploadImg", uploadimgUrl);
contentimgs.put("img" + imgTag, imgMap);
imagePathSize.add(imagePathMap);
imgTag++;
}
returnMap.put("contentimgs", contentimgs);
returnMap.put("imagePath", imgPath);
returnMap.put("delimgList", delimgList);
returnMap.put("downloadStatusCode", downloadStatusCode);
resultData.put("ErroeMessage", downloadStatusCode);
returnMap.put("imagePathSize", imagePathSize);
resultData.put("contentimgs", contentimgs);
resultData.put("imagePath", imgPath);
resultData.put("delimgList", delimgList);
resultData.put("imagePathSize", imagePathSize);
return returnMap;
}
public Map DownloadAndUploadImgNoUa(List<String> imgList, Map<String, Object> resultData) {
String cid = (String) resultData.get("cid");
List<String> imgPath = new ArrayList<>();
List<Map> delimgList = new ArrayList<>();
Map<String, Object> returnMap = new HashMap<>(32);
List<Map> imagePathSize = new ArrayList<>();
Map contentimgs = new HashMap<>(32);
int imgTag = 1;
Integer downloadStatusCode = 0;
for (String url : imgList) {
if (StringUtil.checkDoubleHttp(url)) {
log.info("Download url have many http:" + url + ",cid:" + cid);
continue;
}
Map delFileMap = new HashMap(32);
Map imgMap = new HashMap(32);
Map imagePathMap = new HashMap(32);
if (formatJudgment(url)) {
downloadStatusCode = Constants.URL_FORMAT;
log.info("Download url splicing error url:" + url + ",cid:" + cid);
continue;
}
String[] split = url.split("###");
String fileUrl = split[0];
String fileType = split[1];
try {
if (!StringUtil.hasValue(fileUrl)) {
downloadStatusCode = Constants.IMAGE_LIST_NULL;
continue;
}
downLoadFileExeCommandForWGetNoUa(fileUrl, fileType);
} catch (Exception e) {
downloadStatusCode = Constants.DOWNLOAD_IMAGE_FAIL;
log.info("Image download failed url:{},Exception:",url, e);
continue;
}
String delimgUrl = "";
String uploadimgUrl = "";
try {
String uploadResultJson = upLoadFile(downloadFilePath + getMd5(fileUrl) + fileType);
JSONObject jsonObject = JSON.parseObject(uploadResultJson);
String md5 = jsonObject.getString("md5");
delimgUrl = gofastUrl.replace("/upload", "") + "/delete?md5=" + md5;
uploadimgUrl = jsonObject.getString("url").replaceAll("\\?.*", "");;
Long size = jsonObject.getLong("size");
imagePathMap.put("videoTime", "");
try {
String imgWidthAndHeight = getImgWidthAndHeight(downloadFilePath + getMd5(fileUrl) + fileType);
String printSize = getPrintSize(size);
String src = jsonObject.getString("src");
imagePathMap.put("resolution", imgWidthAndHeight);
imagePathMap.put("size", printSize);
imagePathMap.put("url", src);
} catch (Exception e) {
String printSize = getPrintSize(size);
String src = jsonObject.getString("src");
imagePathMap.put("resolution", "100x100");
imagePathMap.put("size", printSize);
imagePathMap.put("url", src);
log.info("Image size calculation failed url:" + url + ",Exception:" + e);
}
} catch (Exception e) {
downloadStatusCode = Constants.UPLOAD_IMAGE_FAIL;
log.error("Image upload failed url:{},Exception:",url, e);
continue;
}
imgPath.add(uploadimgUrl);
delFileMap.put(uploadimgUrl, delimgUrl);
delimgList.add(delFileMap);
imgMap.put("img", fileUrl);
imgMap.put("rawimg", fileUrl);
imgMap.put("imgtag", "img" + imgTag);
imgMap.put("uploadImg", uploadimgUrl);
contentimgs.put("img" + imgTag, imgMap);
imagePathSize.add(imagePathMap);
imgTag++;
}
returnMap.put("contentimgs", contentimgs);
returnMap.put("imagePath", imgPath);
returnMap.put("delimgList", delimgList);
returnMap.put("downloadStatusCode", downloadStatusCode);
resultData.put("ErroeMessage", downloadStatusCode);
returnMap.put("imagePathSize", imagePathSize);
resultData.put("contentimgs", contentimgs);
resultData.put("imagePath", imgPath);
resultData.put("delimgList", delimgList);
resultData.put("imagePathSize", imagePathSize);
return returnMap;
}
public String getImgWidthAndHeight(String filePath) {
File file = new File(filePath);
InputStream is = null;
BufferedImage src = null;
int width = -1;
int height = -1;
try {
is = new FileInputStream(file);
src = javax.imageio.ImageIO.read(is);
width = src.getWidth(null);
height = src.getHeight(null);
is.close();
} catch (IOException e) {
}
return width + "x" + height;
}
private void downLoadFileExeCommandForWGetNoUa(String fileUrl, String filetype) {
String news_id = getMd5(fileUrl);
//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
String command = "wget --no-check-certificate " + fileUrl + " -O " + downloadFilePath + news_id + filetype;
InputStream in = null;
BufferedReader read = null;
try {
log.info("wget 方法进入:" + command);
Process process;
try {
String line = "";
process = Runtime.getRuntime().exec(command);
log.info("执行命令");
log.info("视频下载命令:" + command);
in = process.getErrorStream();
read = new BufferedReader(new InputStreamReader(in));
while ((line = read.readLine()) != null) {
log.info("视频下载进度:" + line);
}
process.destroy();
} catch (IOException var26) {
var26.printStackTrace();
log.info("下载视频异常", var26);
}
} finally {
try {
in.close();
read.close();
} catch (IOException var25) {
var25.printStackTrace();
}
}
}
public Map DownloadAndUploadFile(List<String> fileList, Map<String, Object> resultData) {
String header = "header";
Map<String, String> hederMap = new HashMap<>();
List<String> filePath = new ArrayList<>();
List<Map> delFileList = new ArrayList<>();
Map<String, Object> returnMap = new HashMap<>(32);
List<Map> forwardUrl = new ArrayList<>();
List<Map> filePathSize = new ArrayList<>();
Integer downloadStatusCode = 0;
for (String url : fileList) {
Map fileInfoMap = new HashMap(32);
Map delFileMap = new HashMap(32);
Map fileMap = new HashMap(32);
if (formatJudgment(url)) {
downloadStatusCode = Constants.URL_FORMAT;
continue;
}
String[] split = url.split("###");
String fileUrl = split[0];
String fileType = split[1];
try {
if (!StringUtil.hasValue(fileUrl)) {
downloadStatusCode = Constants.IMAGE_LIST_NULL;
continue;
}
File file = new File(downloadFilePath + getMd5(fileUrl).concat(fileType));
if (resultData.containsKey(header)) {
hederMap = (Map<String, String>) resultData.get(header);
downloadFile(fileUrl, downloadFilePath, getMd5(fileUrl).concat(fileType), hederMap);
} else {
downloadFile(fileUrl, downloadFilePath, getMd5(fileUrl).concat(fileType));
if (!file.exists() || file.length() < 100) {
downLoadFileExeCommandForWGet(fileUrl, fileType);
}
}
} catch (Exception e) {
downloadStatusCode = Constants.DOWNLOAD_FILE_FAIL;
log.error("File download failed url:{},Exception:",url,e);
continue;
}
String delFileUrl = "";
String uploadFileUrl = "";
try {
fileInfoMap.put("videoTime", "");
fileInfoMap.put("resolution", "");
String uploadResultJson = upLoadFile(downloadFilePath + getMd5(fileUrl) + fileType);
JSONObject jsonObject = JSON.parseObject(uploadResultJson);
String src = jsonObject.getString("src");
String md5 = jsonObject.getString("md5");
delFileUrl = gofastUrl.replace("/upload", "") + "/delete?md5=" + md5;
uploadFileUrl = jsonObject.getString("url").replaceAll("\\?.*", "");;
Long size = jsonObject.getLong("size");
String printSize = getPrintSize(size);
fileInfoMap.put("size", printSize);
fileInfoMap.put("url", src);
} catch (Exception e) {
downloadStatusCode = Constants.UPLOAD_FILE_FAIL;
log.info("File upload failed url:{},Exception:",url,e);
continue;
}
filePath.add(uploadFileUrl);
delFileMap.put(uploadFileUrl, delFileUrl);
delFileList.add(delFileMap);
fileMap.put("gofastUrl", uploadFileUrl);
fileMap.put("originalUrl", fileUrl);
forwardUrl.add(fileMap);
filePathSize.add(fileInfoMap);
}
returnMap.put("forwardUrl", forwardUrl);
returnMap.put("filePath", filePath);
returnMap.put("delFileList", delFileList);
returnMap.put("downloadStatusCode", downloadStatusCode);
resultData.put("ErroeMessage", downloadStatusCode);
returnMap.put("filePathSize", filePathSize);
resultData.put("forwardUrl", forwardUrl);
resultData.put("filePath", filePath);
resultData.put("delFileList", delFileList);
resultData.put("filePathSize", filePathSize);
return returnMap;
}
public Map DownloadAndUploadFileNoUa(List<String> fileList, Map<String, Object> resultData) {
List<String> filePath = new ArrayList<>();
List<Map> delFileList = new ArrayList<>();
Map<String, Object> returnMap = new HashMap<>(32);
List<Map> forwardUrl = new ArrayList<>();
List<Map> filePathSize = new ArrayList<>();
Integer downloadStatusCode = 0;
for (String url : fileList) {
Map fileInfoMap = new HashMap(32);
Map delFileMap = new HashMap(32);
Map fileMap = new HashMap(32);
if (formatJudgment(url)) {
downloadStatusCode = Constants.URL_FORMAT;
continue;
}
String[] split = url.split("###");
String fileUrl = split[0];
String fileType = split[1];
try {
if (!StringUtil.hasValue(fileUrl)) {
downloadStatusCode = Constants.IMAGE_LIST_NULL;
continue;
}
downLoadFileExeCommandForWGetNoUa(fileUrl, fileType);
} catch (Exception e) {
downloadStatusCode = Constants.DOWNLOAD_FILE_FAIL;
log.error("File download failed url:{},Exception:",url,e);
continue;
}
String delFileUrl = "";
String uploadFileUrl = "";
try {
fileInfoMap.put("videoTime", "");
fileInfoMap.put("resolution", "");
String uploadResultJson = upLoadFile(downloadFilePath + getMd5(fileUrl) + fileType);
JSONObject jsonObject = JSON.parseObject(uploadResultJson);
String src = jsonObject.getString("src");
String md5 = jsonObject.getString("md5");
delFileUrl = gofastUrl.replace("/upload", "") + "/delete?md5=" + md5;
uploadFileUrl = jsonObject.getString("url").replaceAll("\\?.*", "");;
Long size = jsonObject.getLong("size");
String printSize = getPrintSize(size);
fileInfoMap.put("size", printSize);
fileInfoMap.put("url", src);
} catch (Exception e) {
downloadStatusCode = Constants.UPLOAD_FILE_FAIL;
log.error("File upload failed url:{},Exception:",url, e);
e.printStackTrace();
continue;
}
filePath.add(uploadFileUrl);
delFileMap.put(uploadFileUrl, delFileUrl);
delFileList.add(delFileMap);
fileMap.put("gofastUrl", uploadFileUrl);
fileMap.put("originalUrl", fileUrl);
forwardUrl.add(fileMap);
filePathSize.add(fileInfoMap);
}
returnMap.put("forwardUrl", forwardUrl);
returnMap.put("filePath", filePath);
returnMap.put("delFileList", delFileList);
returnMap.put("downloadStatusCode", downloadStatusCode);
resultData.put("ErroeMessage", downloadStatusCode);
returnMap.put("filePathSize", filePathSize);
resultData.put("forwardUrl", forwardUrl);
resultData.put("filePath", filePath);
resultData.put("delFileList", delFileList);
resultData.put("filePathSize", filePathSize);
return returnMap;
}
}