Browse Source

下载文件

wzy 5 months ago
parent
commit
0e2b859514

+ 7 - 0
mjava-lingmingguangzi/src/main/java/com/malk/lingmingguangzi/controller/LingmingguangziController.java

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
 
+import java.io.IOException;
 import java.util.Map;
 import java.util.Objects;
 
@@ -201,4 +202,10 @@ public class LingmingguangziController {
     public McR stopProcess(@RequestBody Map map) {
         return lingmingguangziService.stopProcess(map);
     }
+
+    @GetMapping("/testDownloadFile")
+    public McR downloadFile(String ydDownloadUrl,String path,String fileName) throws IOException {
+        lingmingguangziService.downloadFile(ydDownloadUrl,path,fileName);
+        return McR.success();
+    }
 }

+ 2 - 0
mjava-lingmingguangzi/src/main/java/com/malk/lingmingguangzi/service/LingmingguangziService.java

@@ -50,4 +50,6 @@ public interface LingmingguangziService {
     McR addContacts(Map map);
 
     McR stopProcess(Map map);
+
+    void downloadFile(String fileURL, String savePath , String fileName) throws IOException;
 }

+ 5 - 4
mjava-lingmingguangzi/src/main/java/com/malk/lingmingguangzi/service/LingmingguangziServiceImpl.java

@@ -534,6 +534,7 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
                 List<Map> fileList = (List<Map>) JSONArray.parse(String.valueOf(map1.get("attachmentField_ly8fxusz")));
                 String ydDownloadUrl = fileList.get(0).get("downloadUrl").toString();
                 String fileName = fileList.get(0).get("name").toString();
+                fileName = convertChineseToUtf8Hex(fileName);
                 if ("深圳".equals(type)){
                     //将downloadurl下载到本地
                     try {
@@ -621,7 +622,7 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
         mcBody.put("fileName",fileName);
         mcBody.put("downloadPath","/home/mc/mjs/lingmingguangzi/");
 
-        fileURL = UtilHttp.doPost("https://mc.cloudpure.cn/mc/api/request/downloadFile",null,null,mcBody);
+        fileURL = UtilHttp.doPost("https://mc.cloudpure.cn/api/mc/request/downloadFile",null,null,mcBody);
 
 //        fileURL = ((DDR_New) UtilHttp.doGet("https://api.dingtalk.com/v1.0/yida/apps/temporaryUrls/" + ydConf.getAppType(), ddClient.initTokenHeader(), param, DDR_New.class)).getResult().toString();
 
@@ -632,7 +633,7 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
         // 检查HTTP响应代码是否为200
         if (responseCode == HttpURLConnection.HTTP_OK) {
             InputStream inputStream = httpConn.getInputStream();
-            FileOutputStream outputStream = new FileOutputStream(savePath);
+            FileOutputStream outputStream = new FileOutputStream(savePath + fileName);
 
             byte[] buffer = new byte[4096];
             int bytesRead = -1;
@@ -931,9 +932,9 @@ public class LingmingguangziServiceImpl implements LingmingguangziService {
         MultipartEntityBuilder builder = MultipartEntityBuilder.create();
 
         //如果fileUrl中包含中文,则需要对其进行编码
-        String encodeFileName = convertChineseToUtf8Hex(file.getName());
+//        String encodeFileName = convertChineseToUtf8Hex(file.getName());
 
-        builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, encodeFileName);
+        builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, file.getName());
 
         // 设置请求体
         HttpEntity multipart = builder.build();