Java 从服务器下载文件到本地

运行main方法即可运行。。。

  1. package com.Manage.control;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.DataInputStream;  
  5. import java.io.DataOutputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStreamReader;  
  9. import java.net.HttpURLConnection;  
  10. import java.net.MalformedURLException;  
  11. import java.net.URL;  
  12. import java.net.URLConnection;  
  13.   
  14. public class aatest {  
  15.     public static boolean saveUrlAs(String photoUrl, String fileName) {  
  16.         //此方法只能用户HTTP协议  
  17.             try {  
  18.               URL url = new URL(photoUrl);  
  19.               HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
  20.               DataInputStream in = new DataInputStream(connection.getInputStream());  
  21.               DataOutputStream out = new DataOutputStream(new FileOutputStream(fileName));  
  22.               byte[] buffer = new byte[4096];  
  23.               int count = 0;  
  24.               while ((count = in.read(buffer)) > 0) {  
  25.                 out.write(buffer, 0, count);  
  26.               }  
  27.               out.close();  
  28.               in.close();  
  29.               return true;  
  30.             }  
  31.             catch (Exception e) {  
  32.               return false;  
  33.             }  
  34.           }  
  35.            
  36.         public String getDocumentAt(String urlString) {  
  37.         //此方法兼容HTTP和FTP协议  
  38.             StringBuffer document = new StringBuffer();  
  39.             try {  
  40.               URL url = new URL(urlString);  
  41.               URLConnection conn = url.openConnection();  
  42.               BufferedReader reader = new BufferedReader(new InputStreamReader(conn.  
  43.                   getInputStream()));  
  44.               String line = null;  
  45.               while ( (line = reader.readLine()) != null) {  
  46.                 document.append(line + "/n");  
  47.               }  
  48.               reader.close();  
  49.             }  
  50.             catch (MalformedURLException e) {  
  51.               System.out.println("Unable to connect to URL: " + urlString);  
  52.             }  
  53.             catch (IOException e) {  
  54.               System.out.println("IOException when connecting to URL: " + urlString);  
  55.             }  
  56.             return document.toString();  
  57.           }  
  58.           
  59.         public static void main(String[] args) {  
  60.            
  61.               
  62.               String photoUrl = "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png";                                    
  63.               String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));  
  64.               String filePath = "d:/";  
  65.               boolean flag = saveUrlAs(photoUrl, filePath + fileName);  
  66.               System.out.println("Run ok!/n<BR>Get URL file " + flag);  
  67.                 
  68.         }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值