什么';我的代码怎么了?为什么可以';我不能在Android上用Chrome下载pdf文件吗?

什么';我的代码怎么了?为什么可以';我不能在Android上用Chrome下载pdf文件吗?,android,jsp,servlets,pdf,content-disposition,Android,Jsp,Servlets,Pdf,Content Disposition,晚上好 我在一家公司工作。我接到一个任务,想弄明白为什么不能用安卓手机下载PDF文件。我得到一个可以访问pdf文件的URL。一旦它使用了HTTP协议,就没有问题了。当他们改为HTTPS协议时,问题就出现了 我可以使用Firefox、Chrome和IE在我的电脑上下载。当我尝试使用我的Android手机(三星Galaxy Note 2)、Android 4.4.2、浏览器:Chrome和一个图标为蓝色地球仪的手机访问该文件时,蓝色下载栏上会显示文件名“”,然后下载失败。我尝试从其他网站下载其他pd

晚上好

我在一家公司工作。我接到一个任务,想弄明白为什么不能用安卓手机下载PDF文件。我得到一个可以访问pdf文件的URL。一旦它使用了HTTP协议,就没有问题了。当他们改为HTTPS协议时,问题就出现了

我可以使用Firefox、Chrome和IE在我的电脑上下载。当我尝试使用我的Android手机(三星Galaxy Note 2)、Android 4.4.2、浏览器:Chrome和一个图标为蓝色地球仪的手机访问该文件时,蓝色下载栏上会显示文件名“”,然后下载失败。我尝试从其他网站下载其他pdf文件,效果很好。当我使用Android版Firefox时,我可以成功且正确地下载它。iOS和WindowsPhone没有我上面提到的问题

这是代码。它与JSP一起用Javaservlet编写。(我这里只带了Java代码。)我不希望得到清晰的解决方案,但至少我在寻找一些线索。我是一个新的程序员。如果这个问题听起来很傻,请原谅


公共ActionForward下载(ActionMapping映射、ActionForm表单、, HttpServletRequest请求,HttpServletResponse响应){ HttpSession session=request.getSession(); BusinessUser bu=(BusinessUser)session.getAttribute(ConfigConstant.session\u CURRENT\u USER); String id=request.getParameter(ConfigConstant.PARAMETER\u id); 字符串reportId=id; reportId=新字符串(Base64.decodeBase64(reportId)); 字符串imgId=reportId.split(ConfigConstant.SPLITTER_破折号)[0]; String reportType=request.getParameter(“reportType”);//硬代码๏ฟฝ๏ฟฝ๏ฟฝวค๏ฟฝ๏ฟฝ๏ฟฝ 字符串fileType=request.getParameter(“fileType”); if(StringUtils.isNotEmpty(imgId)){ File File=fileService.getFileByfileID(imgId); 日志=新日志(); log.setLogSessionID(session.getId()); log.setLogType(ConfigConstant.log\u TYPE\u语句); log.setLogAction(ConfigConstant.log\u ACTION\u 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解析器; InputStream InputStream=null; BufferedReader BufferedReader=null; 试一试{ outputStream=response.getOutputStream(); if(fileType.equalsIgnoreCase(“csv”)){ bufferedReader=新的bufferedReader(新的InputStreamReader(新的ByteArrayInputStream(file.getContent()),“TIS-620”); //微软Excel风格 解析器=新的ExcelCSVParser(bufferedReader); 字符串内容=”; 字符串[][]allValues=parser.getAllValues(); 布尔值优先=真; 布尔headTr=true; 布尔头td=真; int checkRowColor=0; 如果(所有值!=null){ 内容+=“”+ “#详细信息{字体系列:”+ “Arial,Helvetica,无衬线;边框折叠:折叠;}#细节td,”+ “#详细信息{字体大小:0.7em;边框:1px纯黑色;填充:3px 7px 2px 7px;}”+ “#详细信息{字体大小:0.9em;文本对齐:居中;顶部填充:5px;底部填充:4px;”+ “背景色:#C2CFDF;颜色:黑色;}#细节tr.alt td”+ “{color:#000000;背景色:#EEF2F7;}”+ “函数提交(url){window.location=url;}”; 内容+=“导出到CSV”;
对于(int i=0;i您可以用另一种方式下载PDF。(下载PDF的工作原理与下载任何其他二进制文件相同。)

  • 使用连接的方法读取文件

  • 创建并写入inputstream


  • 查看示例源代码。

    你能发布pdf的URL吗?以及你用来下载pdf的代码吗?恐怕URL仅限于员工,你可能无法访问。但是链接是这样的:…xxx.do?mode=download&id=MS0wNjExMjAxNCAxNzU5&reportType=RLEE860&fileType=pdf我知道一些HTML代码。我认为这是应该是:class=“odd”onclick=“popupDownload('MS0wNjExMjAxNCAxODI5','RLEE860','pdf')”抱歉,我不知道任何进一步的代码。很可能,该Web应用正在做一些Android浏览器不一定支持的事情,例如
    内容处理
    。好的,至少发布您用于下载的代码。我已经开发了这样的应用程序。此pdf作为您要下载的url的附件下载n正在加载。你只需要更改请求的标题。发布代码,我可能会帮助你。我已经发布了代码。我仍然无法使用Android上的Chrome下载pdf文件。但我已经使用Firefox下载了这些文件。请说点什么。我现在很绝望。如果代码不清楚,请告诉我。谢谢。同时检查此链接
     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;
    }