導航:首頁 > 編程語言 > javaurl下載文件

javaurl下載文件

發布時間:2024-05-28 07:58:49

A. java程序下載pdf文件

主要是 URL 和 HttpURLConnection 類的運用,看代碼:


importjava.io.DataInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.net.HttpURLConnection;
importjava.net.URL;

publicclassHttpDownloader{

_FILE_URL="http://211.103.156.163/u/cms/www/201511/25051940i6ou.pdf";
privatestaticfinalStringLOCAL_FILE_PATH="D:/some.pdf";//改成你保存文件的路徑

publicstaticvoidmain(String[]args){
newHttpDownloader(REMOTE_FILE_URL,LOCAL_FILE_PATH).download();
}

privateStringremoteFileUrl;
privateStringlocalFilePath;

publicHttpDownloader(StringremoteFileUrl,StringlocalFilePath){
this.remoteFileUrl=remoteFileUrl;
this.localFilePath=localFilePath;
}

publicvoiddownload(){
try{
URLurl=newURL(remoteFileUrl);

=(HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout(5*1000);//5000毫秒內沒有連接上則放棄連接
httpURLConnection.connect();//連接
System.out.println("連接URL成功~");

intfileLenght=httpURLConnection.getContentLength();
System.out.println("文件大小:"+(fileLenght/1024.0)+"KB");

System.out.println("開始下載...");
try(DataInputStreamdis=newDataInputStream(httpURLConnection.getInputStream());
FileOutputStreamfos=newFileOutputStream(localFilePath)){
byte[]buf=newbyte[10240];//根據實際情況可以增大buf大小
for(intreadSize;(readSize=dis.read(buf))>0;){
fos.write(buf,0,readSize);
}
System.out.println("下載完畢~");
}catch(IOExceptionex){
System.out.println("下載時出錯");
}

httpURLConnection.disconnect();
}catch(IOExceptionex){
System.out.println("URL不存在或者連接超時");
}
}
}

B. 用java實現文件的上傳與下載

1.下載簡單,無非是把伺服器上的文件或者資料庫中的BLob(或其他二進制型),用流讀出來,然後寫到客戶端即可,要注意 ContentType。

2.上傳,可以用Apache Commons Upload等開源工具,或者自己寫:
form要用enctype="multipart/form-data"
然後伺服器端也是用IO把客戶端提交的文件流讀入,然後寫到伺服器的文件系統或者資料庫里。不同的資料庫對Lob欄位操作可能有所不同,建議用Hibernate,JPA等成熟的ORM框架,可以不考慮資料庫細節。

C. Java 利用url下載MP3保存到本地

//mp3Url MP3的URL
InputStream in=new URL(mp3Url).openConnection().getInputStream(); //創建連接、輸入流
FileOutputStream f = nre FileOutputStream("c:\mmm.mp3");//創建文件輸出流
byte [] bb=new byte[1024]; //接收緩存
int len;
while( (len=in.read(bb))>0){ //接收
f.write(bb, 0, len); //寫入文件
}
f.close();
in.close();
基本框架,自己調試修改一下

D. 用java實現文件的下載,如何提高下載速度(非web開發)

下面貼出的代碼是一個簡單的讀取遠程文件保存到本地的實現,至於提高下載速度你可以利用多線程,具體可參考最下面的那個網址——

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class DownloadTester {
public static void main(String[] args) throws IOException {
String urlStr = "http://img..com/img/logo-.gif";
String path = "D:/";

String name = urlStr.substring(urlStr.trim().lastIndexOf("/"));

URL url = new URL(urlStr);
InputStream in = url.openConnection().getInputStream();

File file = new File(path + name);
FileOutputStream out = new FileOutputStream(file, true);

int counter = 0;
int ch;
byte[] buffer = new byte[1024];
while ((ch = in.read(buffer)) != -1) {
out.write(buffer, 0, ch);
counter += ch;

System.out.println(counter + ":byte");
}
out.flush();
in.close();
out.close();
}
}

E. 我用java做了一個通過url地址下載指定文件的功能,文件名可能包含中文,IE正常,火狐失敗.

您好!很高興為您答疑!

火狐下您可以安裝Firebug檢查頁面代碼,它集HTML查看和編輯、Javascript控制台、網路狀況監視器於一體,是開發JavaScript、CSS、HTML和Ajax的得力助手。
您可以在火狐社區了解更多內容。希望我的回答對您有所幫助,如有疑問,歡迎繼續在本平台咨詢。

閱讀全文

與javaurl下載文件相關的資料

熱點內容
lua編譯有什麼用 瀏覽:350
買了伺服器如何架設 瀏覽:929
如何運用mex函數編譯c 瀏覽:896
24歲程序員倒在工作上 瀏覽:919
怎麼算梁的加密區 瀏覽:93
2016版office怎麼解壓 瀏覽:270
怎麼把安卓手機調的更暗 瀏覽:167
蘋果空間新演算法 瀏覽:91
android文字動畫效果 瀏覽:146
java調試命令 瀏覽:213
android子線程looper 瀏覽:782
linux安裝java7 瀏覽:189
單片機fdh 瀏覽:107
單片機原理與應用下載 瀏覽:590
順風車車主app在哪裡下載 瀏覽:235
雷石柏雲伺服器功率 瀏覽:102
全球服是什麼伺服器 瀏覽:237
感測器怎麼連接伺服器 瀏覽:705
大數學pdf 瀏覽:646
哪個app可以登記自己的藏書 瀏覽:89