@RequestMapping("/downloadFile.htm")//文件下载 @ResponseBody public void file2Stream( Map<String, Object> requestMap, HttpServletResponse response,HttpServletRequest request) { InputStream iStream = null; OutputStream outStrem = null; String url=request.getParameter("url").trim();//获取前端传来的起始年度 try { String[] filename=url.split("/"); writeFilesService.downFile(url, filename[filename.length-1], request, response); } catch (Exception e) { e.printStackTrace(); }finally { if(iStream != null){ try { iStream.close(); iStream = null; } catch (IOException e) { e.printStackTrace(); } } if(outStrem != null){ try { outStrem.close(); outStrem = null; } catch (IOException e) { e.printStackTrace(); } } } } serviceImplement public HttpServletResponse downFile(String path1,String fileName,HttpServletRequest request,HttpServletResponse response) throws Exception { request.setCharacterEncoding("UTF-8"); // String ctxPath = request.getSession().getServletContext().getRealPath("") // + File.separator + "export_table"+ File.separator; // path1 =ctxPath+path1; File file =new File(path1); System.out.println(path1); //String filename = file.getName(); try { InputStream fis = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[fis.available()]; try { fis.read(buffer); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } response.reset(); //URLEncoder.encode(path2,"UTF-8"); // if(fileName.length()>150){ // fileName = new String(path2.getBytes("GBK"),"ISO-8859-1"); response.addHeader("Content-disposition", "p_w_upload; filename="+ fileName); response.addHeader("Content-Length", String.valueOf(file.length())); OutputStream tc = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); tc.write(buffer); tc.flush(); tc.close(); // file.delete(); // } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return response; }
SpringMVC文件下载
精选 原创
©著作权归作者所有:来自51CTO博客作者lCarrieLiul的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:jQuery按钮置灰及恢复
下一篇:Java获取Excel单元格数据
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java和vue实现文件下载
java和vue实现文件下载
java vue -
springMVC 文件下载
5[/url]springMVC 文件下载[est.down.ut...
java 开发工具 spring MVC -
SpringMVC实现文件下载实践
基于SpringMVC的文件下载,xml不需要额外配置。默认使用HttpMessageConverter进行信息解析。具体点击查看HttpMessageConverter信息解析/**
spring mvc 文件下载 重定向 文件名 下载文件 -
SpringMVC文件上传与下载
文件的上传与下载在我们的日常工作中十分的常见,在项目开发中也是经常要用到的,springmvc可以更好地支持文件的上传和下载;但是springmvc
SpringMVC spring mvc xml -
SpringMVC 实现文件上传/下载
SpringMVC 实现文件上传/下载
字符串 16进制 spring