wing3501 2012-09-20 12:10
浏览 290
已采纳

如何实现下载PDF时弹出下载对话框?

需要两个链接,一个实现PDF在线打开,一个实现PDF下载。
在线打开
请问PDF下载该如何实现?
百度文库里的PDF是可以弹出下载对话框的。

  • 写回答

1条回答 默认 最新

  • jinnianshilongnian 2012-09-20 12:12
    关注

    首先给你之前我写的代码 自己改改吧。

    重点是 设置contentType为下载 否则如pdf默认是打开的
    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");

    [code="java"]package cn.javass.ssonline.upload.web.servlet;

    import cn.javass.ssonline.upload.util.UploadUtils;

    import java.io.*;
    import java.net.URLDecoder;
    import java.net.URLEncoder;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class DownloadServlet extends HttpServlet
    {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException
    {
    doPost(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
    //只允许有一个参数 filename

    String filename = new String(req.getQueryString().getBytes("ISO-8859-1"), "UTF-8").substring(9);
    if ((filename == null) || (filename.trim().length() == 0) || (filename.contains("\\.\\."))) {
      resp.setContentType("text/html;charset=utf-8");
      resp.getWriter().write("您下载的文件不存在!");
      return;
    }
    download(req, resp, filename);
    

    }

    private void download(HttpServletRequest request, HttpServletResponse response, String filename)
    {
    String userAgent = request.getHeader("User-Agent");
    boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);
    try
    {
    filename = URLDecoder.decode(filename, "UTF-8");

      String filePath = UploadUtils.getUploadDir(request) + filename;
    
      File file = new File(filePath);
      if ((!(file.exists())) || (!(file.canRead()))) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("您下载的文件不存在!");
        return;
      }
      byte[] buf = new byte[1024];
      int len = 0;
      BufferedInputStream br = null;
      OutputStream ut = null;
      response.reset();
    
      response.setHeader("Pragma", "No-cache");
      response.setHeader("Cache-Control", "must-revalidate, no-transform");
      response.setDateHeader("Expires", 0L);
    
      String displayFilename = filename.substring(filename.lastIndexOf("_") + 1);
      displayFilename = displayFilename.replace(" ", "_");
      response.setContentType("application/x-download");
    
      displayFilename = "[私塾在线学习网]"+displayFilename;
      if (isIE) {
        displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
      } else {
        displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
      }
    
      br = new BufferedInputStream(new FileInputStream(file));
      ut = response.getOutputStream();
      while ((len = br.read(buf)) != -1)
        ut.write(buf, 0, len);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    

    }
    }[/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求抢运动场地的插件
  • ¥15 jscolor 赋值input 没能引起前边色框的颜色变化
  • ¥100 驱动程序在\device\raidport1 上检测到控制器错误
  • ¥15 JS报错变量未定义,如何解决?
  • ¥20 找辅导 初学者 想实现一个项目 没有方向
  • ¥15 研究方向遥感场景识别分类。求深度学习基础课程
  • ¥15 关于渗漏场的电场分布模拟
  • ¥24 matlab怎么修改仿真初始时间
  • ¥15 两分段线性回归模型分析阈值效应
  • ¥15 前端和后端代码都没报错,但是点登录没反应的?