java - Send an upload in chunks to avoid OutOfMemoryError with httpclient 3.1 and URLConnection -


I am trying to send a file using an IIS server using apache commons httpclient 3.1, but I get a memory error Coming out / P>

  InputStream = FileService.getInputStream (filename, FileService.HDD); BytereonOutOperputStream FileUpload = New Bytereanputstream (); Byterepartsource file = null; Try {byte [] buffer = new byte [1024]; For (Int Lennon; (Lane = Reid (buffer))! = -1;) fileToUpload.write (buffer, 0, lane); File = new byte array source (filename, fileToUpload.toByteArray ()); Post.setContentChunked (wrong); Part [] part = new part [] {new file page (filename, file)}; Post.setRequestEntity (New Multilateral Request (Part, Post. GetParams ()); } Hold (OutofMemory Irrore E) {} Finally {try {if (=! =) Is.close (); If (fileToUpload! = Null) fileToUpload.close (); } Hold (IOException E) {}}  

OutOfMemoryError fileToUpload.toByteArray happens on (). What is a way to send fragmentation to a file? I can not use the file object because I get permission errors.

I have also tried without using httpClient but I still get a memory error

  InputStream fileToUpload = FileService.getInputStream (filename, file service.HDD); OutputStream Output = connection.getOutputStream (); Printer author = null; Try the author (= "+"); Writer.println ("Content-Disposal: Form-Data"); Writer = New Print VRter (Try Out New OutputStream Water (try.getOutputStream ("UTF-8"); Writer.println ("Content-Type: Application / Octet-Stream"); ​​Writer.println ("Content-Transfer-Encoding : Binary "); writer.println (); writer.flush (); LogService.info (" FileSendor #setfileopopload () "before); Byte [] buffer = new byte [2048]; int bytes read; while (( BytesRead = fileToUpload.read (buffer)) = -1) {output.write (buffer, 0, bytes read);} LogService.info ("FileSender # setFileToUpload ()"); output.flush (); writer .println (); writer.flush (); Writer.println ("- + + +" - ");} Finally {if (author! = Null) the author. Close (); if (output! = Zero) output .close (); if (fileToUpload! = Null) fileToUpload.close ();}  

< P> You need to stream the data, so it is not in some memory once in a while like:

  InputStream = FileService.getInputStream (filename, FileService.HDD); {InputStreamBody input streamloads = try new InputStreamBody (is, fileName); MultipartEntity unit = new MultipartEntity (); Entity.addPart (filename, inputstreambody); Post.setEntity (unit); HttpClient.execute (post); ...} is finally {is.close (); }  

Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -