描述:
nutz中间层把硬盘绝对地址的img或txt文件用流输出到一个新弹出的jsp中,报错如下:
2017-08-02 11:14:54,546 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) WARN - Error@/problemDemand/previewOfficeFile :
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:609)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:211)
at org.nutz.mvc.view.UTF8JsonView.render(UTF8JsonView.java:65)
at org.nutz.mvc.impl.processor.ViewProcessor.process(ViewProcessor.java:66)
百度上说:
因为jsp页面会调用JspWriter的out()方法将内容输出,同时我们的图片又调用了response.getOutputStream()方法因此会抛出这个异常。
这个何解啊?
请求的JS如下:
function previewFile(fileId,fileName){
var parent$ = self.parent.$;
if(parent$('#tabs').tabs('exists', fileName)){
parent$('#tabs').tabs('select', fileName);
}else{
$.ajax({
url: base + "/problemDemand/previewOfficeFile",
data: {
"fileId": fileId
},
dataType: "json",
async : false,
success: function(response) {
if(response.code == '1') {
//location.href = response.result;
//-------------预览弹窗开始------------
//var content = '<iframe scrolling="auto" frameborder="0" src="'+response.result+'" style="width:100%;height:100%;"></iframe>';
var content = '<iframe scrolling="auto" frameborder="0" src="/shlxoa/readonline2.jsp" style="width:100%;height:100%;"></iframe>';
parent$('#tabs').tabs('add',{ //实现add方法
title : fileName,
content : content,
//href : response.result,
closable : true
});
//-------------预览弹窗结束------------
} else {
//alert('出错了!');
$.messager.alert('提示:', '加载数据出错了!'+response.result);
}
}
});
}
}
ACTION如下:
@At("/previewOfficeFile")
public Object previewOfficeFile(@Param("fileId")String fileId) {
Object result = null;
//封装参数
NutMap re = new NutMap();
re.setv("fileId", fileId);
//向ComService层传入功能号和NutMap
result = comServices.ExcuteServices("B01.01.17", re);
return result;
//return Json.toJson(result);
}
SERVICE如下:
public Object previewOfficeFile(NutMap map) throws Exception {
long fileId = map.getLong("fileId");
UploadFile targetFile = dao.fetch(UploadFile.class, fileId);
//获取附件的文件名(用uuid命名)
String fullFileName = targetFile.getLocation() + targetFile.getUuidName() + targetFile.getExt();
if(problemDemandServiceBo.isOfficeFile(targetFile.getFileName())){
//情况一:是office文件,例如doc、docx、xls、xlsx、ppt等
//判断此目录下是否有preview文件夹
File previewDirectory = new File(targetFile.getLocation()+"preview");
//如果文件夹不存在则创建
if(!previewDirectory .exists() && !previewDirectory .isDirectory()){
System.out.println(targetFile.getLocation()+"下没有preview文件夹,新建一个...");
previewDirectory.mkdir();
}else{
System.out.println(targetFile.getLocation()+"下存在preview文件夹");
}
System.out.println("开始转换..."+fullFileName+"【"+targetFile.getFileName()+"】");
//生成swf文件,并返回路径
String swfFilePath = previewDirectory+"\\"+targetFile.getUuidName()+".swf";
//调用工具方法,根据源文件,生成pdf,再生成swf,并把swf的内容写入新建的swf文件内
String previewResultPath = Office2Swf.office2Swf(fullFileName, swfFilePath);
Mvcs.getHttpSession().setAttribute("previewFileName", new File(previewResultPath).getName());
//String oldName = targetFile.getFileName();
//Mvcs.getResp().setHeader("Content-Disposition", "attachment;filename="+java.net.URLEncoder.encode(oldName, "UTF-8"));
return Mvcs.getServletContext().getContextPath()+ "/readonline.jsp";
}else{
//情况二,是非office文件时,先从服务器硬盘绝对地址拿到图片、TXT,再向目标JSP写文件流
//写ContentType
Mvcs.getResp().setContentType("image/*"); // 设置返回的文件类
//读取文件
InputStream in = new FileInputStream(fullFileName);
OutputStream out = Mvcs.getResp().getOutputStream();
//写文件
int b;
while((b=in.read())!= -1)
{
out.write(b);
}
in.close();
out.close();
return Mvcs.getServletContext().getContextPath()+ "/readonline2.jsp";
//return new File(fullFileName );
}
}