高手指教:在java下通过url获取远端图片,保存到本地 如题,在线等 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java.net.URL,java.net.URLConnection,网上喳喳这两个类。从一个指定的url,用流的方式吧数据读过来就可以了 哪个高手帮忙写一下啊,我那样做就是出不来啊public class ReadGif { public static void getGif() throws Exception { String URLName = "http://www.baidu.com/img/logo.gif"; int HttpResult; URL url=new URL(URLName); URLConnection urlconn=url.openConnection(); urlconn.connect(); HttpURLConnection httpconn=(HttpURLConnection)urlconn; HttpResult=httpconn.getResponseCode(); if(HttpResult!=HttpURLConnection.HTTP_OK) { System.out.println("cant connct0"); } else { int filesize=urlconn.getContentLength(); InputStreamReader isReader=new InputStreamReader(urlconn.getInputStream()); char[] buffer=new char[filesize]; int num=0; PrintWriter fileOut = new PrintWriter(new FileWriter("1.gif")); String temp = ""; //while(num>-1){ num=isReader.read(buffer); //if(num<0)break; try { fileOut.write(buffer, 0, num); } catch (Exception e) { System.out.println(e); } fileOut.close(); //} isReader.close(); } } public static void main(String[] args) { try { ReadGif.getGif(); } catch (Exception e) { } }} package getpicture;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2005</p> * <p>Company: </p> * @author not attributable * @version 1.0 */import java.net.*;import java.io.*;import javax.imageio.*;import javax.imageio.stream.ImageOutputStream;public class GetPic { public GetPic() { } public static void main(String[] args) { GetPic getPic1 = new GetPic(); getPic1.saveToFile("http://community.csdn.net/images/CSDN_logo.GIF"); } public void saveToFile(String destUrl) { FileOutputStream fos = null; BufferedInputStream bis = null; HttpURLConnection httpUrl = null; URL url = null; int BUFFER_SIZE = 1024; 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()); fos = new FileOutputStream("e:\\haha.gif"); 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) { } } }} 难题...JAVA接收键盘事件... 讨论一个问题:怎样避免一个类在不断地变化中对它相关联的类产生不利影响 如何解决JavaBean同包问题??? ajax javascript document 属性处理问题 有关字符编码格式转换的问题?望不吝赐教。 -----windows下CVS的配置和使用问题(不知道应该在哪里问)------ Vector里的get(int index) 和elementAt(int index)有什么区别吗? 奇怪的问题!!!!急!!!! 关于jre how to learn java 前景问题 如何实现像FTP一样的下载任务和百分比的时时更新
public class ReadGif { public static void getGif() throws Exception {
String URLName = "http://www.baidu.com/img/logo.gif";
int HttpResult;
URL url=new URL(URLName);
URLConnection urlconn=url.openConnection();
urlconn.connect();
HttpURLConnection httpconn=(HttpURLConnection)urlconn;
HttpResult=httpconn.getResponseCode();
if(HttpResult!=HttpURLConnection.HTTP_OK)
{
System.out.println("cant connct0");
}
else
{
int filesize=urlconn.getContentLength();
InputStreamReader isReader=new InputStreamReader(urlconn.getInputStream());
char[] buffer=new char[filesize];
int num=0;
PrintWriter fileOut = new PrintWriter(new FileWriter("1.gif"));
String temp = "";
//while(num>-1){
num=isReader.read(buffer);
//if(num<0)break;
try {
fileOut.write(buffer, 0, num);
} catch (Exception e) {
System.out.println(e);
}
fileOut.close();
//}
isReader.close();
}
} public static void main(String[] args) { try {
ReadGif.getGif();
} catch (Exception e) { } }}
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.net.*;
import java.io.*;
import javax.imageio.*;
import javax.imageio.stream.ImageOutputStream;public class GetPic {
public GetPic() {
} public static void main(String[] args) {
GetPic getPic1 = new GetPic();
getPic1.saveToFile("http://community.csdn.net/images/CSDN_logo.GIF");
} public void saveToFile(String destUrl) {
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
int BUFFER_SIZE = 1024; 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()); fos = new FileOutputStream("e:\\haha.gif");
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) {
}
}
}
}