Java obtains remote files (guarantee the integrity of the file, and there will be no situation where it cannot be opened)

public class FileUtil {  
    /**  
     * Get remote files  
     * @param remoteFilePath remote file path  
     * @param localFilePath local file path  
     */  
    public void getFile(String remoteFilePath,String localFilePath){  
        URL urlfile = null;   
        HttpURLConnection httpUrl = null;   
        BufferedInputStream bis = null;   
        BufferedOutputStream bos = null;   
        File f = new File(localFilePath);  
        /*  
        //If you need to set the proxy  
        String proxy = "192.168.224.12";  
        String port = "8080";  
        Properties systemProperties = System.getProperties();  
        systemProperties.setProperty("http.proxyHost",proxy);  
        systemProperties.setProperty("http.proxyPort",port);*/  
        try{   
            urlfile = new URL(remoteFilePath);   
            httpUrl = (HttpURLConnection)urlfile.openConnection();   
            httpUrl.connect();   
            bis = new BufferedInputStream(httpUrl.getInputStream());  
            bos = new BufferedOutputStream(new FileOutputStream(f));  
            int len ​​= 2048;  
            byte [] b = new byte [len];   
            while((len=bis.read(b))!=-1) {   
                bos.write(b, 0, len);  
            }  
            bos.flush();   
            bis.close();  
            httpUrl.disconnect();  
            System.out.println("done~");  
        }catch(Exception e){   
        }   
    }  
}  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326460438&siteId=291194637