如题,在线等
解决方案 »
- eclipse IDE 遇到的不解的问题
- c/s模式编程的问题
- 响应delete键
- SQL server 将nvarchar值转换成 int 时失败··
- 新人求助!oracle jdbc连接问题,万望各位大人指点一下!
- 求助:JAVA窗口切换面板,实现不同的界面
- 关于Swing中按钮的问题
- 【求助】为什么内部类传参要求对象是final的?不然就会编译错误?是出于什么考虑呢?
- 问一个关于Eclipse的问题(关于拖动选中的代码)
- 我的IE不能显示applet,请问会是什么原因?原来想是只是自己机器上的看不了,谁知网上的也不行:(用appletviewer可以看到)
- 前景问题
- 如何实现像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) {
}
}
}
}