Browse Source

文件下载

wang_xy 1 year ago
parent
commit
bc22374606
1 changed files with 26 additions and 1 deletions
  1. 26 1
      ruoyi-gas/src/main/java/com/ruoyi/gas/utils/FileInstallUtils.java

+ 26 - 1
ruoyi-gas/src/main/java/com/ruoyi/gas/utils/FileInstallUtils.java

@@ -8,8 +8,12 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.io.BufferedInputStream;
 import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.util.List;
 
 @Service
@@ -51,12 +55,15 @@ public class FileInstallUtils {
                   createNamefile.mkdirs();
               }
               try {
-                  obsService.download(createNamefile.getPath(), photo.substring(photo.lastIndexOf("/")+1));
+//                  obsService.download(createNamefile.getPath(), photo.substring(photo.lastIndexOf("/")+1));
+                  downloadFile(createNamefile.getPath(), photo);
                   System.out.println(photo.substring(photo.lastIndexOf("/")+1));
                   System.out.println(photo);
                   System.out.println(createNamefile.getPath());
               } catch (IOException e) {
                   e.printStackTrace();
+              } catch (Exception e) {
+                  throw new RuntimeException(e);
               }
           }
 
@@ -69,4 +76,22 @@ public class FileInstallUtils {
     return  filePath+"/"+zipFileBoList.get(0).getName();
 
     }
+    public void downloadFile(String savedir,String fileUrl) throws Exception{
+        URL url = new URL(fileUrl);
+        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
+        conn.connect();
+        BufferedInputStream br = new BufferedInputStream(conn.getInputStream());
+        File f = new File(savedir);
+        if(!f.exists()){ f.mkdirs(); }
+        FileOutputStream fos = new FileOutputStream(savedir + fileUrl.substring(fileUrl.lastIndexOf("/")));
+        byte[] buf = new byte[1024];
+        int size = 0 ;
+        while((size = br.read(buf) )!= -1){
+            fos.write(buf,0,size);
+        }
+        br.close();
+        fos.close();
+        conn.disconnect();
+    }
+
 }