❶ 用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);
}
}

//給你一個下載的例子吧,僅供參考。

❷ 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);
}
}

//輸出內容:

文件名為:090602、09-10(1)校歷.xls

文件大小:42KB

下載成功,文件保存在:D:90602、09-10(1)校歷.xls

❸ java,http下載文件

http是流傳輸,一次請求中你是不能將流截斷的,如果想做到你說的只有兩種形式:內
1、就是你使用的方式,多容段讀取(其實就是斷點續傳的原理),多個請求,每個請求請求一部分,如果你覺得效率低可以從兩方面優化
(1)不要用URLConnecion,而使用HttpClient之類的工具包進行請求
(2)使用多線程並發請求,其實就是斷點續傳了,迅雷就是幾個線程一起下嘛
2、只請求一次,請求全部的,將全部流緩存到內存中(byte[]),然後根據你的需要分段截取,寫入文件

❹ Java 求改代碼可以下載http的東西

給你個下載東西的工具類,見附件,使用示例見main函數,其實很簡單,Download構造版函數的第一個參數即下載權源URL,第二個參數是下載生成的目標文件

Downloadd=newDownload(newURL("http://2.su.bdimg.com/skin/"+i+".jpg"),newFile(parent,i+".jpg"));
d.download();


❺ 請Java大牛幫忙,關於HTTP文件下載,功能可以實現,但是下載後總是有個別文件不能被刪除.


importjava.io.*;
importjava.net.*;
importjava.util.Scanner;


publicclassTest{
privatestaticStringfileName="http://localhost/file/1.jpg";
publicstaticvoidmain(Stringargs[])throwsException{
URLurl=newURL(fileName);
URLConnectionuc=url.openConnection();
uc.connect();
HttpURLConnectionhuc=(HttpURLConnection)uc;
if(huc.getResponseCode()!=HttpURLConnection.HTTP_OK){
//判斷是否成功連接到http,如果不能連接則返回
System.out.println("can'tconnect");
return;
}

Filef=newFile("D://text.jpg");//把文件復制到D盤的text.jpg中
InputStreamin=(uc.getInputStream());//打開輸入流
byte[]data=newbyte[1024*10];
intl=in.read(data);
if(!f.exists())f.createNewFile();
FileOutputStreamout=newFileOutputStream(f);
while(l!=-1){
out.write(data,0,l);
l=in.read(data);
}
out.flush();
out.close();
System.out.println("OK");
}
}

自己把源地址修改下

❻ 誰有基於http協議的java下載組件

JAVA 自帶的HttpURLConnection,裝飾一下了,,,關鍵是要明白HTTP協議

❼ Java編寫程序,用於下載HTTP伺服器上的某個文件

發送http請求,獲得二進制流,然後用outputstream輸出成對應文件格式就行了

❽ java如何將本地文件返回給對方一個下載鏈接(http方式下載)

根據http協議來做,
響應的時候 Content-Type 類型進行設置,當然你需要對外提供這個介面,將需要下載的本地文件以流的形式傳輸

❾ 一段JAVA HTTP下載的代碼,能幫我加上顯示下載速度的代碼嗎

完整的代碼過程