溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

java中怎么從網(wǎng)絡(luò)下載多個(gè)文件

發(fā)布時(shí)間:2021-08-07 14:53:15 來(lái)源:億速云 閱讀:154 作者:Leah 欄目:編程語(yǔ)言

今天就跟大家聊聊有關(guān)java中怎么從網(wǎng)絡(luò)下載多個(gè)文件,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

具體內(nèi)容如下

首先是打包下載多文件,即打成壓縮包在下載。

其次 別處的資源:可以是別的服務(wù)器,可以是網(wǎng)上的資源,當(dāng)然也可以是本地的(更簡(jiǎn)單)

最后:一次性下載,一次性下載多個(gè)文件

三步走:

一、先將 “別處” 需要下載的文件下載到服務(wù)器,然后將文件的路徑改掉

二、然后將服務(wù)器上的文件打成壓縮包

三、下載這個(gè)壓縮包

//下載 @RequestMapping("/download01") public void downloadImage(String tcLwIds, HttpServletRequest request, HttpServletResponse response) throws Exception{ boolean dflag = false; String[] paths = tcLwIds.split(","); File [] file1 = new File[paths.length]; DownLoadImageUtil imageUtils = new DownLoadImageUtil();  if(paths.length > 1){ for (int i = 0; i < paths.length; i++) { String imagepath=paths[i]; imageUtils.makeImage(imagepath); //將url的圖片下載到本地,這個(gè)方法在下邊 //修改為圖片存放路徑 file1[i] = new File("D:/upload/"+imagepath.substring(imagepath.lastIndexOf("/"))); } filesDown(request, response, file1);//這是下邊的一個(gè)方法 } }  //將下載到 服務(wù)器 的圖片 放入壓縮包 public void filesDown(HttpServletRequest request,HttpServletResponse response,File[] file1 ) throws Exception { Random r=new Random(); String tmpFileName =r.nextInt(10000) +"downImage.zip";  String upath=request.getRealPath("/"); upath=upath.substring(0,upath.length()-1); upath=upath.substring(0,upath.lastIndexOf("\\")); //服務(wù)地址的存放路徑 String FilePath = upath+"/ROOT/data/"; File f=new File(FilePath); if(!f.exists()){ //路徑不存在就創(chuàng)建 FileUtil.createDir(FilePath); }   byte[] buffer = new byte[1024];   String strZipPath = FilePath + tmpFileName; //路徑加文件名  try {    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipPath));    for (int i = 0; i < file1.length; i++) {     FileInputStream fis = new FileInputStream(file1[i]);     out.putNextEntry(new ZipEntry(file1[i].getName()));     //設(shè)置壓縮文件內(nèi)的字符編碼,不然會(huì)變成亂碼     out.setEncoding("GBK");         int len;     // 讀入需要下載的文件的內(nèi)容,打包到zip文件     while ((len = fis.read(buffer)) > 0) {      out.write(buffer, 0, len);     }     out.closeEntry();     fis.close();   }    out.close();    //下載的服務(wù)地址根據(jù)實(shí)際情況修改   boolean dflag = downloafile(request, response,"http://localhost:8080/data/"+tmpFileName);   //將服務(wù)器上 壓縮前的源文件 刪除   for (int i = 0; i < file1.length; i++) {  if (file1[i].isFile()) {  file1[i].delete(); }   }   //將服務(wù)器上的壓縮包刪除   File fileZip=new File(strZipPath);   fileZip.delete();  } catch (Exception e) {   e.printStackTrace();  }  }   //寫(xiě)到本地 public boolean downloafile(HttpServletRequest request,HttpServletResponse response, String path) { String name = path.substring(path.lastIndexOf("/")+1); String filename = DownLoadImageUtil.encodeChineseDownloadFileName(request, name); response.setHeader("Content-Disposition", "attachment; filename=" + filename + ";"); boolean flag = false; try { URL url = new URL(path);   InputStream inStream = url.openConnection().getInputStream(); BufferedInputStream in = new BufferedInputStream(inStream); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] temp = new byte[1024]; int size = 0; while ((size = in.read(temp)) != -1) { out.write(temp, 0, size); } in.close(); ServletOutputStream os = response.getOutputStream(); os.write(out.toByteArray()); os.flush(); os.close(); flag = true; } catch (Exception e) { logger.error("違法信息下載...出錯(cuò)了"); } return flag; }

makeImage(); 方法(如果是服務(wù)器上的圖片,可以省略這一步,直接打包)

/**   * 下載圖片,并按照指定的路徑存儲(chǔ)   * @param bean   * @param filePath   */  public void makeImage( String filePath) {   // 網(wǎng)絡(luò)請(qǐng)求所需變量   try {    //new一個(gè)URL對(duì)象    URL url = new URL(filePath);    //打開(kāi)鏈接    HttpURLConnection conn = (HttpURLConnection)url.openConnection();    //設(shè)置請(qǐng)求方式為"GET"    conn.setRequestMethod("GET");    //超時(shí)響應(yīng)時(shí)間為5秒    conn.setConnectTimeout(5 * 1000);    //通過(guò)輸入流獲取圖片數(shù)據(jù)    InputStream inStream = conn.getInputStream();       ByteArrayOutputStream outStream = new ByteArrayOutputStream();    //創(chuàng)建一個(gè)Buffer字符串    byte[] buffer = new byte[1024];    //每次讀取的字符串長(zhǎng)度,如果為-1,代表全部讀取完畢    int len = 0;    //使用一個(gè)輸入流從buffer里把數(shù)據(jù)讀取出來(lái)    while( (len=inStream.read(buffer)) != -1 ){     //用輸出流往buffer里寫(xiě)入數(shù)據(jù),中間參數(shù)代表從哪個(gè)位置開(kāi)始讀,len代表讀取的長(zhǎng)度     outStream.write(buffer, 0, len);    }    byte []data=outStream.toByteArray();   //先將圖片從url下載到服務(wù)器的D:/upload/   File imageFile = new File("D:/upload/"+filePath.substring(filePath.lastIndexOf("/")));    //創(chuàng)建輸出流    FileOutputStream foutStream = new FileOutputStream(imageFile);    foutStream.write(data);    //關(guān)閉輸出流    foutStream.close();   inStream.close();     } catch (MalformedURLException e) {    e.printStackTrace();   } catch (IOException e) {    e.printStackTrace();   }  }

看完上述內(nèi)容,你們對(duì)java中怎么從網(wǎng)絡(luò)下載多個(gè)文件有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI