zoukankan      html  css  js  c++  java
  • Java下载HTTP URL链接示例

    这里以下载迅雷U享版为例。
    示例代码:

    package com.zifeiy.snowflake.handle.filesget;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    
    public class HttpFileGetter {
    	
    	public boolean download(String urlString, File localFile) {
    		try {
    			URL url = new URL(urlString);
    			URLConnection connection = url.openConnection();
    			connection.setConnectTimeout(5*1000);
    			InputStream inputStream = connection.getInputStream();
    			byte[] byteArr = new byte[1024];
    			int len;
    			File dir = localFile.getParentFile();
    			if (dir.exists() == false) 
    				dir.mkdirs();
    			OutputStream outputStream = new FileOutputStream(localFile);
    			while ((len = inputStream.read(byteArr)) != -1) {
    				outputStream.write(byteArr, 0, len);
    			}
    			outputStream.close();
    			inputStream.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    			return false;
    		}
    		
    		return true;
    	}
    	
    	// main for test
    	public static void main(String[] args) {
    		HttpFileGetter httpFileGetter = new HttpFileGetter();
    		httpFileGetter.download("http://down.sandai.net/ThunderVIP/ThunderVIP-xlgw.exe", new File("D:\test_download.exe"));
    	}
    }
    

    下载下来的程序运行也没有问题:

  • 相关阅读:
    阿里云CDN缓存加速学习总结
    阿里云SLB学习总结
    zabbix3.2安装
    drf中的增删改查接口
    drf中二次封装Response
    drf常用模块
    Django—auth模块
    csrf跨站请求伪造与CBV装饰器
    Django—cookie与session
    Django—中间件(待更新)
  • 原文地址:https://www.cnblogs.com/zifeiy/p/10175754.html
Copyright © 2011-2022 走看看