title: What's wrong with my code? Why can't I download pdf files with Chrome on Android?
body: <p>Good evening. </p>
  
  <p>I work for a company. I get an assignment to figure out why PDF files can't be downloaded using Android phones. I get an URL which has access to the pdf files. Once it used HTTP protocol and there was no problems. When they change to HTTPS protocol, then the problem comes out. </p>
  
  <p>I can download it with my PC using Firefox, Chrome and IE. When I try to access the file using my Android phone (Samsung Galaxy Note 2), Android 4.4.2, browser: Chrome and one whose icon is a blue globe, the blue download bar appears with the file name saying "&lt; Untitled >" and then the download fails. I try to download other pdf file from other website and it works fine. When I use Firefox for Android, I can download it successfully and correctly. iOS and WindowsPhone have no problems I mentioned above.</p>
  
  <p>Here is the code. It is written in Java Servlet along with JSP. (I only bring Java code here.) I don't wish I would get clear solutions but at least I am looking for some clues. I'm a new programmer. Please excuse me if this question sounds silly. </p>
  
  <hr>
  
  <pre><code> 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 += "&lt;!DOCTYPE html&gt;&lt;html&gt;&lt;meta http-equiv='Content-Type' content='text/html; charset=UTF-8' pageEncoding='UTF-8' /&gt;" +
                              "&lt;head&gt;&lt;style type=\"text/css\"&gt;#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;}&lt;/style&gt;&lt;/head&gt;" +
                              "&lt;script type='text/javascript'&gt;function submit(url){window.location = url; }&lt;/script&gt;&lt;body&gt;";
  
                      content += "&lt;table align='center'&gt;&lt;tr&gt;&lt;td&gt;&lt;button onclick='submit(\"franchiseStatement.do?mode=exportFileToCsv&amp;id="+id+"\");'&gt;Export to CSV&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;";
                      for(int i=0;i&lt;allValues.length;i++){
                          if(allValues[i].length ==1 &amp;&amp; first){
                              content += "&lt;div&gt;"+allValues[i][0]+"&lt;/div&gt;";
                          }else{
                              if(first){                                  
                                  first = false;                                  
                                  content += "&lt;table border='1' bordercolor='black'  cellpadding='0' cellspacing='0' id='detail'&gt;";
                              }
                              if(headTr){
                                  content += "&lt;tr bgcolor='#E2E0FF'&gt;";
                                  headTr = false;
                              }else{
                                  if(checkRowColor%2 != 0){
                                      content += "&lt;tr class='alt'&gt;";
                                  }else{
                                      content += "&lt;tr&gt;";
                                  }                                   
                                  checkRowColor++;
                                  headTd = false;
                              }
  
                              for(int j=0;j&lt;allValues[i].length;j++){
                                  if(headTd){
                                      content += "&lt;td align='center'&gt;&lt;b&gt;"+allValues[i][j]+"&lt;b&gt;&lt;/td&gt;";
                                  }else if(org.apache.commons.lang.StringUtils.isEmpty(allValues[i][j])){
                                      content += "&lt;td&gt;&amp;nbsp;&lt;/td&gt;";
                                  }else{      
                                      if(TextUtils.checkNumeric(allValues[i][j])){
                                          content += "&lt;td align='right'&gt;"+allValues[i][j]+"&lt;/td&gt;";
                                      }else{
                                          content += "&lt;td&gt;"+allValues[i][j]+"&lt;/td&gt;";
                                      }                                       
                                  }
                              }
                              content += "&lt;/tr&gt;";
                          }   
                      }
                      content += "&lt;/table&gt;";
                      content += "&lt;/body&gt;&lt;/html&gt; ";
                  }
  
                  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;
  }
  </code></pre>
  
  <p>That's all I guess it's all about. If it is not enough, please tell me. Thank you in advance.</p>