Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Spring MVC uploading file as byte[]
    primarykey
    data
    text
    <p>I have the following problem. </p> <p>I have a ActiveX control written in C++/CLI, which I'm using on client side. one of the control method return binary file by fixed chunk size (for example by 1024). This file is zipped.</p> <p>I need to write method using C++/CLI which will send each file chunk to the server. I have Java Spring MVC method which receives this byte[] chunk and append it to file. In other words I'm just uploading zip file by chunks. </p> <p>My problem is, that although the file size of both files (Original and its copy on server) ,the MD5 checksum of this file is not the same and I can't open this file using zip. The file is corrupted. </p> <p>I'm just taking each chunk byte[] convert it with BASE64 and send it to server using my regular POST request:</p> <p>My code is the following:</p> <pre><code>int SceneUploader::AddChunk(int id, array&lt;Byte&gt; ^buffer,int size){ WebRequest^ request = WebRequest::Create(AddChunkURI); request-&gt;Method = "POST"; request-&gt;ContentType = "application/x-www-form-urlencoded"; System::String ^base64Image = Convert::ToBase64String(buffer); String ^param = "id="+id+"&amp;chunk="+base64Image+"&amp;len="+size; //Just make another copy of the file to verify that sent byted are ok!!! String ^path = "C:\\Users\\dannyl\\AppData\\Local\\Temp\\test.zip"; FileStream ^MyFileStream = gcnew FileStream(path, FileMode::Append, FileAccess::Write); MyFileStream-&gt;Write(buffer,0,size); MyFileStream-&gt;Close(); //Adding the byteArray to the stream. System::IO::Stream ^stream = request-&gt;GetRequestStream(); System::IO::StreamWriter ^streamWriter = gcnew System::IO::StreamWriter(stream); streamWriter-&gt;Write(param); streamWriter-&gt;Close(); HttpWebResponse^ response = dynamic_cast&lt;HttpWebResponse^&gt;(request-&gt;GetResponse()); Stream^ dataStream = response-&gt;GetResponseStream(); StreamReader^ reader = gcnew StreamReader( dataStream ); String^ responseFromServer = reader-&gt;ReadToEnd(); return System::Int32::Parse(responseFromServer); } </code></pre> <p>My MVC controller looks like this:</p> <pre><code> @RequestMapping(value="/addChunk.dlp",method = RequestMethod.POST) @ResponseBody public String addChunk(@RequestParam("id") String id, @RequestParam("chunk") String chunk, @RequestParam("len") String len){ try{ BASE64Decoder decoder = new BASE64Decoder(); Integer length = Integer.decode(len); byte[] decodedBytes = new byte[length]; decodedBytes = decoder.decodeBuffer(chunk.trim()); File sceneFile = new File(VAULT_DIR+id+".zip"); if (!sceneFile.exists()){ sceneFile.createNewFile(); } long fileLength = sceneFile.length(); RandomAccessFile raf = new RandomAccessFile(sceneFile, "rw"); raf.seek(fileLength); raf.write(decodedBytes); raf.close(); } catch(FileNotFoundException ex){ ex.getStackTrace(); System.out.println(ex.getMessage()); return "-1"; } catch(IOException ex){ ex.getStackTrace(); System.out.println(ex.getMessage()); return "-1"; } return "0"; } </code></pre> <p>What am I doing wrong?</p> <p><strong>Update:</strong> the problem solved. Original Base64 String had '+' characters, after the request was submitted to server the chunk parameter was URLdecoded by Spring and all'+' characters were replaced by spaces, as a result zip file was corrupted. Please give me back my +50 of reputation :)</p> <p>Thank you, Danny.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload