Map map = getSearchValue();
File excelFile = orderService.getexportexcel(id,map);
InputStream is = null;
OutputStream os = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//以流的形式输出文件
try {
if (excelFile.exists()) {
String fileName = "订单.xls";
is = new FileInputStream(excelFile);
HttpServletResponse response = ServletActionContext
.getResponse();
os = response.getOutputStream();
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(os);
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
// fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/ vnd.ms-excel");// 不同类型的文件对应不同的MIME类型
response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
}
bos.flush();
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally{
IOUtils.closeQuietly(bis);
IOUtils.closeQuietly(bos);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
javaweb reponse 写出文件
最新推荐文章于 2023-06-28 14:57:17 发布