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

前端 未结 21 2935
情深已故
情深已故 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:52
    public class DownloadManager {
    
        static String urls = "[WEBSITE NAME]";
    
        public static void main(String[] args) throws IOException{
            URL url = verify(urls);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream in = null;
            String filename = url.getFile();
            filename = filename.substring(filename.lastIndexOf('/') + 1);
            FileOutputStream out = new FileOutputStream("C:\\Java2_programiranje/Network/DownloadTest1/Project/Output" + File.separator + filename);
            in = connection.getInputStream();
            int read = -1;
            byte[] buffer = new byte[4096];
            while((read = in.read(buffer)) != -1){
                out.write(buffer, 0, read);
                System.out.println("[SYSTEM/INFO]: Downloading file...");
            }
            in.close();
            out.close();
            System.out.println("[SYSTEM/INFO]: File Downloaded!");
        }
        private static URL verify(String url){
            if(!url.toLowerCase().startsWith("http://")) {
                return null;
            }
            URL verifyUrl = null;
    
            try{
                verifyUrl = new URL(url);
            }catch(Exception e){
                e.printStackTrace();
            }
            return verifyUrl;
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:55

    Personally, I've found Apache's HttpClient to be more than capable of everything I've needed to do with regards to this. Here is a great tutorial on using HttpClient

    0 讨论(0)
  • 2020-11-21 05:56
    import java.io.*;
    import java.net.*;
    
    public class filedown {
        public static void download(String address, String localFileName) {
            OutputStream out = null;
            URLConnection conn = null;
            InputStream in = null;
    
            try {
                URL url = new URL(address);
                out = new BufferedOutputStream(new FileOutputStream(localFileName));
                conn = url.openConnection();
                in = conn.getInputStream();
                byte[] buffer = new byte[1024];
    
                int numRead;
                long numWritten = 0;
    
                while ((numRead = in.read(buffer)) != -1) {
                    out.write(buffer, 0, numRead);
                    numWritten += numRead;
                }
    
                System.out.println(localFileName + "\t" + numWritten);
            } 
            catch (Exception exception) { 
                exception.printStackTrace();
            } 
            finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                    if (out != null) {
                        out.close();
                    }
                } 
                catch (IOException ioe) {
                }
            }
        }
    
        public static void download(String address) {
            int lastSlashIndex = address.lastIndexOf('/');
            if (lastSlashIndex >= 0 &&
            lastSlashIndex < address.length() - 1) {
                download(address, (new URL(address)).getFile());
            } 
            else {
                System.err.println("Could not figure out local file name for "+address);
            }
        }
    
        public static void main(String[] args) {
            for (int i = 0; i < args.length; i++) {
                download(args[i]);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题