JAVA如何實現(xiàn)上傳下載

本篇內(nèi)容介紹了“JAVA如何實現(xiàn)上傳下載”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供嵩明網(wǎng)站建設(shè)、嵩明做網(wǎng)站、嵩明網(wǎng)站設(shè)計、嵩明網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、嵩明企業(yè)網(wǎng)站模板建站服務(wù),十余年嵩明做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

  public class FileUtil {
	/**
	*
	* Description: 復制文件
	*
	 * [@param](https://my.oschina.net/u/2303379) srcFile 上傳文件
	 * [@param](https://my.oschina.net/u/2303379) filePath 保存文件地址
	* [@return](https://my.oschina.net/u/556800)
	* [@date](https://my.oschina.net/u/2504391) 2019/1/18
	*/
   public static void copyFile(MultipartFile srcFile, String filePath) throws Exception {
	  // 判斷是否上傳文件
	  if (srcFile == null || srcFile.isEmpty() || StringUtils.isBlank(filePath)) {
		 throw new NullPointerException("上傳文件為空");
	  }
	  InputStream inputStream = srcFile.getInputStream();
	  File file = new File(filePath);
	  if (!file.exists()) {
		 file.getParentFile().mkdirs();
	  }
	  FileOutputStream outputStream = new FileOutputStream(file);
	  try {
		 IOUtils.copy(inputStream, outputStream);
	  } finally {
		 IOUtils.closeQuietly(inputStream);
		 IOUtils.closeQuietly(outputStream);
	  }
   }
}




/**
 * Description: 從本地下載PDF
 *
 * [@param](https://my.oschina.net/u/2303379) fileName
 *            文件名
 * @return
 * @date 2018年02月01日
 */
public static boolean copyPdf(HttpServletRequest request, HttpServletResponse response, String filePath,
		String fileName) {
   File file = new File(filePath + fileName);
   // 文件不存在,直接返回
   if (!file.exists()) {
	  return false;
   }
   BufferedInputStream inputStream = null;
   BufferedOutputStream outputStream = null;
   try {
	  setDownLoadResponseHeader(request, response, fileName);
	  inputStream = new BufferedInputStream(new FileInputStream(file));
	  outputStream = new BufferedOutputStream(response.getOutputStream());
	  IOUtils.copy(inputStream, outputStream);
   } catch (IOException e) {
	  e.printStackTrace();
   } finally {
	  try {
		 if (inputStream != null) {
			inputStream.close();
		 }
		 if (outputStream != null) {
			outputStream.close();
		 }
	  } catch (IOException e) {
		 e.printStackTrace();
	  }
   }
   return true;
}

“JAVA如何實現(xiàn)上傳下載”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

當前題目:JAVA如何實現(xiàn)上傳下載
網(wǎng)站URL:http://muchs.cn/article6/gdegog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化商城網(wǎng)站、ChatGPT、移動網(wǎng)站建設(shè)網(wǎng)站導航、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名