1. 用java下载HTTP文件时遇到问题
importjava.net.*;
importjava.io.*;
publicclassURLConnectionDemo{
publicstaticvoidmain(String[]args)throwsException{
URLurl=newURL("http://www.scp.e.cn/pantoschoolzz/BG/Bord/Message/DownloadMessageAttachment.aspx?ID=215");
URLConnectionuc=url.openConnection();
StringfileName=uc.getHeaderField(6);
fileName=URLDecoder.decode(fileName.substring(fileName.indexOf("filename=")+9),"UTF-8");
System.out.println("文件名为:"+fileName);
System.out.println("文件大小:"+(uc.getContentLength()/1024)+"KB");
Stringpath="D:"+File.separator+fileName;
FileOutputStreamos=newFileOutputStream(path);
InputStreamis=uc.getInputStream();
byte[]b=newbyte[1024];
intlen=0;
while((len=is.read(b))!=-1){
os.write(b,0,len);
}
os.close();
is.close();
System.out.println("下载成功,文件保存在:"+path);
}
}
//给你一个下载的例子吧,仅供参考。
2. java怎样读取http文件服务器上的文件列表并下载
要求文件名不能写死,那么只能到服务器上去遍历目录,如果服务器开了ftp权限的话到可以用apache的commons-net包,里面有ftp功能可以上传下载文件,也可以遍历文件
3. java 获得http下载文件的真实名称
有两种获取链接文件名的方法:
第一种:从连接URL,从描述中获取
比如这种地址:
http://book.booktxt.com/txtbuk/20130421/xuanhuan/2013043601180.rar