springmvc 文件下载报错 getOutputStream() has already been called for this response

springmvc 文件下载报错 getOutputStream() has already been called for this response,第1张

使用struts2 进行文件下载是,总报错

java.lang.IllegalStateException: getOutputStream() has already been called for this respons

解决办法:

把对应的action的返回设置为空,即可轻松解决。

例如:

public class DownloadFileAction extends ActionSupport implements

ServletRequestAware, ServletResponseAware {

/**

*

*/

private static final long serialVersionUID = -7448748577778248376L

private HttpServletRequest request

private HttpServletResponse response

private String savePath

@Override

public String execute() throws Exception {

String fileName=request.getParameter("fileName")

String fullPath=getSavePath()+"//"+fileName

fileName=new String(fileName.getBytes("utf-8"),"iso-8859-1")

InputStream is=new FileInputStream(fullPath)

int len=0

byte []buffers=new byte[1024]

response.reset()

response.setContentType("application/x-msdownload")

response.addHeader("Content-Disposition", "attachmentfilename=\""+fileName+"\"")

OutputStream os = null

//把文件内容通过输出流打印到页面上供下载

while((len=is.read(buffers))!=-1){

os=response.getOutputStream()

os.write(buffers, 0, len)

}

is.close()

os.flush()

//return SUCCESS//会报错:java.lang.IllegalStateException: getOutputStream() has already been called for this respons

return null//ok

}

public void setServletRequest(HttpServletRequest req) {

this.request=req

}

public void setServletResponse(HttpServletResponse resp) {

this.response=resp

}

@SuppressWarnings("deprecation")

public String getSavePath() {

return request.getRealPath(savePath)

}

public void setSavePath(String savePath) {

this.savePath = savePath

}

}

我们后台是用hibernate实现的

将数据库对应的实体的类型设为blob类型

用hibernate将二进制流转为blob类型

Hibernate.createBlob(inputStream)转为blob


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7953988.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存