|
@@ -13,6 +13,10 @@ import com.fs.qwApi.Result.QwUploadResult;
|
|
|
import com.fs.qwApi.service.QwApiService;
|
|
|
import com.fs.voice.utils.StringUtil;
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@@ -21,6 +25,7 @@ import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
@@ -146,36 +151,61 @@ public class QwMaterialServiceImpl extends ServiceImpl<QwMaterialMapper, QwMater
|
|
|
*/
|
|
|
public static File urlToFile(String fileUrl) throws Exception {
|
|
|
|
|
|
- // 从URL中提取文件名,文件后缀
|
|
|
- String fileExtension = getFileExtension(new File(fileUrl).getName());
|
|
|
- String fileName = extractFileName(new URL(fileUrl))+new Date().getTime();
|
|
|
- File tempFile=null;
|
|
|
- if (!StringUtil.strIsNullOrEmpty(fileExtension)){
|
|
|
- // 创建一个临时文件
|
|
|
- tempFile = File.createTempFile(fileName, "."+fileExtension);
|
|
|
- }else {
|
|
|
- tempFile = File.createTempFile(fileName, null);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- // 使用 try-with-resources 语句自动关闭资源
|
|
|
- try (BufferedInputStream in = new BufferedInputStream(new URL(fileUrl).openStream());
|
|
|
- FileOutputStream out = new FileOutputStream(tempFile)) {
|
|
|
-
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
- int len;
|
|
|
+ // 编码文件名部分
|
|
|
+ String encodedFileUrl = fileUrl.replace(
|
|
|
+ new File(fileUrl).getName(),
|
|
|
+ URLEncoder.encode(new File(fileUrl).getName(), "UTF-8")
|
|
|
+ );
|
|
|
|
|
|
- // 读取数据并写入到临时文件
|
|
|
- while ((len = in.read(buffer)) != -1) {
|
|
|
- out.write(buffer, 0, len);
|
|
|
- }
|
|
|
- out.flush();
|
|
|
+ // 生成安全的临时文件名
|
|
|
+ String fileExtension = getFileExtension(new File(fileUrl).getName());
|
|
|
+ String safeFileName = "temp_" + System.currentTimeMillis();
|
|
|
+ File tempFile = File.createTempFile(safeFileName, "." + fileExtension);
|
|
|
+
|
|
|
+ // 使用 HttpClient 下载
|
|
|
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
|
|
|
+ HttpGet request = new HttpGet(encodedFileUrl);
|
|
|
+ request.setHeader("User-Agent", "Mozilla/5.0");
|
|
|
+ FileUtils.copyInputStreamToFile(
|
|
|
+ client.execute(request).getEntity().getContent(),
|
|
|
+ tempFile
|
|
|
+ );
|
|
|
} catch (IOException e) {
|
|
|
- // 处理异常,例如删除创建的临时文件
|
|
|
tempFile.delete();
|
|
|
- throw e;
|
|
|
+ throw new RuntimeException("下载文件失败: " + encodedFileUrl, e);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+// // 从URL中提取文件名,文件后缀
|
|
|
+// String fileExtension = getFileExtension(new File(fileUrl).getName());
|
|
|
+// String fileName = extractFileName(new URL(fileUrl))+new Date().getTime();
|
|
|
+// File tempFile=null;
|
|
|
+// if (!StringUtil.strIsNullOrEmpty(fileExtension)){
|
|
|
+// // 创建一个临时文件
|
|
|
+// tempFile = File.createTempFile(fileName, "."+fileExtension);
|
|
|
+// }else {
|
|
|
+// tempFile = File.createTempFile(fileName, null);
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// // 使用 try-with-resources 语句自动关闭资源
|
|
|
+// try (BufferedInputStream in = new BufferedInputStream(new URL(fileUrl).openStream());
|
|
|
+// FileOutputStream out = new FileOutputStream(tempFile)) {
|
|
|
+//
|
|
|
+// byte[] buffer = new byte[1024];
|
|
|
+// int len;
|
|
|
+//
|
|
|
+// // 读取数据并写入到临时文件
|
|
|
+// while ((len = in.read(buffer)) != -1) {
|
|
|
+// out.write(buffer, 0, len);
|
|
|
+// }
|
|
|
+// out.flush();
|
|
|
+// } catch (IOException e) {
|
|
|
+// // 处理异常,例如删除创建的临时文件
|
|
|
+// tempFile.delete();
|
|
|
+// throw e;
|
|
|
+// }
|
|
|
+
|
|
|
return tempFile;
|
|
|
}
|
|
|
|
|
@@ -255,6 +285,36 @@ public class QwMaterialServiceImpl extends ServiceImpl<QwMaterialMapper, QwMater
|
|
|
List<QwMaterial> qwMaterials = qwMaterialMapper.selectQwMaterialListByMediaIdList();
|
|
|
|
|
|
|
|
|
+// qwMaterials.forEach(item->{
|
|
|
+// // 将URL转换为文件
|
|
|
+// File materialFile = null;
|
|
|
+// try {
|
|
|
+// materialFile = urlToFile(item.getMaterialUrl());
|
|
|
+// } catch (Exception e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 上传素材到企业微信
|
|
|
+// QwUploadResult uploadResult = null;
|
|
|
+// try {
|
|
|
+// uploadResult = qwApiService.upload(
|
|
|
+// materialFile,
|
|
|
+// item.getMaterialType(),
|
|
|
+// item.getCorpId()
|
|
|
+// );
|
|
|
+// } catch (IOException e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+// }
|
|
|
+//
|
|
|
+// if (uploadResult.getErrCode() == 0) {
|
|
|
+// // 设置素材信息
|
|
|
+// item.setMaterialMediaId(uploadResult.getMediaId());
|
|
|
+// item.setCreatedAt(Long.valueOf(uploadResult.getCreatedAt()));
|
|
|
+// } else {
|
|
|
+// log.error("上传失败:{},{}", uploadResult.getErrMsg(), item);
|
|
|
+// }
|
|
|
+// });
|
|
|
+
|
|
|
// 创建线程安全的集合用于存储处理结果
|
|
|
ConcurrentLinkedQueue<QwMaterial> successQueue = new ConcurrentLinkedQueue<>();
|
|
|
|