关于Java中Http下载的一些整理

/**
	 * 下载小图
	 * @param img
	 */
	public String download(String file){
		DataInputStream is = null;
		DataOutputStream os = null;
		String p = "";
		HttpURLConnection con = null;
		try {
			URL url = new URL(file);
			con = (HttpURLConnection) url.openConnection();
			URL path = Thread.currentThread().getContextClassLoader().getResource("");
			p = path.getPath();
			String[] strs = url.getFile().split("/");
			p = p.replace("WEB-INF/classes/", "") + "images/" + strs[strs.length-1];
			p = p.substring(1);
			File img11 = new File(p);
			if(!img11.exists()){
				img11.createNewFile();
			}
			is = new DataInputStream(con.getInputStream());
			os = new DataOutputStream(new FileOutputStream(p));
			byte[] buffer = new byte[51200];
			int count = 0;
			while(((count = is.read(buffer)) > 0)){
				os.write(buffer, 0, count);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				is.close();
				os.close();
				con.disconnect();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return p;
		}
	}
原文地址:https://www.cnblogs.com/wpcnblog/p/2112652.html