public void returnWord(File file,String uuid,HttpServletResponse response) throws IOException{
response.reset();
response.setContentType("application/msword;charset=GBK");
response.setHeader("Content-Disposition", (new StringBuilder(
"attachment; filename=")).append(uuid).toString());
FileInputStream fis = new FileInputStream(file);
BufferedInputStream buff = new BufferedInputStream(fis);
response.setContentLength((int) file.length());
byte[] b = new byte[1024];// 相当于我们的缓存
long k = 0;
OutputStream myout = response.getOutputStream();
while (k < file.length()) {
int j = buff.read(b, 0, 1024);
k += j;
myout.write(b, 0, j);
}
myout.flush();
if (buff != null) {
buff.close();
file.deleteOnExit();
}
}
response.reset();
response.setContentType("application/msword;charset=GBK");
response.setHeader("Content-Disposition", (new StringBuilder(
"attachment; filename=")).append(uuid).toString());
FileInputStream fis = new FileInputStream(file);
BufferedInputStream buff = new BufferedInputStream(fis);
response.setContentLength((int) file.length());
byte[] b = new byte[1024];// 相当于我们的缓存
long k = 0;
OutputStream myout = response.getOutputStream();
while (k < file.length()) {
int j = buff.read(b, 0, 1024);
k += j;
myout.write(b, 0, j);
}
myout.flush();
if (buff != null) {
buff.close();
file.deleteOnExit();
}
}
本文介绍了一个使用Java实现的导出Word文档的方法。该方法通过设置HTTP响应头及内容类型来支持在线下载功能,并利用字节流完成文件读写操作。具体步骤包括:初始化响应、设置内容类型为application/msword、指定下载文件名、创建文件输入流及缓冲区、设置输出流并逐块写入文件内容。
1177

被折叠的 条评论
为什么被折叠?



