前台:
<a href="${ctx}/renhang/tbPbocFile/downloadFile?id=${tbPbocFile.id}">下载文件</a>
后台:
@RequestMapping(value = "downloadFile")public String downloadFile(TbPbocFile tbPbocFile, HttpServletRequest request, HttpServletResponse response){DataInputStream in = null;OutputStream out = null;response.reset();//try {response.setCharacterEncoding("UTF-8");response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(tbPbocFile.getFileName(),"UTF-8"));// 设定输出文件头response.setContentType("application/"+tbPbocFile.getFileName().split(".")[1]);// 定义输出类型//输入流:本地文件路径in = new DataInputStream(new FileInputStream(new File("E:\\" + tbPbocFile.getFileName())));//输出流out = response.getOutputStream();//输出文件int bytes = 0;byte[] bufferOut = new byte[1024];while ((bytes = in.read(bufferOut)) != -1) {out.write(bufferOut, 0, bytes);}}catch (Exception e){logger.error("下载文件出错:",e);e.printStackTrace();}return null;}
记录一下。