根据文件的url在网络上下载文件url.openConnection()

文章来源:


  1. URL url = new URL("http://127.0.0.1:8080/cfStruts2Ex2/");  
  2. URLConnection urlConn = url.openConnection();  
  3.   
  4. System.out.println("Date: " + new Date(urlConn.getDate()));  
  5. System.out.println("Content-Type: " + urlConn.getContentType());  
  6.   
  7. int length = urlConn.getContentLength();  
  8. System.out.println("Content-Lentgth: " + length);  
  9.   
  10. if (length > 0) {  
  11.     System.out.println("========== Content ==========");  
  12.     InputStream input = urlConn.getInputStream();  
  13.   
  14.     int i = length;  
  15.     int c;  
  16.     while ((c = input.read()) != -1 && --i > 0) {  
  17.         System.out.print((char) c);  
  18.     }  
  19.     input.close();  
  20. else {  
  21.     System.out.println("No Content.");  
  22. }  


你可能感兴趣的:(根据文件的url在网络上下载文件url.openConnection())