用Java如何下载一个网页里面的图片到本地?

xEclipse 2005-04-20 11:16:20
如题~传入URL,URL为图片的地址
就相当于鼠标右键另存的功能~
...全文
823 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bwsabc 2005-04-20
  • 打赏
  • 举报
回复
applet.
chinajava 2005-04-20
  • 打赏
  • 举报
回复
JavaAnts研究一下
lucia_venture 2005-04-20
  • 打赏
  • 举报
回复
public String saveToFile(String destUrl, String pathName) {
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
String fullPath = null;

byte[] buf = new byte[BUFFER_SIZE];
int size = 0;

try {
url = new URL(destUrl);
httpUrl = (HttpURLConnection) url.openConnection();

httpUrl.connect();

bis = new BufferedInputStream(httpUrl.getInputStream());

fullPath = getFullPath(pathName,this.fileFormat);
fos = new FileOutputStream(fullPath);

while ( (size = bis.read(buf)) != -1) {
fos.write(buf, 0, size);
}
fos.flush();
}
catch(IOException e) {

}
catch(ClassCastException e) {

}
finally {
try {
fos.close();
bis.close();
httpUrl.disconnect();
}
catch(IOException e) {

}
catch(NullPointerException e) {

}
}
return getImageName(fullPath);
}
ngqzmjmj 2005-04-20
  • 打赏
  • 举报
回复
URL url = new URL("图片地址");
URLConnection uc = url.openConnection();
InputStream is = uc.getInputStream();
File file = new File("本地路径");
FileOutputStream out = new FileOutputStream(file);
int i=0;
while ((i=is.read())!=-1) {
out.write(i);
}
is.close();

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