wulianwei %!s(int64=2) %!d(string=hai) anos
pai
achega
bac240cde9
Modificáronse 2 ficheiros con 50 adicións e 0 borrados
  1. BIN=BIN
      file/1.2.bin
  2. 50 0
      src/main/java/com/tuoren/remote/DownloadFileFromURL.java

BIN=BIN
file/1.2.bin


+ 50 - 0
src/main/java/com/tuoren/remote/DownloadFileFromURL.java

@@ -0,0 +1,50 @@
+package com.tuoren.remote;
+
+import java.io.BufferedInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.net.URL;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+
+public class DownloadFileFromURL {
+
+	public static void main(String[] args) {
+        String url = "https://tuoren-nb-prod.oss-cn-hangzhou.aliyuncs.com/2023-09-07/1694058842303.bin";
+
+        try {
+        	System.out.println("begin download");
+          //  downloadUsingNIO(url, "D:/users/maxsu/sitemap.xml");
+
+            downloadUsingStream(url, "D:/users/sitemap_stream.bin");
+            System.out.println("e download");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static void downloadUsingStream(String urlStr, String file) throws IOException{
+        URL url = new URL(urlStr);
+        BufferedInputStream bis = new BufferedInputStream(url.openStream());
+        FileOutputStream fis = new FileOutputStream(file);
+        byte[] buffer = new byte[1024];
+        int count=0;
+        while((count = bis.read(buffer,0,1024)) != -1)
+        {
+            fis.write(buffer, 0, count);
+        }
+        fis.close();
+        bis.close();
+    }
+
+    public static void downloadUsingNIO(String urlStr, String file) throws IOException {
+        URL url = new URL(urlStr);
+        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
+        FileOutputStream fos = new FileOutputStream(file);
+        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
+        fos.close();
+        rbc.close();
+    }
+
+
+}