Struts2文件的上傳和下載

ccpp 9年前發布 | 3K 次閱讀 Java Struts2

/**

  • 單個文件上傳
  • @param is
  • @param fileName
  • @param filePath */ public static void upFile(File uploadFile, String fileName, String filePath) {

FileOutputStream fos = null; BufferedOutputStream bos = null; FileInputStream is = null; BufferedInputStream bis = null; File file = new File(filePath); if (!file.exists()) { file.mkdirs(); } File f = new File(filePath + "/" + fileName); try { is = new FileInputStream(uploadFile); bis = new BufferedInputStream(is); fos = new FileOutputStream(f); bos = new BufferedOutputStream(fos); byte[] bt = new byte[4096]; while ((bis.read(bt)) > 0) { bos.write(bt); } } catch (Exception e) { e.printStackTrace(); } finally {

try { if (null != bos) { bos.close(); bos = null; } if (null != fos) { fos.close(); fos = null; } if (null != is) { is.close(); is = null; }

if (null != bis) { bis.close(); bis = null; } } catch (Exception e) { e.printStackTrace(); } } }

/**

  • 文件下載
  • @param response
  • @param downloadFile */ public static void downloadFile(HttpServletRequest request, HttpServletResponse response, String downloadFile, String fileName) {

BufferedInputStream bis = null; InputStream is = null; OutputStream os = null; BufferedOutputStream bos = null; try { File file = new File(downloadFile); // :文件的聲明 is = new FileInputStream(file); // :文件流的聲明 os = response.getOutputStream(); // 重點突出 bis = new BufferedInputStream(is); bos = new BufferedOutputStream(os);

if (request.getHeader("User-Agent").toLowerCase() .indexOf("firefox") > 0) { fileName = new String(fileName.getBytes("GB2312"), "ISO-8859-1"); } else { // 對文件名進行編碼處理中文問題 fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 處理中文文件名的問題 fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 處理中文文件名的問題 }

response.reset(); // 重點突出 response.setCharacterEncoding("UTF-8"); // 重點突出 response.setContentType("application/x-msdownload");// 不同類型的文件對應不同的MIME類型 // // 重點突出 // inline在瀏覽器中直接顯示,不提示用戶下載 // attachment彈出對話框,提示用戶進行下載保存本地 // 默認為inline方式 response.setHeader("Content-Disposition", "attachment;filename="

  • fileName); // response.setHeader("Content-Disposition", // "attachment; filename="+fileName); // 重點突出 int bytesRead = 0; byte[] buffer = new byte[4096];// 4k或者8k while ((bytesRead = bis.read(buffer)) != -1) { // 重點 bos.write(buffer, 0, bytesRead);// 將文件發送到客戶端 }

} catch (Exception ex) { throw new RuntimeException(ex.getMessage()); } finally { // 特別重要 // 1. 進行關閉是為了釋放資源 // 2. 進行關閉會自動執行flush方法清空緩沖區內容 try { if (null != bis) { bis.close(); bis = null; } if (null != bos) { bos.close(); bos = null; } if (null != is) { is.close(); is = null; } if (null != os) { os.close(); os = null; } } catch (Exception ex) { throw new RuntimeException(ex.getMessage()); } } }

/**

  • 文件下載
  • @param response
  • @param downloadFile */ public static void downloadFile(HttpServletResponse response, String downloadFile, String showFileName) {

BufferedInputStream bis = null; InputStream is = null; OutputStream os = null; BufferedOutputStream bos = null; try { File file = new File(downloadFile); // :文件的聲明 String fileName = file.getName(); is = new FileInputStream(file); // :文件流的聲明 os = response.getOutputStream(); // 重點突出 bis = new BufferedInputStream(is); bos = new BufferedOutputStream(os); // 對文件名進行編碼處理中文問題 fileName = java.net.URLEncoder.encode(showFileName, "UTF-8");// 處理中文文件名的問題 fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 處理中文文件名的問題 response.reset(); // 重點突出 response.setCharacterEncoding("UTF-8"); // 重點突出 response.setContentType("application/x-msdownload");// 不同類型的文件對應不同的MIME類型 // // 重點突出 // inline在瀏覽器中直接顯示,不提示用戶下載 // attachment彈出對話框,提示用戶進行下載保存本地 // 默認為inline方式 response.setHeader("Content-Disposition", "attachment; filename="

  • fileName); // 重點突出 int bytesRead = 0; byte[] buffer = new byte[1024]; while ((bytesRead = bis.read(buffer)) != -1) { // 重點 bos.write(buffer, 0, bytesRead);// 將文件發送到客戶端 }

} catch (Exception ex) { throw new RuntimeException(ex.getMessage()); } finally { // 特別重要 // 1. 進行關閉是為了釋放資源 // 2. 進行關閉會自動執行flush方法清空緩沖區內容 try { if (null != bis) { bis.close(); bis = null; } if (null != bos) { bos.close(); bos = null; } if (null != is) { is.close(); is = null; } if (null != os) { os.close(); os = null; } } catch (Exception ex) { throw new RuntimeException(ex.getMessage()); } } }</pre>

 本文由用戶 ccpp 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!