CreateTime--2017年9月7日10:25:33
Author:Marydon
struts2實現文件查看、下載
1.界面展示
<a style="color: #199ED8;" target="_blank" href="<c:url value="/telemedicine/reseCons/viewFile.do?fileName=201516529IO.jpg"/>">查看</a> <a style="color: #199ED8;" target="_blank" href="<c:url value="/telemedicine/reseCons/downloadFile.do?fileName=201516529IO.jpg"/>">下載</a>
2.struts2配置
<!-- 文件預覽 --> <action name="viewFile" class="telemedicine.web.actions.reseCons.FileOperationAction"method="viewFile"><result name="success" type="stream"><!-- 設置返回的文件類型 --><param name="contentType">${contentType}</param> <!-- 設置獲取流的方法 --><param name="inputName">inputStream</param> <!-- bufferSize 設置緩沖區字節大小默認是1024 --></result> </action> <!-- 文件下載 --> <action name="downloadFile" class="telemedicine.web.actions.reseCons.FileOperationAction"method="downloadFile"><result name="success" type="stream"><!-- 設置返回的文件類型 --><param name="contentType">${contentType}</param> <!-- 設置獲取文件流的方法 --><param name="inputName">inputStream</param> <!--添加參數,即就是下載的名稱--> <param name="contentDisposition">${contentDisposition}</param><!-- bufferSize 設置緩沖區字節大小默認是1024 --></result> </action>
說明:struts2使用${}方式動態從action類中取值
3.action(控制器)
3.1 供struts2取值
// 文件輸入流 private ByteArrayInputStream inputStream; // 返回內容類型 private String contentType; // 文件下載時,指定的名稱 private String contentDisposition;public ByteArrayInputStream getInputStream() {return inputStream; }public String getContentType() {return contentType; }public String getContentDisposition() {return contentDisposition; }
3.2 詳細代碼
需要導入:
import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.InputStream; import java.util.Map; import org.apache.struts2.ServletActionContext;
?
/*** 文件預覽 * @return*/ public String viewFile() {try {// 1.獲取客戶端提交參數String fileName = WebUtils.getParameter("fileName");// 2.獲取文件路徑String filePath = "WEB-INF/uploadFiles/" + fileName;// 獲取真實路徑filePath = WebUtils.getRealPath(filePath);// 字節輸出流ByteArrayOutputStream bos = new ByteArrayOutputStream();// 3.將文件轉換成文件流// 如果文件不存在,會拋出異常FileInputStream fis = new FileInputStream(filePath);// 4.將文件流讀取到緩沖區(內存中),目的:提高讀取效率InputStream input = new BufferedInputStream(fis);// 5.指定內存空間大小byte[] bt = new byte[1024];int len = 0;// 6.從內存中每次讀取1024個字節,放到字節數組bt中,然后將bt中的字節寫入到輸出流中while ((len = input.read(bt)) > 0) {bos.write(bt, 0, len);}// 7.私有屬性賦值// 7.1 字節輸入流this.inputStream = new ByteArrayInputStream(bos.toByteArray());// 7.2獲取該文件所對應的文件類型this.contentType = WebUtils.getServletContext().getMimeType(fileName);bos.close();input.close();} catch (Exception e) {this.addMessage(-1, e.getMessage());this.msg = "" + getExceptionMessage(e);this.code = -1;log.error(e.getMessage());e.printStackTrace();}return SUCCESS; }/*** 文件下載* @return*/ public String downloadFile() {try {// 1.獲取客戶端提交參數String fileName = WebUtils.getParameter("fileName");// 2.獲取文件路徑String filePath = "WEB-INF/uploadFiles/" + fileName;// 獲取真實路徑filePath = WebUtils.getRealPath(filePath);// 字節輸出流ByteArrayOutputStream bos = new ByteArrayOutputStream();// 3.將文件轉換成文件流// 如果文件不存在,會拋出異常FileInputStream fis = new FileInputStream(filePath);// 4.將文件流讀取到緩沖區(內存中),目的:提高讀取效率InputStream input = new BufferedInputStream(fis);// 5.指定內存空間大小byte[] bt = new byte[1024];int len = 0;// 6.從內存中每次讀取1024個字節,放到字節數組bt中,然后將bt中的字節寫入到輸出流中while ((len = input.read(bt)) > 0) {bos.write(bt, 0, len);}// 7.私有屬性賦值// 7.1 字節輸入流this.inputStream = new ByteArrayInputStream(bos.toByteArray());// 7.2獲取該文件所對應的文件類型this.contentType = WebUtils.getServletContext().getMimeType(fileName);// 7.3指定下載該文件時的文件名稱this.contentDisposition = "attachment;fileName=" + fileName;bos.close();input.close();} catch (Exception e) {this.addMessage(-1, e.getMessage());this.msg = "" + getExceptionMessage(e);this.code = -1;log.error(e.getMessage());e.printStackTrace();}return SUCCESS; }
說明:
其中,通過WebUtils.java類調用的方法,請依次移步至文章:struts2獲取前臺提交的參數,struts2獲取文件真實路徑和struts2獲取ServletContext對象
?