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();
}
}
最近下载更多
最近浏览更多
SZEPEZS LV9
2月18日
ma406805131 LV19
2024年5月25日
一本正经的胡乱叭叭叭
2024年5月20日
暂无贡献等级
朱俪的邮件及存储 LV8
2024年3月11日
admin_z LV22
2024年1月29日
ljt289917726 LV3
2023年12月18日
Pro_Guoli
2023年3月23日
暂无贡献等级
12cq345 LV6
2022年11月15日
softandroad
2022年11月8日
暂无贡献等级
heqian LV17
2022年10月17日

