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

服务端生成一个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条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样