struts2檔案上傳下載
阿新 • • 發佈:2018-12-22
package com.zking.five; import java.awt.image.BufferedImage; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.coyote.http11.filters.BufferedInputFilter; import com.zking.test.web.BaseAction; /** * 檔案上傳的三種方式 * 1.上傳的圖片以二進位制儲存到資料庫(activiti,jbpm工作框架) oa系統 * 2.將檔案上傳到指定到伺服器的硬碟(cpu快,硬碟較大) * 3.將檔案上傳到tomcat所在伺服器(對應的靜態資源伺服器--上線不重啟) * * @author Administrator * */ public class fileAction extends BaseAction { // 這是jsp傳遞過來的具體檔案 private File file; // 檔名 private String fileFileName; // 檔案型別 private String fileContentType; // 虛擬路徑 private String serverDir = "/upload"; /** * 上傳圖片至伺服器 * * @return * @throws IOException */ public String upload() throws IOException { System.out.println("fileFileName:" + fileFileName); System.out.println("fileContentType:" + fileContentType); String realPath = getRealPath(serverDir + "/" + fileFileName); System.out.println("真實路徑:" + realPath); FileUtils.copyFile(file, new File(realPath)); return "success"; } /** * 獲取資料夾的真實路徑 * * @param path * @return */ private String getRealPath(String path) { return this.request.getServletContext().getRealPath(path); } /** * 下載圖片 * * @return * @throws IOException */ public String saveAs() throws IOException { // 從資料庫獲取fileName,fileType String fileName = "s1.jpg"; String fileType = "image/jpg"; response.setContentType(fileType); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); String realPath = getRealPath(serverDir + "/" + fileName); // 從伺服器獲取圖片寫到本地 FileUtils.copyFile(new File(realPath), response.getOutputStream()); return null; } /** * 直接在頁面開啟圖片 * * @return * @throws IOException */ public String openAs() throws IOException { // 從資料庫獲取fileName,fileType String fileName = "s2.jpg"; String fileType = "image/jpg"; response.setContentType(fileType); response.setHeader("Content-Disposition", "filename=" + fileName); String realPath = getRealPath(serverDir + "/" + fileName); // 從伺服器獲取圖片寫到本地 // FileUtils.copyFile(new File(realPath), response.getOutputStream()); BufferedInputStream in =new BufferedInputStream(new FileInputStream(new File(realPath))); BufferedOutputStream out=new BufferedOutputStream(response.getOutputStream()); copyBufStream(in, out); return null; } /** * copy二進位制檔案用的 * @param in 從那裡來 * @param out 寫到那裡去 * @throws IOException */ private void copyBufStream(BufferedInputStream in,BufferedOutputStream out) throws IOException { byte[] buf=new byte[1024]; int len=0; while((len=in.read(buf))!=-1) { out.write(buf,0,len); } in.close(); out.close(); } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } }