//将数据保存到临时文件
public String wordPrint(File file,HttpServletRequest request, HttpServletResponse response)
throws IOException {
InputStream fin = null;
ServletOutputStream out = null;
try {
fin = new FileInputStream(file);
response.setCharacterEncoding("utf-8");
response.setContentType("application/msword");
// 设置浏览器以下载的方式处理该文件名
response.setHeader("Content-Disposition", "attachment;filename="
.concat(String.valueOf(URLEncoder.encode(file.getName(), "UTF-8"))));
out = response.getOutputStream();
byte[] buffer = new byte[2048]; // 缓冲区
int bytesToRead = -1;
// 通过循环将读入的Word文件的内容输出到浏览器中
while((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} finally {
if(fin != null) fin.close();
if(out != null) out.close();
if(file != null) file.delete(); // 删除临时文件
}
return null;
}