qq_18296295 2018-04-09 11:01 采纳率: 40%
浏览 4463
已采纳

服务端生成一个word文件 如何返回下载链接

我在程序里生成一个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();   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致