Maybe you could try using httpclient ?
haven't tried myself but using "chunked" request, the output will not be 
buffered.
for exemple, cf
http://www.java-tips.org/other-api-tips/httpclient/how-to-use-unbuffered-chunk-encoded-post-re.html
 


Mike


zeeshan a écrit :
> Hi Experts,
>
> I need to post some files on my server, it works fine for small files
> like 1mb but when i try for more than 4 mb ; it force close because of
> memory overload.
>
> please have a look on my code below:
>
>       URL url = new URL(uri);
>       URLConnection conn = url.openConnection();
>
>
>                       String boundary = "myboundary";
>                       conn.setDoOutput(true);
>
>                       conn.setDoInput(true);
>                       conn.setUseCaches(false);
>                       conn.setRequestProperty("Connection","Keep-Alive");
>                       conn.setRequestProperty("user-
> agent",this.user_agent);
>                       conn.setRequestProperty("accept-
> language",this.accept_language);
>                       conn.setRequestProperty("user-agent-
> id",this.user_agent_id);
>                       conn.setRequestProperty("Content-Type","multipart/
> form-data; boundary=" + boundary);
>                       DataOutputStream wr = new DataOutputStream
> (conn.getOutputStream());
>                       // Send the normal form data
>
>                       Iterator i = parameters.entrySet().iterator();
>                       while (i.hasNext()) {
>                           Map.Entry param = (Map.Entry)i.next();
>                           wr.write( ("--" + boundary + "\r\n").getBytes
> () );
>                           wr.write( ("Content-Disposition: form-data; name=
> \""+(String)param.getKey()+"\"\r\n\r\n").getBytes());
>
>                           wr.write( ((String)param.getValue() +
> "\r").getBytes());
>
>                           wr.flush();
>                       }
>                      /* wr.write( ("\r--" + boundary + "--\r\n").getBytes
> ());
>                       wr.flush();*/
>                     if(includeFiles){
>                       int bytesRead = 0;
>
>                       byte [] buffer = new byte[1024*4];
>                       InputStream is;
>                       Iterator i2 = fileParameters.entrySet().iterator
> ();
>                       while (i2.hasNext()) {
>
>                           Map.Entry param = (Map.Entry)i2.next();
>                           String filekey = (String)param.getKey();
>                           String fileName = (String)param.getValue();
>
>                           wr.write( ("--" + boundary + "\r\n").getBytes
> () );
>                           wr.write( ("Content-Disposition: form-data; name=
> \""+(String)param.getKey()+"\"; ").getBytes());
>
>                           if(filekey.endsWith("message[audio]")){
>                                if(fileName.contains("/sdcard")){
>                                        wr.write( ("filename=\"" + fileName +
> "\"\r").getBytes());
>                                               File file = new File(fileName);
>                                                   Uri ab= Uri.parse(fileName);
>                                                   is = new 
> FileInputStream(fileName);}
>                                else {
>                                        wr.write( ("filename=\"" + 
> fileName.concat
> (".amr") + "\"\r").getBytes());
>                                        is = this.cr.openInputStream( Uri.parse
> (fileName));}
>                           wr.write( ("Content-Type: audio \r\n\r
> \n").getBytes());
>                           wr.flush();
>
>                           /*File file = new File(fileName);
>                           Uri ab= Uri.parse(fileName);
>                           is = new FileInputStream(fileName);*/
>                           }
>                           else{
>                                if(fileName.contains("/sdcard")){
>
>                                               File file = new File(fileName);
>                                               //long temp=file.length();
>                                                   Uri ab= Uri.parse(fileName);
>                                                   is = new 
> FileInputStream(fileName);}
>                                else {
>
>                                        is = this.cr.openInputStream( Uri.parse
> (fileName));}
>
>                           wr.write( ("filename=\"" + fileName +
> "\"\r").getBytes());
>                           wr.write( ("Content-Type: image \r\n\r
> \n").getBytes());
>                           wr.flush();
>
>
>                           }
>
>
>
>                           //FileInputStream is = new FileInputStream(new
> File(fileName));
>
>                           //bytesAvailable = is.available();
>
>
>
>                           while( (bytesRead = is.read(buffer)) > 0) {
>                               wr.write(buffer,0,bytesRead);
>                               wr.flush();
>                           }
>                           if(i2.hasNext()){
>                           wr.write( ("\r\n").getBytes());
>                           }
>                       }
>                      }
>                       wr.write( ("\r--" + boundary + "--\r\n").getBytes());
>                       wr.flush();
>
>                   wr.close();
>                 }
>                       ///////////////////////////////
>
>
>
> i believe there is somewhere memory leakage but dont know where, i
> have done a lot research but not successfull.
>
> please let me know if u figure out any problem in my code
> >
>   

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to