首页 新闻 会员 周边

excel文件下载

0
悬赏园豆:20 [已解决问题] 解决于 2017-02-08 11:47
@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,不知道哪里出错了,求解
骚年,你渴望力量吗的主页 骚年,你渴望力量吗 | 初学一级 | 园豆:45
提问于:2016-12-19 10:36
< >
分享
最佳答案
-1

我自己解决了

骚年,你渴望力量吗 | 初学一级 |园豆:45 | 2017-02-08 11:45
其他回答(2)
0

Repose 有误,仔细看看

收获园豆:10
悦光阴 | 园豆:2251 (老鸟四级) | 2016-12-19 12:32

不晓得你说的哪?HttpServletResponse response?

0

文件输出的地方有错误。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]);        
        }

 

收获园豆:10
登顶 | 园豆:52 (初学一级) | 2016-12-23 15:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册