2014-11-06 53 views
1

晚上好。我的代码有什么问题?为什么我无法使用Android上的Chrome下载PDF文件?

我为一家公司工作。我得到一个任务,找出为什么不能使用Android手机下载PDF文件。我得到一个可以访问pdf文件的URL。一旦它使用HTTP协议,并没有问题。当他们更改为HTTPS协议时,问题就出来了。

我可以用我的电脑使用Firefox,Chrome和IE下载。当我尝试使用我的Android手机(Samsung Galaxy Note 2),Android 4.4.2,浏览器:Chrome以及其图标是蓝色地球的图标访问文件时,出现蓝色下载栏,文件名称为“< Untitled>”然后下载失败。我尝试从其他网站下载其他PDF文件,它工作正常。当我使用Firefox for Android时,我可以成功并正确地下载它。 iOS和WindowsPhone没有我上面提到的问题。

这是代码。它与JSP一起用Java Servlet编写。 (我只在这里提供Java代码。)我不希望我会得到明确的解决方案,但至少我正在寻找一些线索。我是一名新程序员。请原谅,如果这个问题听起来很愚蠢。


public ActionForward download(ActionMapping mapping, ActionForm form, 
      HttpServletRequest request, HttpServletResponse response) { 

    HttpSession session = request.getSession(); 
    BusinessUser bu = (BusinessUser) session.getAttribute(ConfigConstant.SESSION_CURRENT_USER); 
    String id = request.getParameter(ConfigConstant.PARAMETER_ID); 
    String reportId = id; 
    reportId = new String(Base64.decodeBase64(reportId)); 
    String imgId = reportId.split(ConfigConstant.SPLITTER_DASH)[0]; 
    String reportType = request.getParameter("reportType"); // HardCode ๏ฟฝ๏ฟฝ๏ฟฝวค๏ฟฝ๏ฟฝ๏ฟฝ 
    String fileType = request.getParameter("fileType"); 


    if(StringUtils.isNotEmpty(imgId)) { 

     File file = fileService.getFileByfileID(imgId); 

     Log log = new Log(); 
     log.setLogSessionID(session.getId()); 
     log.setLogType(ConfigConstant.LOG_TYPE_FRANCHISE_STATEMENT); 
     log.setLogAction(ConfigConstant.LOG_ACTION_OPEN); 
     log.setLogClass(this.getClass().getName()); 
     log.setLogItemId(imgId); 
     log.setLogUserID(bu.getUserID()); 
     log.setCreateUser(bu.getFirstName() + " " + bu.getLastName()); 
     log.setLogIpAddress(request.getRemoteAddr()); 
     logService.insertLog(log); 
     log = null; 

     OutputStream outputStream = null; 
     CSVParse parser; 
     InputStream inputStream = null; 
     BufferedReader bufferedReader = null; 
     try{ 

      outputStream = response.getOutputStream(); 


      if(fileType.equalsIgnoreCase("csv")){ 
       bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(file.getContent()), "TIS-620")); 

       // Microsoft Excel style 
       parser = new ExcelCSVParser(bufferedReader); 
       String content = ""; 
       String[][] allValues = parser.getAllValues(); 
       boolean first = true; 
       boolean headTr = true; 
       boolean headTd = true; 
       int checkRowColor = 0; 
       if(allValues != null){ 
        content += "<!DOCTYPE html><html><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' pageEncoding='UTF-8' />" + 
          "<head><style type=\"text/css\">#detail{font-family: " + 
          "Arial, Helvetica, sans-serif;border-collapse:collapse;}#detail td, " + 
          "#detail th{font-size:0.7em;border:1px solid black;padding:3px 7px 2px 7px;}" + 
          "#detail th {font-size:0.9em;text-align:center;padding-top:5px;padding-bottom:4px;" + 
          "background-color:#C2CFDF;color:black;}#detail tr.alt td " + 
          "{color:#000000;background-color:#EEF2F7;}</style></head>" + 
          "<script type='text/javascript'>function submit(url){window.location = url; }</script><body>"; 

        content += "<table align='center'><tr><td><button onclick='submit(\"franchiseStatement.do?mode=exportFileToCsv&id="+id+"\");'>Export to CSV</button></td></tr></table>"; 
        for(int i=0;i<allValues.length;i++){ 
         if(allValues[i].length ==1 && first){ 
          content += "<div>"+allValues[i][0]+"</div>"; 
         }else{ 
          if(first){         
           first = false;         
           content += "<table border='1' bordercolor='black' cellpadding='0' cellspacing='0' id='detail'>"; 
          } 
          if(headTr){ 
           content += "<tr bgcolor='#E2E0FF'>"; 
           headTr = false; 
          }else{ 
           if(checkRowColor%2 != 0){ 
            content += "<tr class='alt'>"; 
           }else{ 
            content += "<tr>"; 
           }         
           checkRowColor++; 
           headTd = false; 
          } 

          for(int j=0;j<allValues[i].length;j++){ 
           if(headTd){ 
            content += "<td align='center'><b>"+allValues[i][j]+"<b></td>"; 
           }else if(org.apache.commons.lang.StringUtils.isEmpty(allValues[i][j])){ 
            content += "<td>&nbsp;</td>"; 
           }else{  
            if(TextUtils.checkNumeric(allValues[i][j])){ 
             content += "<td align='right'>"+allValues[i][j]+"</td>"; 
            }else{ 
             content += "<td>"+allValues[i][j]+"</td>"; 
            }          
           } 
          } 
          content += "</tr>"; 
         } 
        } 
        content += "</table>"; 
        content += "</body></html> "; 
       } 

       outputStream.write(content.getBytes("UTF-8")); 
      }else{ 

       if(file != null){ 

        response.setContentType(file.getFileContentType()); 

     // if not PDF, choose open, save or cancel 
        if(! StringUtils.equalsIgnoreCase(file.getFileContentType() , "application/pdf")){ 

    //       response.setHeader("Content-Disposition", "attachment; filename=\""+file.getFileName()+"\""); 

         String fileName = StringUtils.isNotEmpty(file.getFileName()) ? file.getFileName() : "output" ; 
         try { 
          String header = "attachment; filename=\"" + fileName + "\""; 
          response.setHeader("Content-Disposition", new String(header.getBytes("TIS620") , "ISO8859-1")); 
         } catch (UnsupportedEncodingException e) { 
          LogUtils.fatal(this.getClass(),e); 
          response.setHeader("Content-disposition", "attachment; filename="+ fileName); 
         } 
        }else{ 
         response.setHeader("Content-disposition", "inline;"); 
        } 

        outputStream.write(file.getContent()); 

       }else{ 
        // File is null. 

        response.setContentType("application/pdf"); 
        response.setHeader("Content-disposition", "inline;"); 

        //String url = "https://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/font/" + "UPCDL.TTF" ; 
        String url = "."+ "/font/" + "UPCDL.TTF" ; 

        BaseFont baseFont = BaseFont.createFont(url, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 
        Font font = new Font(baseFont, 20); 

        Document document = new Document(PageSize.A4); 
        PdfWriter.getInstance(document , outputStream); 
        document.open(); 

        document.add(new Paragraph(new ThaiChunk("๏ฟฝ๏ฟฝ่พบ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝะบ๏ฟฝ ๏ฟฝ๏ฟฝุณาติด๏ฟฝ๏ฟฝอผ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝ๏ฟฝะบ๏ฟฝ" , font))); 
        document.add(Chunk.NEWLINE); 
        document.add(new Paragraph(new ThaiChunk("Report Id : " + reportId , font)));      
        document.close();      
       } 
      } 
     }catch(Exception ex){ 
      LogUtils.fatal(this.getClass(),ex); 
      LogUtils.error(this.getClass(), ex); 
     }finally{    
      try {     
       if(file != null){ 
        file.setContent(null); 
       } 

       if(outputStream != null){ 
        outputStream.flush(); 
        outputStream.close(); 
       } 

       if(inputStream != null){ 
        inputStream.close(); 
       } 

       if(bufferedReader != null){ 
        bufferedReader.close();      
       } 

      } catch (IOException e) { 
       LogUtils.error(this.getClass(), e); 
      }finally{ 
       file = null; 
       outputStream = null; 
       inputStream = null; 
       bufferedReader = null; 
      } 
     }   
    } 
    return null; 
} 

