来者有分,下载文件我这么做对吗?

hanker000 2003-10-18 11:55:56
import java.io.*;
import java.net.*;
.....
try{
URL url=new URL ("http://。。。。。/zip.exe");
HttpURLConnection httpCon=(HttpURLConnection)url.openConnection();
httpCon.setRequestProperty ("User-Agent","NetFox");
httpCon.setRequestProperty ("RANGE","bytes=0");
InputStream input=httpCon.getInputStream ();
RandomAccessFile file=new RandomAccessFile ("c:\\zip.exe","wr");
file.seek (0);
byte[] b=new byte [1024];
int count;
while((count=input.read (b,0,1024))>0)
{file.write (b,0,count);}
System.out.println ("下载完毕");
}catch (IOException w){System.out.println ("io");}
catch (Exception w){System.out.println ("other");}}
...全文
27 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
imagex 2003-10-18
  • 打赏
  • 举报
回复
http://www.manyi100.com/view.asp?id=627
cbhyk 2003-10-18
  • 打赏
  • 举报
回复
输出到文件用FileOutputStream好一些:
...
URL url=new URL ("http://。。。。。/zip.exe");
HttpURLConnection httpCon=(HttpURLConnection)url.openConnection();
httpCon.setRequestProperty ("User-Agent","NetFox");
httpCon.setRequestProperty ("RANGE","bytes=0");
InputStream input=httpCon.getInputStream ();
OutputStream out = new FileOutputStream("c:\\zip.exe");
byte[] b=new byte [4096];
int count;
while((count=input.read (b,0,4096))>0)
out.write(b, 0, count);
out.close();
input.close();
...
LoveRose 2003-10-18
  • 打赏
  • 举报
回复
看起来是可以的
sasa_sasa 2003-10-18
  • 打赏
  • 举报
回复
学习……
zp0414 2003-10-18
  • 打赏
  • 举报
回复
关注
exitzhang 2003-10-18
  • 打赏
  • 举报
回复
!

62,612

社区成员

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

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