package test;
import java.io.*;
import java.net.*;
public class DownloadUtil {
	
// 下载指定路径图片
public static boolean downloadImg(String netImg,String filePath){
	//第一步,创建网络连接
	try {
		URL url = new URL(netImg);
	//第二步,打开连接
		URLConnection urlconnection = url.openConnection();
		//第三步,获取流对象
		InputStream inputStream = urlconnection.getInputStream();
		//第四步,创建本地文件路径
		File file = new File(filePath);
		//第五步,下载
		OutputStream outputStream = new FileOutputStream(file);
		int temp = 0;
		while((temp = inputStream.read())!=-1){
			outputStream.write(temp);
		}
		//第六步,关闭IO
		outputStream.close();
		inputStream.close();
		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
}
// 获取网页源代码
public static String htmlSource(String link,String encoding){
	StringBuilder stringBuilder = new StringBuilder();
	//1,创建连接
	try {
		URL url=new URL(link);
		//2,打开连接
		URLConnection urlConnection =url.openConnection();
		//3,打开流
		InputStream inputStream = urlConnection.getInputStream();
		//4,利用转换流将字节流转换为字符流,并存入缓冲区
		InputStreamReader isr = new InputStreamReader(inputStream,encoding);
		BufferedReader bufferedReader = new BufferedReader(isr);
		String line = null;
		while((line = bufferedReader.readLine())!=null){
			stringBuilder.append(line+"\r\n");
		}
		//5,关闭
		bufferedReader.close();
		inputStream.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return stringBuilder.toString();
}
}
最近下载更多
朱俪的邮件及存储  LV8 3月11日
linkedwin  LV1 2021年6月30日
xbybjc  LV4 2021年6月30日
陈王子  LV12 2021年5月15日
geekcjj  LV18 2021年1月31日
146zui  LV1 2020年10月31日
王天麟  LV12 2020年9月9日
decisionx1  LV1 2020年6月2日
164317494  LV1 2020年4月14日
eeeerer  LV9 2020年4月12日
最近浏览更多
朱俪的邮件及存储  LV8 3月11日
admin_z  LV22 1月29日
ljt289917726  LV3 2023年12月18日
Pro_Guoli 2023年3月23日
暂无贡献等级
12cq345  LV6 2022年11月15日
softandroad 2022年11月8日
暂无贡献等级
heqian  LV16 2022年10月17日
sunlea  LV17 2022年9月8日
akbar2020  LV9 2022年8月29日
2206371875  LV7 2022年4月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友