怎么用url写一个下载图片 怎么用url写一个下载图片 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 此回复为自动发出,仅用于显示而已,并无任何其他特殊作用楼主【zq620901】截止到2008-07-09 16:48:29的历史汇总数据(不包括此帖):发帖的总数量:0 发帖的总分数:0 每贴平均分数:0 回帖的总数量:0 得分贴总数量:0 回帖的得分率:0% 结贴的总数量:0 结贴的总分数:0 无满意结贴数:0 无满意结贴分:0 未结的帖子数:0 未结的总分数:0 结贴的百分比:---------------------结分的百分比:---------------------无满意结贴率:---------------------无满意结分率:---------------------如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html URL ?Runtime.getRuntime() 好了.启动迅雷下,嘿嘿!愿楼主好运噢!! IO学的不太好,给个程序你参考一下吧/*** picture 是图片地址* dest 是保存图片的地址**/public class GetPicture { public static void getPicture(String picture, String dest) { try { URL url = new URL(picture); HttpURLConnection con = (HttpURLConnection) url.openConnection(); DataInputStream reader = new DataInputStream(con.getInputStream()); DataOutputStream writer = new DataOutputStream(new FileOutputStream(dest)); int ks = 0; while((ks = reader.read()) != -1) { writer.write(ks); } reader.close(); writer.close(); } catch (FileNotFoundException ex) { System.out.println("NOT FOUND:" + picture); } catch (ConnectException ex) { System.out.println("Timeout:" + picture); } catch (Exception ex) { ex.printStackTrace(); } }} 3楼正解,如果把关闭输入输出流的 close 方法放到 finally 块里就更完美了。 import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.SocketTimeoutException;import java.net.URL;public class URLtest { public static void main(String[] args) { String imgurl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif"; String svpath = "C:/ZCG/downloads/dimg.gif"; try { URL url = new URL(imgurl); HttpURLConnection con = (HttpURLConnection) url.openConnection(); System.out.println("ResponseCode = " + con.getResponseCode()); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(con.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream(svpath)); byte[] bs = new byte[100]; int n = 0; while ((n = bis.read(bs)) != -1) { bos.write(bs, 0, n); } bos.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } finally { bis = null; } } if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } finally { bos = null; } } } con.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (SocketTimeoutException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }} import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.SocketTimeoutException;import java.net.URL;public class ImgDlder { public static void main(String[] args) { String imgurl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif"; String svpath = "C:/downloads/dimg.gif"; try { URL url = new URL(imgurl); HttpURLConnection con = (HttpURLConnection)url.openConnection(); System.out.println("ResponseCode = " + con.getResponseCode()); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(con.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream(svpath)); byte[] bs = new byte[100]; int n = 0; while ((n = bis.read(bs)) != -1) { bos.write(bs, 0, n); } bos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } finally { bis = null; } } if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } finally { bos = null; } } } con.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (SocketTimeoutException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }} 完善 // 增加referer参数,用于下载那些防止盗链的图片 con.addRequestProperty("referer",picture); import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.SocketTimeoutException;import java.net.URL;public class ImgDlder { public static void main(String[] args) { String imgurl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif"; String svpath = "C:/downloads/dimg.gif"; try { URL url = new URL(imgurl); HttpURLConnection con = (HttpURLConnection)url.openConnection(); System.out.println("ResponseCode = " + con.getResponseCode()); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(con.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream(svpath)); byte[] bs = new byte[100]; int n = 0; while ((n = bis.read(bs)) != -1) { bos.write(bs, 0, n); } bos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } finally { bis = null; } } if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } finally { bos = null; } } } con.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (SocketTimeoutException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}sagezk 终于升了 得散点分阿 :) 反射概念明白了,但是反射干嘛用的又不明白了,哪位大神能帮我解释一下。 各位大虾请赐教啊! Applet JFrame&JPanel 如何初始化本地变量 求救关于打印机打印指定网页 一条语句难倒全公司人,高手进!! 怎么实现JButton的拖拽啊 ??? 怎样用JBuilder 7编写用JDBC (for MS SQL 2000 )访问MS sql 2000 Server 的应用程序 问个小问题! 线程问题,请高手支招!谢谢! 线程死锁问题源码不理解,请高手帮忙
楼主【zq620901】截止到2008-07-09 16:48:29的历史汇总数据(不包括此帖):
发帖的总数量:0 发帖的总分数:0 每贴平均分数:0
回帖的总数量:0 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:---------------------结分的百分比:---------------------
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
URL ?
Runtime.getRuntime() 好了.
启动迅雷下,嘿嘿!愿楼主好运噢!!
* picture 是图片地址
* dest 是保存图片的地址
**/
public class GetPicture {
public static void getPicture(String picture, String dest) {
try {
URL url = new URL(picture);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
DataInputStream reader = new DataInputStream(con.getInputStream());
DataOutputStream writer = new DataOutputStream(new FileOutputStream(dest));
int ks = 0;
while((ks = reader.read()) != -1) {
writer.write(ks);
}
reader.close();
writer.close();
} catch (FileNotFoundException ex) {
System.out.println("NOT FOUND:" + picture);
} catch (ConnectException ex) {
System.out.println("Timeout:" + picture);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;public class URLtest { public static void main(String[] args) {
String imgurl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif";
String svpath = "C:/ZCG/downloads/dimg.gif";
try {
URL url = new URL(imgurl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
System.out.println("ResponseCode = " + con.getResponseCode());
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(con.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(svpath));
byte[] bs = new byte[100];
int n = 0;
while ((n = bis.read(bs)) != -1) {
bos.write(bs, 0, n);
}
bos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
bis = null;
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
bos = null;
}
}
}
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}}
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;public class ImgDlder { public static void main(String[] args) {
String imgurl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif";
String svpath = "C:/downloads/dimg.gif";
try {
URL url = new URL(imgurl);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
System.out.println("ResponseCode = " + con.getResponseCode());
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(con.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(svpath));
byte[] bs = new byte[100];
int n = 0;
while ((n = bis.read(bs)) != -1) {
bos.write(bs, 0, n);
}
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
bis = null;
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
bos = null;
}
}
}
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}}
// 增加referer参数,用于下载那些防止盗链的图片
con.addRequestProperty("referer",picture);
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;public class ImgDlder { public static void main(String[] args) {
String imgurl = "http://www.google.cn/intl/zh-CN/images/logo_cn.gif";
String svpath = "C:/downloads/dimg.gif";
try {
URL url = new URL(imgurl);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
System.out.println("ResponseCode = " + con.getResponseCode());
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(con.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(svpath));
byte[] bs = new byte[100];
int n = 0;
while ((n = bis.read(bs)) != -1) {
bos.write(bs, 0, n);
}
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
bis = null;
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
bos = null;
}
}
}
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}}
sagezk 终于升了 得散点分阿 :)