package com.eaoa.uc.attachment.action;
import java.io.InputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.eaoa.base.EABaseAction;
import com.eaoa.query.PubAttachmentQuery;
import com.eaoa.vo.PubAttachment;
import com.stony.core.util.CoreUtils;
/**
* Find a attachment from table "EAPT_ATTACHMENT" by attachmentno, and show it on page.
* @author zhenglun.zhou
*
*/
public class AttachmentViewAction extends EABaseAction {
protected ActionForward doWork(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String attachmentnostr = request.getParameter("attachmentno");
if(StringUtils.isBlank(attachmentnostr))
return null;
Long attachmentno = CoreUtils.parseLong(attachmentnostr);
PubAttachment attachment = PubAttachmentQuery.loadByNo(attachmentno);
//扩展名和文件名
String extname = attachment.getExtendname().toLowerCase();
String filename = attachment.getAttachmentname();
//设置response
setIsStream(response);
response.reset(); //清除response的buffer
if (extname.indexOf("bmp,jpeg,gif") != -1) {
response.setContentType("image/"+extname);
} else if ("jpg".equals(extname)) {
response.setContentType("image/jpeg");
} else if ("xls".equals(extname)) {
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment; filename="+filename);
} else if ("doc".equals(extname)) {
response.setContentType("application/msword; charset=gb2312");
response.setHeader("Content-disposition", "attachment; filename="+filename);
} else if ("ppt".equals(extname)) {
response.setContentType("application/vnd.ms-powerpoint; charset=gb2312");
response.setHeader("Content-disposition", "attachment; filename="+filename);
}else{
response.setHeader("Content-disposition", "attachment; filename="+filename);
}
//输出流
InputStream ins = null;
try {
ins = attachment.getContent().getBinaryStream();
ServletOutputStream outs = response.getOutputStream();
int blobsize = (int) attachment.getContent().length();
byte[] blobbytes = new byte[blobsize];
int bytesRead = 0;
while ((bytesRead = ins.read(blobbytes)) != -1) {
outs.write(blobbytes, 0, bytesRead);
}
ins.close();
outs.flush();
outs.close();
} catch (Exception e) {
}
return null;
}
}
分享到:
相关推荐
FTP上传,删除,下载文件的工具类(可供Action/Controller层使用) FtpUtil.uploadFileFromProduction(hostname, port, username, password, pathname, filename, originfilename);
自己用 FinalBuilder Action Studio 自定义修改INI文件值Action,源码
Spring in Action CN.001<br>Spring in Action CN.002<br>Spring in Action ...Action CN.007<br>Spring in Action CN.008<br>Spring in Action CN.BAT<br><br>Spring in Action 中文版的pdf文件较大... <br>本书信息: Craig Walls,...
Android的ACTION说明文档,详细介绍了Intent调用的各个ACTION!
Struts2上传和下载Action配置 上传:获取名称和类型,复制到固定文件里 下载:该方法的返回值为一个InputStream流。
Kubernetes教程
matlab环境下,将UTKinect-Action数据库中xml数据读取成mat文件
Spring in Action CN.001<br>Spring in Action CN.002<br>Spring in Action ...Action CN.007<br>Spring in Action CN.008<br>Spring in Action CN.BAT<br><br>Spring in Action 中文版的pdf文件较大... <br>本书信息: Craig Walls,...
Spring in Action(第二版)中文版 pdf,给喜欢的人,网上很多,但是都是要积分,我就烦那些这样赚积分的人,于是我就用积分下载后免费提供。我上传的文件大小限制了,我分两个压缩包上传,全部免费。气死那些要积分的...
Lucene in Action帮助文档
资源中示例了一个struts2的配置文件中配置的action的result结果为另外一个action的配置,该跳转的过程中是带所有参赛调整的,在另外一个action中可以接受到当前action的所有参数,此配置文件是我日常开发中的经验...
ActionForward and ActionMapping
Struts2中关于action不能用forward的问题,文档里面有正确的解决方法
spring in action 第四版 spring in action 第四版 spring in action 第四版
Action和类型转换
有Struts 1.x经验的朋友都知道Action是Struts的核心内容,当然Struts 2.0也不例外。不过,Struts 1.x与Struts 2.0的Action模型很大的区别 本文档对struts2.0 action进行了详细介绍
mule in action 和doc文档详细介绍 Mule的核心组件是UMO(Universal Message Objects,从Mule2.0开始UMO这一概念已经被组件Componse所代替),UMO实现整合逻辑。UMO可以是POJO,JavaBean等等。它支持30多种传输协议...
Spring.in.Action.rar 文档及源代码
ElasticSearch实战 in Action(原著原版)压缩包,ElasticSearch文档,ES全文检索从入门到精通,高清PDF,是学习es和精通es必备手册,能快速达到精通ElasticSearch,一册在手,ElasticSearch无忧,开发、学习、调优...