Java 如何將網(wǎng)絡(luò)資源url轉(zhuǎn)化為File文件
更新時(shí)間:2021年09月16日 10:33:56 作者:狂風(fēng)之力
這篇文章主要介紹了Java 如何將網(wǎng)絡(luò)資源url轉(zhuǎn)化為File文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
將網(wǎng)絡(luò)資源url轉(zhuǎn)化為File文件
將互聯(lián)網(wǎng)上的http開頭的url資源,保存到本地。
private File getNetUrlHttp(String path){
//對(duì)本地文件命名,path是http的完整路徑,主要得到資源的名字
String newUrl = path;
newUrl = newUrl.split("[?]")[0];
String[] bb = newUrl.split("/");
//得到最后一個(gè)分隔符后的名字
String fileName = bb[bb.length - 1];
//保存到本地的路徑
String filePath="e:\\audio\\"+fileName;
File file = null;
URL urlfile;
InputStream inputStream = null;
OutputStream outputStream = null;
try{
//判斷文件的父級(jí)目錄是否存在,不存在則創(chuàng)建
file = new File(filePath);
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
try{
//創(chuàng)建文件
file.createNewFile();
}catch (Exception e){
e.printStackTrace();
}
//下載
urlfile = new URL(newUrl);
inputStream = urlfile.openStream();
outputStream = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead=inputStream.read(buffer,0,8192))!=-1) {
outputStream.write(buffer, 0, bytesRead);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (null != outputStream) {
outputStream.close();
}
if (null != inputStream) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return file;
}
url轉(zhuǎn)變?yōu)?MultipartFile對(duì)象
/**
* url轉(zhuǎn)變?yōu)?MultipartFile對(duì)象
* @param url
* @param fileName
* @return
* @throws Exception
*/
private static MultipartFile createFileItem(String url, String fileName) throws Exception{
FileItem item = null;
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setReadTimeout(30000);
conn.setConnectTimeout(30000);
//設(shè)置應(yīng)用程序要從網(wǎng)絡(luò)連接讀取數(shù)據(jù)
conn.setDoInput(true);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
FileItemFactory factory = new DiskFileItemFactory(16, null);
String textFieldName = "uploadfile";
item = factory.createItem(textFieldName, ContentType.APPLICATION_OCTET_STREAM.toString(), false, fileName);
OutputStream os = item.getOutputStream();
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
is.close();
}
} catch (IOException e) {
throw new RuntimeException("文件下載失敗", e);
}
return new CommonsMultipartFile(item);
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis-Plus如何實(shí)現(xiàn)自動(dòng)加密解密
這篇文章主要介紹了MyBatis-Plus實(shí)現(xiàn)自動(dòng)加密解密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
使用Java讀取Excel文件數(shù)據(jù)的方法詳解
通過編程方式讀取Excel數(shù)據(jù)能實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入、批量處理、數(shù)據(jù)比對(duì)和更新等任務(wù)的自動(dòng)化,本文為大家介紹了三種Java讀取Excel文件數(shù)據(jù)的方法,需要的可以參考下2024-01-01
JAVA中ListIterator和Iterator詳解與辨析(推薦)
這篇文章主要介紹了JAVA中ListIterator和Iterator詳解與辨析,需要的朋友可以參考下2017-04-04
java system類使用方法示例 獲取系統(tǒng)信息
這篇文章主要介紹了java system類使用方法,該類中的方法都是靜態(tài)的。不能被實(shí)例化,沒有對(duì)外提供構(gòu)造函數(shù),該類可以獲取系統(tǒng)信息2014-01-01
如何通過java實(shí)現(xiàn)highcharts導(dǎo)出圖片至excel
這篇文章主要介紹了如何通過java實(shí)現(xiàn)highcharts導(dǎo)出圖片至excel。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面我們就來一起學(xué)習(xí)一下吧2019-06-06
java ArrayList集合中的某個(gè)對(duì)象屬性進(jìn)行排序的實(shí)現(xiàn)代碼
這篇文章主要介紹了java ArrayList集合中的某個(gè)對(duì)象屬性進(jìn)行排序的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-07-07

