java下载报错:getOutputStream() has already been called for this response

这是代码publicvoiddownFile(Stringfilepath,Stringfilename,HttpServletResponseresponse){res... 这是代码
public void downFile(String filepath, String filename,HttpServletResponse response) {
response.reset();
response.setContentType("application/x-msdownload");
String fileheader = filename;
try {
byte[] b = fileheader.getBytes("GBK");
fileheader = new String(b, "ISO8859_1");
} catch (UnsupportedEncodingException e2) {
e2.printStackTrace();
}
response.setHeader("content-disposition", "attachment; filename=\""
+ fileheader + "\" ");
FileInputStream fileinputstream;
try {
fileinputstream = new FileInputStream(filepath);
response.setCharacterEncoding("UTF-8");
try {
OutputStream out = response.getOutputStream();
把这个改成//PrintWriter out = response.getWriter();就不报错了。但是出现内容乱码怎么办
int i;
while ((i = fileinputstream.read()) != -1) {
out.write(i);
}

fileinputstream.close();

out.close();

} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
问题解决了,加上PrintWriter out = response.getWriter();
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setHeader("content-type","text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
展开
 我来答
娱乐这个feel倍爽儿
2015-07-11 · 人生如戏,戏如人生 娱百家事,乐万千户
娱乐这个feel倍爽儿
采纳数:47982 获赞数:334165

向TA提问 私信TA
展开全部
  出现getOutputStream() has already been called for this response异常的原因和解决方法:
  出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有妥善处理好的原因。
  解决的办法,在使用完输出流以后调用以下两行代码即可:
 
  out.clear();
  out = pageContext.pushBody();
pieryon
推荐于2016-08-22 · 知道合伙人数码行家
pieryon
知道合伙人数码行家
采纳数:14410 获赞数:166872
获取软件设计师高级职称 万达金融最佳创新奖

向TA提问 私信TA
展开全部
使用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", "attachment;filename=\""+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;
}

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
丶柃真幸机异其13
2012-06-11 · TA获得超过105个赞
知道小有建树答主
回答量:102
采纳率:0%
帮助的人:96.1万
展开全部
这个改成UTF-8的
fileheader = new String(b, "utf-8");
追问
改成这样,文件名乱码了,我把文件名改为英文,内容还是乱码
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式