这就是我想这是怎么一回事。如果还不够,请告诉我。先谢谢你。

+1

你可以发布pdf的网址吗?和你用来下载PDF的代码? – Darpan 2014-11-06 11:15:26

+0

恐怕URL仅限于员工,您可能无法访问它。但链接如下: ... xxx.do?mode=download&id=MS0wNjExMjAxNCAxNzU5&reportType=RLEE860&fileType=pdf 我知道一些HTML代码。我认为这将是: class =“odd”onclick =“popupDownload('MS0wNjExMjAxNCAxODI5','RLEE860','pdf')” 对不起,我不知道任何进一步的代码。 – dolkung 2014-11-06 11:33:14

+0

最有可能的是,Web应用程序正在做一些Android浏览器不一定支持的内容,比如'Content-disposition'。 – CommonsWare 2014-11-06 12:00:32

回答

0

您可以下载PDF用另一种方式。(下载PDF的工作方式相同下载任何其他二进制文件。)

  1. 打开HttpUrlConnection

  2. 使用该连接的getInputStream()方法来读取文件

  3. 创建一个FileOutputStream并写入输入流。

检查this post例如源代码。

+0

检查此链接也http://zacktutorials.blogspot.in/2014/07/android-downloading-and-viewing-pdf.html – samsad 2014-11-06 12:25:37