1 2 3 4 5 | import java.io.IOException; import java.net.ConnectException; import java.net.URL; import java.io.InputStream; import java.io.FileOutputStream; |
1 2 3 4 5 6 7 8 | public static void download(String source, String destination) throws IOException, ConnectException{ InputStream is = new URL(source).openConnection().getInputStream(); FileOutputStream fos = new FileOutputStream(destination); byte [] buffer = new byte [ 1024 ]; for ( int length; (length = is.read(buffer)) > 0 ; fos.write(buffer, 0 , length)); fos.close(); is.close(); } |
此方法亦可以下載 FTP 的檔案,若 FTP 伺服器需要登入身份可利用 FTP URL 格式來登入伺服器
例如 ftp://username:password@www.url.com:port/file.ext
This method can download files in FTP, if FTP Server require login identify to use FTP URL format to login the server
for example ftp://username:password@www.url.com:port/file.ext
沒有留言 :
張貼留言