免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版
打開APP
未登錄
開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服
開通VIP
首頁
好書
留言交流
下載APP
聯(lián)系客服
java 從網(wǎng)絡(luò)Url中下載文件
aaie_
>《JAVA》
2016.11.27
關(guān)注
[java]
view plain
copy
print
?
/**
* 從網(wǎng)絡(luò)Url中下載文件
* @param urlStr
* @param fileName
* @param savePath
* @throws IOException
*/
public
static
void
downLoadFromUrl(String urlStr,String fileName,String savePath)
throws
IOException{
URL url =
new
URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//設(shè)置超時間為3秒
conn.setConnectTimeout(
3
*
1000
);
//防止屏蔽程序抓取而返回403錯誤
conn.setRequestProperty(
"User-Agent"
,
"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"
);
//得到輸入流
InputStream inputStream = conn.getInputStream();
//獲取自己數(shù)組
byte
[] getData = readInputStream(inputStream);
//文件保存位置
File saveDir =
new
File(savePath);
if
(!saveDir.exists()){
saveDir.mkdir();
}
File file =
new
File(saveDir+File.separator+fileName);
FileOutputStream fos =
new
FileOutputStream(file);
fos.write(getData);
if
(fos!=
null
){
fos.close();
}
if
(inputStream!=
null
){
inputStream.close();
}
System.out.println(
"info:"
+url+
" download success"
);
}
/**
* 從輸入流中獲取字節(jié)數(shù)組
* @param inputStream
* @return
* @throws IOException
*/
public
static
byte
[] readInputStream(InputStream inputStream)
throws
IOException {
byte
[] buffer =
new
byte
[
1024
];
int
len =
0
;
ByteArrayOutputStream bos =
new
ByteArrayOutputStream();
while
((len = inputStream.read(buffer)) != -
1
) {
bos.write(buffer,
0
, len);
}
bos.close();
return
bos.toByteArray();
}
public
static
void
main(String[] args) {
try
{
downLoadFromUrl(
"http://101.95.48.97:8005/res/upload/interface/apptutorials/manualstypeico/6f83ce8f-0da5-49b3-bac8-fd5fc67d2725.png"
,
"百度.jpg"
,
"d:/resource/images/diaodiao/country/"
);
}
catch
(Exception e) {
// TODO: handle exception
}
}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報
。
打開APP,閱讀全文并永久保存
查看更多類似文章
猜你喜歡
類似文章
jfreechart圖片直接轉(zhuǎn)為byte[]然后轉(zhuǎn)為base64導(dǎo)入xml,最后圖片導(dǎo)入word
(原)用java流方式判斷文件類型
基于SMB/JCIFS協(xié)議的共享文件上傳和下載
HttpURLConnection(訪問網(wǎng)絡(luò))
文件上傳流式處理commons
將輸出流(OutputStream)轉(zhuǎn)化為輸入流(InputStream)的方法(一)
更多類似文章 >>
生活服務(wù)
首頁
萬象
文化
人生
生活
健康
教育
職場
理財
娛樂
藝術(shù)
上網(wǎng)
留言交流
回頂部
聯(lián)系我們
分享
收藏
點(diǎn)擊這里,查看已保存的文章
導(dǎo)長圖
關(guān)注
一鍵復(fù)制
下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!
聯(lián)系客服
微信登錄中...
請勿關(guān)閉此頁面
先別劃走!
送你5元優(yōu)惠券,購買VIP限時立減!
5
元
優(yōu)惠券
優(yōu)惠券還有
10:00
過期
馬上使用
×