java 下载指定路径_用java流的方式怎么指定下载到指定目录下
展開(kāi)全部
舉例代碼:/**
*?下載文件。
*?@param?urlStr?文件的URL
*?@param?savePath?保存到62616964757a686964616fe4b893e5b19e31333365656662的目錄
*?@param?fileName?保存的文件名稱
*?@param?description?描述(如:歌曲,專輯封面,歌詞等)
*?@throws?IOException
*/
public?static?void?downLoad(String?urlStr,?String?savePath,?String?fileName,?String?description)?throws?IOException
{
URL?url?=?new?URL(urlStr);
HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection();
conn.setConnectTimeout(100000);??//?設(shè)置超時(shí)間為10秒
conn.setRequestProperty("User-Agent",?"Mozilla/4.0?(compatible;?MSIE?5.0;?Windows?NT;?DigExt)");??//?防止屏蔽程序抓取而返回403錯(cuò)誤
File?saveDir?=?new?File(savePath);
File?file?=?new?File(saveDir?+?File.separator?+?fileName);
try?(InputStream?inputStream?=?conn.getInputStream();
FileOutputStream?fos?=?new?FileOutputStream(file))
{
byte[]?flowData?=?readInputStream(inputStream);
fos.write(flowData);
}?catch?(Exception?e)?{
MainFrame.logEvent("字節(jié)保存時(shí)出現(xiàn)意外:"?+?e.getMessage());
}
MainFrame.logEvent(description?+?"下載完成:"?+?url);
}
MainFrame.logEvent()是我自己弄的日志記錄而已,可以忽略不看
總結(jié)
以上是生活随笔為你收集整理的java 下载指定路径_用java流的方式怎么指定下载到指定目录下的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: xvid开放源码xvidcore-1.1
- 下一篇: 五月天歌曲下载【专辑整理版】