springmvc 文件下载报错 getOutputStream() has already been called for this response
publicvoiddownload(StringfilePath,HttpServletResponseresponse){try{Filefile=newFile(f...
public void download(String filePath,HttpServletResponse response){
try {
File file = new File(filePath);
String fileName = filePath.substring(filePath.lastIndexOf("/")+1);//得到文件名
response.setContentType("application/octet-stream");//告诉浏览器输出内容为流
response.addHeader("Content-Disposition", "attachment;filename="+fileName);
String len = String.valueOf(file.length());
response.setHeader("Content-Length", len);//设置内容长度
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int n;
while((n=in.read(b))!=-1){
out.write(b, 0, n);
}
in.close();
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} 展开
try {
File file = new File(filePath);
String fileName = filePath.substring(filePath.lastIndexOf("/")+1);//得到文件名
response.setContentType("application/octet-stream");//告诉浏览器输出内容为流
response.addHeader("Content-Disposition", "attachment;filename="+fileName);
String len = String.valueOf(file.length());
response.setHeader("Content-Length", len);//设置内容长度
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(file);
byte[] b = new byte[1024];
int n;
while((n=in.read(b))!=-1){
out.write(b, 0, n);
}
in.close();
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} 展开
展开全部
使用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;
}
}
追问
试过了 还是不可以
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询