@RequestMapping(value = "/downloadExcel",method = RequestMethod.POST)
publicvoiddownloadExcel(HttpServletRequest request,HttpServletResponse response) throwsIOException {
String src = "\\download";
String filename = "成员信息表.xlsx";
String path = request.getSession().getServletContext().getRealPath(src);
FileInputStream input = null;
try{
input = newFileInputStream(newFile(path+"\\"+filename));
} catch(FileNotFoundException e) {
e.printStackTrace();
}
OutputStream out = response.getOutputStream();
response.setHeader("Content-Disposition",
"attachment; filename="
+ java.net.URLEncoder.encode(filename, "UTF-8"));//文件名 编码格式
response.setContentType("application/vnd.ms-excel"); //设置格式
//写文件
intflag = 0;
while((flag=input.read()) != -1)
{
out.write(flag);
}
out.flush();
out.close();
input.close();
}
问题:输出 flag 一直不归-1,不知道哪里出错了,求解
我自己解决了
Repose 有误,仔细看看
不晓得你说的哪?HttpServletResponse response?
文件输出的地方有错误。out.write(flag); flag是一个int值啊。
这样写,试试
int flag; byte buffer[]=new byte[1024]; while((flag=input.read(buffer))!=-1){ for(int i=0;i<flag;i++) out.write(buffer[i]); }