StringBuffer stringBuffer=new StringBuffer();
String html=null;
String path="D:\\uoladimg\\html\\";
try {
URL url=new URL("http://127.0.0.1:8080/DdongHouseSystem/index.jsp");
URLConnection connection=url.openConnection();
//connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
BufferedInputStream in=new BufferedInputStream(connection.getInputStream());
byte[] bytes=new byte[4096];
int read=0;
String line=null;
while (read>=0) {
line=new String(bytes,0,read,"utf-8");
stringBuffer.append(line);
read=in.read(bytes);//读取并返回个数,读不到则返回-1
line=null;
}
in.close();
connection=null;
url=null;
html=new String(stringBuffer.toString().getBytes("utf-8"), "utf-8");
System.out.println(html);
Document doucment= Jsoup.parse(html);//Java读取html文件
Elements elements=doucment.getElementsByTag("img");//得到img元素
for(Element element : elements ){
String src=element.attr("src");//得到img元素中src属性的值
if(!"".equals(src)&&src.endsWith(".jpg")||src.endsWith(".jif")||src.endsWith(".png")){//得到所以src属性的值 的并以http开头的图片
System.out.println(src);
String houzuoming=src.substring(src.lastIndexOf("/"));
System.out.println(houzuoming);
File file=new File(path+houzuoming);//创建一个文件夹
if(file.exists()){//判断文件夹是否存在
file.createNewFile();
}
Writer writer=new OutputStreamWriter(new FileOutputStream(file,false),"utf-8");
writer.write(src);
writer.close();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
java 下载图片 图片无法打开求解答!
答案:2 mip版
解决时间 2021-02-26 02:46
- 提问者网友:笑低了眉眼
- 2021-02-25 09:44
最佳答案
- 二级知识专家网友:相忘于江湖
- 2021-02-25 10:48
图片怎么能用写字符的形式写 呢?
全部回答
- 1楼网友:妳旳笑魅惑了涐旳瞳
- 2021-02-25 11:49
任务占坑
我要举报
如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
点此我要举报以上问答信息
推荐资讯