2010-02-20 17 views
6

का उपयोग कर यूआरएल पास करके फ़ाइल डाउनलोड करें मैं जावा में एक कोड लिखने की कोशिश कर रहा हूं जिसमें उपयोगकर्ता यूआरएल लिंक प्रदान करता है और प्रोग्राम यूआरएल लिंक लेता है और एक वेब पेज डाउनलोड करता है जैसा कि यह है और विशेष स्थान पर सहेजता है .. जैसा कि के रूप में सहेजें ... विकल्प वेबपेज पर उपलब्ध है।जावा कोड

कृपया किसी को भी मुझे अग्रिम

+1

क्या आप कह सकते हैं कि आपने अभी तक क्या किया है? आप किस बिंदु पर फंस गए हैं? –

उत्तर

5

आप जावा यूआरएल एपीआई का उपयोग कर सकते हैं तो यह से पढ़ सकते हैं और एक फ़ाइल पर उत्पादन धारा के माध्यम से लिखना यूआरएल पर एक इनपुट स्ट्रीम प्राप्त करने में मदद कर सकते हैं

धन्यवाद।

read data from url देखते हैं, Write to file

1

HtmlParser पर एक नज़र डालें। इसमें कुछ विशेषताएं हैं जो आपको वेब पेज से संसाधन निकालने में मदद करेंगी।

9

// नमूना URL: http://www.novell.com/coolsolutions/tools/downloads/ntradping.zip

import java.io.*; 
import java.net.*; 


public class UrlDownload { 
    final static int size = 1024; 

    public static void fileUrl(String fAddress, String localFileName, String destinationDir) { 
     OutputStream outStream = null; 
     URLConnection uCon = null; 

     InputStream is = null; 
     try { 
      URL url; 
      byte[] buf; 
      int byteRead, byteWritten = 0; 
      url = new URL(fAddress); 
      outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + "\\" + localFileName)); 

      uCon = url.openConnection(); 
      is = uCon.getInputStream(); 
      buf = new byte[size]; 
      while ((byteRead = is.read(buf)) != -1) { 
       outStream.write(buf, 0, byteRead); 
       byteWritten += byteRead; 
      } 
      System.out.println("Downloaded Successfully."); 
      System.out.println("File name:\"" + localFileName + "\"\nNo ofbytes :" + byteWritten); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
       outStream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    public static void fileDownload(String fAddress, String destinationDir) { 
     int slashIndex = fAddress.lastIndexOf('/'); 
     int periodIndex = fAddress.lastIndexOf('.'); 

     String fileName = fAddress.substring(slashIndex + 1); 

     if (periodIndex >= 1 && slashIndex >= 0 && slashIndex < fAddress.length() - 1) { 
      fileUrl(fAddress, fileName, destinationDir); 
     } else { 
      System.err.println("path or file name."); 
     } 
    } 

    public static void main(String[] args) { 
     if (args.length == 2) { 
      for (int i = 1; i < args.length; i++) { 
       fileDownload(args[i], args[0]); 
      } 
     } else { 
     } 
    } 
} 

यह पूरी तरह से काम कर रहा है।

+0

कृपया अपना कोड धो लें, शायद कथन में तर्क 'फ़ाइल डाउनलोड करें (तर्क [i], तर्क [0]);' बदला जाना चाहिए। – Daniele