How to download and save a file from Internet using Java?

前端 未结 21 2881
情深已故
情深已故 2020-11-21 05:06

There is an online file (such as http://www.example.com/information.asp) I need to grab and save to a directory. I know there are several methods for grabbing a

相关标签:
21条回答
  • 2020-11-21 05:29

    This is another java7 variant based on Brian Risk's answer with usage of try-with statement:

    public static void downloadFileFromURL(String urlString, File destination) throws Throwable {
    
          URL website = new URL(urlString);
          try(
                  ReadableByteChannel rbc = Channels.newChannel(website.openStream());
                  FileOutputStream fos = new FileOutputStream(destination);  
                  ){
              fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
          }
    
      }
    
    0 讨论(0)
  • 2020-11-21 05:30

    You can do this in 1 line using netloader for Java:

    new NetFile(new File("my/zips/1.zip"), "https://example.com/example.zip", -1).load(); //returns true if succeed, otherwise false.
    
    0 讨论(0)
  • 2020-11-21 05:31
    public void saveUrl(final String filename, final String urlString)
            throws MalformedURLException, IOException {
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try {
            in = new BufferedInputStream(new URL(urlString).openStream());
            fout = new FileOutputStream(filename);
    
            final byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (fout != null) {
                fout.close();
            }
        }
    }
    

    You'll need to handle exceptions, probably external to this method.

    0 讨论(0)
  • 2020-11-21 05:31

    Below is the sample code to download movie from internet with java code:

    URL url = new 
    URL("http://103.66.178.220/ftp/HDD2/Hindi%20Movies/2018/Hichki%202018.mkv");
        BufferedInputStream bufferedInputStream = new  BufferedInputStream(url.openStream());
        FileOutputStream stream = new FileOutputStream("/home/sachin/Desktop/test.mkv");
    
    
        int count=0;
        byte[] b1 = new byte[100];
    
        while((count = bufferedInputStream.read(b1)) != -1) {
            System.out.println("b1:"+b1+">>"+count+ ">> KB downloaded:"+new File("/home/sachin/Desktop/test.mkv").length()/1024);
            stream.write(b1, 0, count);
        }
    
    0 讨论(0)
  • 2020-11-21 05:31

    If you are behind a proxy, you can set the proxies in java program as below:

            Properties systemSettings = System.getProperties();
            systemSettings.put("proxySet", "true");
            systemSettings.put("https.proxyHost", "https proxy of your org");
            systemSettings.put("https.proxyPort", "8080");
    

    If you are not behind a proxy, don't include the lines above in your code. Full working code to download a file when you are behind a proxy.

    public static void main(String[] args) throws IOException {
            String url="https://raw.githubusercontent.com/bpjoshi/fxservice/master/src/test/java/com/bpjoshi/fxservice/api/TradeControllerTest.java";
            OutputStream outStream=null;
            URLConnection connection=null;
            InputStream is=null;
            File targetFile=null;
            URL server=null;
            //Setting up proxies
            Properties systemSettings = System.getProperties();
                systemSettings.put("proxySet", "true");
                systemSettings.put("https.proxyHost", "https proxy of my organisation");
                systemSettings.put("https.proxyPort", "8080");
                //The same way we could also set proxy for http
                System.setProperty("java.net.useSystemProxies", "true");
                //code to fetch file
            try {
                server=new URL(url);
                connection = server.openConnection();
                is = connection.getInputStream();
                byte[] buffer = new byte[is.available()];
                is.read(buffer);
    
                    targetFile = new File("src/main/resources/targetFile.java");
                    outStream = new FileOutputStream(targetFile);
                    outStream.write(buffer);
            } catch (MalformedURLException e) {
                System.out.println("THE URL IS NOT CORRECT ");
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("Io exception");
                e.printStackTrace();
            }
            finally{
                if(outStream!=null) outStream.close();
            }
        }
    
    0 讨论(0)
  • 2020-11-21 05:34

    Downloading a file requires you to read it, either way you will have to go through the file in some way. Instead of line by line, you can just read it by bytes from the stream:

    BufferedInputStream in = new BufferedInputStream(new URL("http://www.website.com/information.asp").openStream())
        byte data[] = new byte[1024];
        int count;
        while((count = in.read(data,0,1024)) != -1)
        {
            out.write(data, 0, count);
        }
    
    0 讨论(0)
提交回复
热议问题