java 文件下载

后台:

/** *

 * @param fileId
* @param request
* @param response
*/
@RequestMapping(value = "/downFile", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public void downFile(@RequestParam String fileId,HttpServletRequest request, HttpServletResponse response) {

  //查询文件实体信息
// Map<String,String> map = jxhFileSerevice.queryFileById(fileId);

String path = "E:\file\新框架文件.docx";
File file = new File(path);
String fileName = "新框架文件.docx";
if (!file.exists()) {
System.out.println("没有找到: " + path);
return;
}
try {
String head = new String(fileName.getBytes(), "iso-8859-1");
response.setHeader("Content-Disposition", "attachment;filename=" + head);
//输入流:本地文件路径
FileInputStream in = new FileInputStream(file);
//输出流
OutputStream out = response.getOutputStream();
//输出文件
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
out.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


前端js
/** 下载文件 */
function dowmloadFile(fileId){
window.location.href='jxh/inner/serApiApply/downFile?fileId='+fileId;
//window.location.href ="/visits/dowLoadFileToPC?fileId="+obj;
}





 

posted @ 2019-09-27 17:24  superming168  阅读(164)  评论(0编辑  收藏  举报