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