我在程序里生成一个word放到 服务器里 比如放到D盘 然后要怎么返回这个word的 下载链接 然后我在本地直接访问链接就能下载
5条回答 默认 最新
- threenewbee 2018-04-09 11:31关注
如果d盘不是你web服务器的路径,你可以用FileInputStream
String basePath = request.getSession().getServletContext().getRealPath("d:/路径"); //System.out.println(basePath); String filedisplay = "文件名.doc"; String filedownload = basePath + File.separator + "文件名.doc"; System.out.println("----------------------"+filedownload); response.setContentType("applicaiton/x-download"); response.addHeader("Content-Disposition", "attachment;filename="+filedisplay); InputStream is = null; OutputStream os = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; is = new FileInputStream(new File(filedownload)); bis = new BufferedInputStream(is); os = response.getOutputStream(); bos = new BufferedOutputStream(os); byte[] b = new byte[1024]; int len = 0; while((len = bis.read(b)) != -1){ bos.write(b,0,len); } bis.close(); is.close(); bos.close(); os.close();
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报