Java 如何將網(wǎng)絡(luò)資源url轉(zhuǎn)化為File文件
更新時間:2021年09月16日 10:33:56 作者:狂風之力
這篇文章主要介紹了Java 如何將網(wǎng)絡(luò)資源url轉(zhuǎn)化為File文件的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
將網(wǎng)絡(luò)資源url轉(zhuǎn)化為File文件
將互聯(lián)網(wǎng)上的http開頭的url資源,保存到本地。
private File getNetUrlHttp(String path){ //對本地文件命名,path是http的完整路徑,主要得到資源的名字 String newUrl = path; newUrl = newUrl.split("[?]")[0]; String[] bb = newUrl.split("/"); //得到最后一個分隔符后的名字 String fileName = bb[bb.length - 1]; //保存到本地的路徑 String filePath="e:\\audio\\"+fileName; File file = null; URL urlfile; InputStream inputStream = null; OutputStream outputStream = null; try{ //判斷文件的父級目錄是否存在,不存在則創(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對象
/** * url轉(zhuǎn)變?yōu)?MultipartFile對象 * @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); }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Java讀取Excel文件數(shù)據(jù)的方法詳解
通過編程方式讀取Excel數(shù)據(jù)能實現(xiàn)數(shù)據(jù)導入、批量處理、數(shù)據(jù)比對和更新等任務(wù)的自動化,本文為大家介紹了三種Java讀取Excel文件數(shù)據(jù)的方法,需要的可以參考下2024-01-01JAVA中ListIterator和Iterator詳解與辨析(推薦)
這篇文章主要介紹了JAVA中ListIterator和Iterator詳解與辨析,需要的朋友可以參考下2017-04-04java system類使用方法示例 獲取系統(tǒng)信息
這篇文章主要介紹了java system類使用方法,該類中的方法都是靜態(tài)的。不能被實例化,沒有對外提供構(gòu)造函數(shù),該類可以獲取系統(tǒng)信息2014-01-01如何通過java實現(xiàn)highcharts導出圖片至excel
這篇文章主要介紹了如何通過java實現(xiàn)highcharts導出圖片至excel。文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,下面我們就來一起學習一下吧2019-06-06java ArrayList集合中的某個對象屬性進行排序的實現(xiàn)代碼
這篇文章主要介紹了java ArrayList集合中的某個對象屬性進行排序的實現(xiàn)代碼,需要的朋友可以參考下2016-07-07