這篇文章主要介紹java如何下載網(wǎng)絡(luò)文件,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)公司制作網(wǎng)站網(wǎng)頁找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁設(shè)計,成都網(wǎng)站設(shè)計、網(wǎng)站制作,網(wǎng)站設(shè)計,企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為數(shù)千家服務(wù),創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷推廣服務(wù)!
下載網(wǎng)絡(luò)文件的方法有:
字節(jié)流下載
apache的FileUtils工具包下載
NIO下載
學(xué)習(xí)視頻分享:java教學(xué)視頻
實現(xiàn)代碼如下:
package com.dsp.rpc.metricelf;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadZipUtil {
/**
* FileUtils下載網(wǎng)絡(luò)文件
*
* @param serverUrl :網(wǎng)絡(luò)文件地址
* @param savePath:本地保存路徑
* @param zipSavePath :壓縮文件保存路徑
* @return
*/
public static String downloadFile(String serverUrl, String savePath, String zipSavePath) throws Exception {
String result;
File f = new File(savePath);
if (!f.exists()) {
if (!f.mkdirs()) {
throw new Exception("makdirs: '" + savePath + "'fail");
}
}
URL url = new URL(serverUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3 * 1000);
//防止屏蔽程序抓取而放回403錯誤
conn.setRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE 5.0;Windows NT;DigExt)");
Long totalSize = Long.parseLong(conn.getHeaderField("Content-Length"));
if (totalSize > 0) {
FileUtils.copyURLToFile(url, new File(zipSavePath));
result = "success";
} else {
throw new Exception("can not find serverUrl :{}" + serverUrl);
}
return result;
}
/**
* 字節(jié)流下載壓縮文件
* @param serverUrl :網(wǎng)絡(luò)地址
* @param savePath :保持路徑
* @param zipSavePath :壓縮文件保持路徑
* @return :下載結(jié)果
* @throws Exception :異常
*/
public static String downloadZip(String serverUrl,String savePath,String zipSavePath) throws Exception{
String result = "fail";
File f = new File(savePath);
if(!f.exists()){
if (!f.mkdirs()) {
throw new Exception("makdirs: '" + savePath + "'fail");
}
}
//Sardine是WebDAV的工具包
Sardine sardine = SardineFactory.begin("test","test");
if(sardine.exists(serverUrl)){
URL url = new URL(serverUrl);
URLConnection conn = url.openConnection();
int length = conn.getContentLength();
conn.setConnectTimeout(3 * 1000);
// 防止屏蔽程序抓取而返回403錯誤
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
InputStream is = sardine.getInputStream(serverUrl);
BufferedInputStream bis = new BufferedInputStream(is);
FileOutputStream fos = new FileOutputStream(zipSavePath);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len;
byte[] bytes = new byte[length/5];
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
//清除緩存
bos.flush();
//關(guān)閉流
fos.close();
is.close();
bis.close();
bos.close();
result = "success";
}else {
throw new Exception("can not find file");
}
return result;
}
}以上是“java如何下載網(wǎng)絡(luò)文件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁名稱:java如何下載網(wǎng)絡(luò)文件
本文路徑:http://chinadenli.net/article40/iijoeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、動態(tài)網(wǎng)站、域名注冊、網(wǎng)站營銷、電子商務(wù)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)