Tuesday, 15 January 2013

jsp - What's wrong with my code? Why can't I download pdf files with Chrome on Android? -



jsp - What's wrong with my code? Why can't I download pdf files with Chrome on Android? -

good evening.

i work company. assignment figure out why pdf files can't downloaded using android phones. url has access pdf files. 1 time used http protocol , there no problems. when alter https protocol, problem comes out.

i can download pc using firefox, chrome , ie. when seek access file using android phone (samsung galaxy note 2), android 4.4.2, browser: chrome , 1 icon bluish globe, bluish download bar appears file name saying "< untitled >" , download fails. seek download other pdf file other website , works fine. when utilize firefox android, can download , correctly. ios , windowsphone have no problems mentioned above.

here code. written in java servlet along jsp. (i bring java code here.) don't wish clear solutions @ to the lowest degree looking clues. i'm new programmer. please excuse me if question sounds silly.

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 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, take 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" ; seek { string header = "attachment; filename=\"" + filename + "\""; response.setheader( "content-disposition", new string( header.getbytes( "tis620" ) , "iso8859-1" ) ); } grab (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 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{ seek { if( file != null ){ file.setcontent(null); } if( outputstream != null ){ outputstream.flush(); outputstream.close(); } if( inputstream != null ){ inputstream.close(); } if( bufferedreader != null ){ bufferedreader.close(); } } grab (ioexception e) { logutils.error(this.getclass(), e); }finally{ file = null; outputstream = null; inputstream = null; bufferedreader = null; } } } homecoming null; }

that's guess it's about. if not enough, please tell me. give thanks in advance.

you can download pdf way.(downloading pdf works same downloading other binary file.)

open httpurlconnection

use connection's getinputstream() method read file

create fileoutputstream , write inputstream.

check this post illustration source code.

android jsp servlets pdf content-disposition

No comments:

Post a Comment