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"); 展开
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"); 展开
3个回答
推荐于2016-08-22 · 知道合伙人数码行家
关注
展开全部
使用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;
}
}
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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个改成UTF-8的
fileheader = new String(b, "utf-8");
fileheader = new String(b, "utf-8");
追问
改成这样,文件名乱码了,我把文件名改为英文,内容还是乱码
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询