java - Connection gets closed while uploading file(more than 2 MB) in blacberry rim app -


im trying upload file blackberry app amazon s3.

here code

  private synchronized void uploadfiletoamazon(createfileidbean createfileidbean)       throws unsupportedencodingexception, ioexception,connectionnotfoundexception, connectionclosedexception, backupcancelledexception, interruptedexception, backupinterruptedexception {          string boundary = "----------v2ymhfg03ehbqgzcako6jy";          string policy = "{\"expiration\": \"2020-12-01t12:00:00.000z\","                 + "\"conditions\": ["                 + "{\"bucket\": \"" + beanfactory.getusercreatebackupidbean().getbucketname() + "\"},"                 + "{\"x-amz-security-token\": \"" + beanfactory.getusercreatebackupidbean().getamazontoken() + "\"},"                 + "{\"success_action_status\": \"201\"},"                 + "[\"starts-with\", \"$content-type\", \"\"],"                 + "[\"starts-with\", \"$key\", \"" + beanfactory.getusercreatebackupidbean().getbackuppath() + "\"]"                 + "]"                 + "}";         string encodepolicy = base64.encode(policy.getbytes());         string signature = uploadsignature(policy, beanfactory.getusercreatebackupidbean().getamazonsecret());         hashtable params = new hashtable();         params.put("key", beanfactory.getusercreatebackupidbean().getbackuppath() + "/" + beanfactory.getusercreatefileidbean().getfileid());         params.put("awsaccesskeyid", beanfactory.getusercreatebackupidbean().getamazonkey());         params.put("content-type", createfileidbean.getfiletypecontent());         params.put("x-amz-security-token", beanfactory.getusercreatebackupidbean().getamazontoken());         params.put("policy", encodepolicy);         params.put("success_action_status", "201");         params.put("signature", signature);          send(boundary, "http://" + beanfactory.getusercreatebackupidbean().getbucketname() + ".s3.amazonaws.com/", params, "file", beanfactory.getusercreatefileidbean().getfileid(), createfileidbean.getfiletypecontent(), createfileidbean.getfilebyte(), createfileidbean);      }      private synchronized string getboundarymessage(string boundary, hashtable params, string filefield, string filename, string filetype, byte[] filebytes, createfileidbean createfileidbean) {         stringbuffer res = new stringbuffer("--").append(boundary).append("\r\n");          enumeration keys = params.keys();          while (keys.hasmoreelements()) {             string key = (string) keys.nextelement();             string value = (string) params.get(key);              res.append("content-disposition: form-data; name=\"").append(key).append("\"\r\n")                     .append("\r\n").append(value).append("\r\n")                     .append("--").append(boundary).append("\r\n");         }          return res.tostring();     }      private synchronized void send(string boundarry, string url, hashtable params, string filefield, string filename, string filetype, byte[] filebytes, createfileidbean createfileidbean) throws ioexception,connectionclosedexception,connectionnotfoundexception, backupcancelledexception, interruptedexception, backupinterruptedexception {               stringbuffer buffer = new stringbuffer();              httpconnection hc = null;             inputstream = null;             inputstream inputfiledatastream = null;             dataoutputstream dout = null;              string boundary = boundarry;             stringbuffer res = new stringbuffer();              int ch;              string boundarymessage = getboundarymessage(boundary, params, filefield, filename, filetype, filebytes, createfileidbean);              bytearrayoutputstream bos = new bytearrayoutputstream();             bos.write(boundarymessage.getbytes());              res.append("content-disposition: form-data; name=\"").append(filefield).append("\"; filename=\"").append(filename).append("\"\r\n")                     .append("content-type: ").append(filetype).append("\r\n\r\n");              bos.write(res.tostring().getbytes());              string end = "\r\n"+"--"+boundary+"\r\n"+"content-disposition: form-data; name=\""+"submit"+"\"\r\n"+"\r\n"+"upload amazon s3"+"\r\n"+"--"+boundary+"--\r\n";              try {                      hc = (httpconnection) connector.open(url+resources.getconnectionstring(), connector.read_write,true);                     hc.setrequestmethod(httpconnection.post);                     hc.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundarry);                     hc.setrequestproperty("user-agent", resources.getuseragentstring());                     //hc.setrequestproperty("user-agent", "profile/midp-1.0 confirguration/cldc-1.0");                     hc.setrequestproperty("content-language", "en-us");                     hc.setrequestproperty("connection", "keep-alive");                     hc.setrequestproperty("keep-alive", "300");                     hc.setrequestproperty("expect", "100-continue");                      hc.setrequestproperty("content-length", (bos.tobytearray().length+createfileidbean.getfilesize()+end.getbytes().length)+"");                     hc.setrequestproperty("content-length", (bos.tobytearray().length+createfileidbean.getfilesize()+end.getbytes().length)+"");                      dout = new dataoutputstream(hc.opendataoutputstream());                      dout.write(bos.tobytearray());               inputfiledatastream = readinputstream(createfileidbean.getfilepath());                          while ((ch = inputfiledatastream.read()) != -1) {              dout.write(ch);              }                         dout.write(end.getbytes());                      dout.flush();                     //dout.close();                       = hc.opendatainputstream();                      beanfactory.getuseruploadfilebean().setresponsecode(hc.getresponsecode() + "");                     beanfactory.getuseruploadfilebean().setresponsemessage(hc.getresponsemessage());                      while ((ch = is.read()) != -1) {                          buffer.append((char) ch);                     }                      system.out.println("buffer"+buffer);              }              catch (ioexception e) {                 throw new backupinterruptedexception(constants.error_in_upload);              } {                 try {                     if (is != null) {                         is.close();                     }                     if (hc != null) {                         hc.close();                     }                     if(inputfiledatastream !=null)                     {                         inputfiledatastream.close();                     }                     if(dout !=null)                     {                         dout.close();                     }                   } catch (ioexception e2) {                     system.out.println("aa"+e2.getmessage());                     throw e2;                 }             }     }      private synchronized string uploadsignature(string policy, string secretkey) throws unsupportedencodingexception {          string encodepolciy = base64.encode(policy.getbytes());         hmac m = new hmac(new sha1digest());         m.init(new keyparameter(secretkey.getbytes("utf-8")));         byte[] bytes = encodepolciy.getbytes("utf-8");         m.update(bytes, 0, bytes.length);         byte[] mac = new byte[m.getmacsize()];         m.dofinal(mac, 0);         string signature = base64.encode(mac);         return signature;     }     private synchronized inputstream readinputstream(string path) throws ioexception {          fileconnection fc = null;         inputstream = null;          fc = (fileconnection) connector.open(path.tostring(), connector.read);         if (!fc.exists()) {             settings.error_message = "file doesn't exist!";             //throw new backupinterruptedexception(settings.error_message);         } else {              = fc.openinputstream();         }         if(fc !=null)         {             fc.close();         }         return is;     } 

its getting connection closed exception when trying response code(in line beanfactory.getuseruploadfilebean().setresponsecode(hc.getresponsecode() + "");) after uploading data. files less 2mb working fine. please me...

my experience httpconnection buffers full payload before sending anything. blackberry curve 8520, 16mb of data ends making device unusable. worked around using raw socketconnection, , writing http parts directly in app, make sure there wasn't excessive buffering before bytes hit socket.

one area later realized might work, using http-chunked mode built-in httpconnection object. since http-chunked fundamentally streaming mechanism, may allow out of 'buffer all' logic default.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -