直接在controller中写的,前台使用的herf,因为是测试写的所以没有特别复杂
(动态往word中填数据以后用到会补充)
@SuppressWarnings("deprecation")
@RequestMapping("downLoad")
@ResponseBody
public void downLoad(HttpServletRequest request, ModelMap model, HttpSession session, HttpServletResponse response) {
String fileName ="bbb.doc"; //我的文件名
try {
request.setCharacterEncoding("utf-8");
fileName = new String(fileName.getBytes("iso-8859-1"), "utf-8");
//获取文件路径
String path = ServerDirUtil.dataTemplettRoot; //这个是util 获取文件的路径的
String inpath = path + "/" +fileName;
//设置向浏览器端传送的文件格式
response.setContentType("application/x-download");
fileName = CommonUtil.decode(fileName, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename="+ fileName);
FileInputStream fis = null;
OutputStream os = null;
try {
os = response.getOutputStream();
fis = new FileInputStream(inpath);
byte[] b = new byte[1024 * 10];
int i = 0;
while ((i = fis.read(b)) > 0) {
os.write(b, 0, i);
}
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}