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

如何实现下载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]

    点赞 打赏 评论

相关推荐 更多相似问题