struts2圖片上傳與下載
阿新 • • 發佈:2018-10-31
package com.zking.five.web; 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 com.zking.four.web.BaseAction; /** * 檔案上傳的三種方案: * 1、將上傳的檔案存放到資料庫,以二進位制的形式 oa系統 activity工作流框架 * 2、將檔案上傳到檔案伺服器(硬碟U足夠大)中 * 3、將檔案上傳到tomcat所在的普通的web伺服器 * * * 真實路徑與虛擬路徑的概念 * 1、所謂真實路徑指的是在自己電腦上找得到的路徑 * 2、所謂虛擬路徑指的是在自己電腦上找不到的路徑,在別人電腦裡面存在的路徑 * @author a * */ public class UploadAction extends BaseAction{ private File file;//變數名指的是jsp的name屬性,就是你要上傳的屬性 private String fileContentType; private String fileFileName; private String serverDir = "/uplaod"; /*public String execute() { System.out.println("hello Action"); return null; }*/ //檔案上傳 public String upload() { /** * srcFile 引數1:指的是本地檔案 * destFile 引數2:指的是在伺服器生成的檔案 */ String realPath = getRealPath(serverDir + "/" +fileFileName); System.out.println(fileContentType); System.out.println(fileFileName); System.out.println(realPath); try { FileUtils.copyFile(file, new File(realPath)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; } /** * 獲取Linux下的上傳檔案所在的位置 * @param path * @return */ private String getRealPath(String path) { // TODO Auto-generated method stub return application.getRealPath(path); } public String openAs() { String type = "image/jpeg" ; String name = "2.jpg"; response.setContentType(type); response.setHeader("Content-Disposition","filename=" + name); String realPath = getRealPath(serverDir + "/" +name); /** * 將遠端的圖片輸出到本地 * 資料來源inputstream:遠端 new file(realPath) * 目的:輸出到本地的jsp response.getoutputstream */ try { // FileUtils.copyFile(new File(realPath), response.getOutputStream()); BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(realPath))); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream()); copyStream(in, out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } private void copyStream(BufferedInputStream in, BufferedOutputStream out) { byte[] bbuf = new byte[1024]; int len = 0; try { while((len = in.read(bbuf))!=-1) { out.write(bbuf,0,len); } in.close(); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //檔案下載 public String download() { String type = "image/jpeg" ; String name = "2.jpg"; response.setContentType(type); response.setHeader("Content-Disposition","attachment;filename=" + name); String realPath = getRealPath(serverDir + "/" +name); /** * 將遠端的圖片輸出到本地 * 資料來源inputstream:遠端 new file(realPath) * 目的:輸出到本地的jsp response.getoutputstream */ try { FileUtils.copyFile(new File(realPath), response.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } }