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对象​​

 

写在最后

  哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

作者:​​Marydon​​