java吧 关注:1,297,074贴子:12,834,473
  • 10回复贴,共1

用流读取pdf文件,在火狐上能直接显示,但是在IE8上提示保存

只看楼主收藏回复

这个怎么弄,2楼代码


IP属地:山东1楼2015-04-21 14:16回复
    String pdfName=null;
    try {
    pdfName = new String(request.getParameter("pdfName").getBytes("ISO-8859-1"), "UTF-8");
    } catch (UnsupportedEncodingException e2) {
    e2.printStackTrace();
    }
    response.setContentType("application/pdf");
    ServletOutputStream out=null;
    try {
    out = response.getOutputStream();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    //加上下面这句则可以在浏览器外打开,或者保存
    //resp.setHeader("Content-disposition", "attachment;filename=sample.pdf");
    File pdf = null;
    BufferedInputStream buf = null;
    String[] arr = pdfName.split("/");
    try{
    pdf = new File("C:\\"+arr[0]+"/"+arr[1]);
    List<File> fileName = listPath(pdf);
    for(int i = 0; i < fileName.size(); i++){
    String[] ar = fileName.get(i).toString().split("\\\\");
    if(ar[ar.length-1].equals(arr[2]+".pdf")){
    pdf = new File(fileName.get(i).toString());
    response.setContentLength((int)pdf.length());
    FileInputStream input = new FileInputStream(pdf);
    buf = new BufferedInputStream(input);
    int readBytes = 0;
    while((readBytes = buf.read()) != -1){
    out.write(readBytes);
    }
    }
    }
    } catch(IOException e){
    e.printStackTrace();
    } finally{
    try {
    if(out != null) out.close();
    if(buf != null) buf.close();
    }
    catch(Exception e){
    e.printStackTrace();
    }
    }
    return null;


    IP属地:山东2楼2015-04-21 14:17
    收起回复
      2026-02-07 11:57:42
      广告
      不感兴趣
      开通SVIP免广告
      求教


      IP属地:山东3楼2015-04-21 14:18
      回复
        顶!d=====( ̄▽ ̄*)b


        IP属地:山东4楼2015-04-21 14:21
        回复
          怎么让IE直接显示啊


          IP属地:山东5楼2015-04-21 14:23
          回复
            火狐不知道,不太喜欢那浏览器。谷歌也能显示,但需要开启显示PDF的插件,也就是说,其实显示PDF不是浏览器的本职工作


            IP属地:辽宁6楼2015-04-21 14:27
            收起回复


              IP属地:山东7楼2015-04-21 14:37
              回复