java - Download File with URL Connection -


i have following code, downloads file, adobe says file damage. file 74kb big, can download 27kb. can tell me what's wrong. thanks

@webservlet("/getdata") public class getdata extends httpservlet { private static final long serialversionuid = 1l;  private static final int buffer_size = 4096;  protected void dopost(httpservletrequest request,         httpservletresponse response) throws servletexception, ioexception {      printwriter out = response.getwriter();     string typ = ".pdf";     int totalbytesread = 0;       string sep = file.separator;     string savedir = "c:" + sep + "downloads";     string link ="/g02pepe/portal?token=9059829732446568452&action_16502593.maincontent_root_cntxyz_cntshow_actshowfirstrow::km_xxxxxxx-xxxx::=&frontletid=xxxxx";      string adress = "https://xxxxxxx.xxxx.de/g02pepe/entry?rzid=xc&rzbk=0032&trackid=piwikad277d23c35b4990";     string = adress + link;     string username = "xxxxxxxxx";     string password = "xxxxxxxxx";     string authstring = username + ":" + password;     system.out.println("auth string: " + authstring);     byte[] authencbytes = base64.encodebase64(authstring.getbytes());     string authstringenc = new string(authencbytes);      try {         url url = new url(adress);         httpurlconnection connection = (httpurlconnection) url.openconnection();         connection.addrequestproperty("user-agent", "mozilla/5.0 (windows nt 6.1; wow64; rv:37.0) gecko/20100101 firefox/37.0");         connection.setrequestproperty("authorization", "basic " + authstringenc);         connection.addrequestproperty("referer", together);         system.out.println("connected " + together);         connection.setdoinput(true);         connection.setdooutput(true);         connection.setrequestmethod("post");         connection.connect();          int responsecode = connection.getresponsecode();         // check http response code first         if (responsecode == httpurlconnection.http_ok) {             string filename = "";             string disposition = connection.getheaderfield("content-disposition");             string contenttype = connection.getcontenttype();             int contentlength = connection.getcontentlength();              if (disposition != null) {                 // extracts file name header field                 int index = disposition.indexof("filename=");                 if (index > 0) {                     filename = disposition.substring(index + 10,                             disposition.length() - 1);                 }             } else {                 // extracts file name url                 filename = adress.substring(adress.lastindexof("/") + 1,                         adress.length());             }             system.out.println("content-type = " + contenttype);             system.out.println("content-disposition = " + disposition);             system.out.println("content-length = " + contentlength);             system.out.println("filename = " + filename);               inputstream in = connection.getinputstream();              string savefilepath = savedir + file.separator + "entgeltinformationen"  + typ;                           // opens output stream save file             fileoutputstream outputstream = new fileoutputstream(savefilepath);              int bytesread = -1;             byte[] buffer = new byte[buffer_size];             while ((bytesread = in.read(buffer)) != -1) {                 outputstream.write(buffer, 0, bytesread);                  totalbytesread += bytesread;             }             system.out.println("total bytes read "  + totalbytesread);              outputstream.close();             in.close();              system.out.println("file downloaded");         } else {             system.out                     .println("no file download. server replied http code: "                             + responsecode);         }          connection.disconnect();          out.println("download done!");      } catch (exception e) {         e.printstacktrace();     }    } } 

the file want download pdf.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -