FileDowloadITeye - 威尼斯人

FileDowloadITeye

2019年02月28日14时54分20秒 | 作者: 寄南 | 标签: 途径,获取,绝对 | 浏览: 2189

public HttpServletResponse download(String path,HttpServletRequest request, HttpServletResponse response) { StringBuffer sb=new StringBuffer(); try { //File file = new File(request.getSession().getServletContext().getRealPath("/") + "/" + path); File file=new File(path); String filename = file.getName(); //System.out.println("绝对途径:"+file.getAbsolutePath()); sb.append("1绝对途径:"+file.getAbsolutePath()+"\n"); int length=0; InputStream fis = new BufferedInputStream(new FileInputStream(file.getAbsoluteFile())); sb.append("2获取io正确\n"); byte[] buffer = new byte[1024]; response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO-8859-1")); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); sb.append("3写io正确\n"); while((length=fis.read(buffer))!=-1){ toClient.write(buffer, 0, length); sb.append("4写io完结\n"); fis.close(); toClient.flush(); toClient.close(); sb.append("5悉数完结\n"); } catch (IOException ex) { sb.append("获取反常信息:\n"+ex.getMessage()+"\n"); sb.append("获取本地反常:\n"+ex.getLocalizedMessage()); IfoContent.sys(sb.toString()); return response; public HttpServletResponse download1(String path, HttpServletRequest request, HttpServletResponse response) { try { // path是指欲下载的文件的途径。 a href="Download?filename=笔试题" 哈哈,测验文件下载 /a // File file = new // File(request.getSession().getServletContext().getRealPath("/") + // "/" + path); File file = new File("f:/唐超.doc"); String filename = file.getName(); InputStream fis = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO-8859-1")); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response .getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (IOException ex) { ex.printStackTrace(); return response;

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表威尼斯人立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章