java - Read a binary file through FileReader in browser and send it properly to server -


i'm trying following:

  1. read binary file through filereader.
  2. send file server.
  3. read file in server.

the main questions @ bottom.

read binary file through filereader , send server:

i'm using angular, don't believe it's relevant unless ask me i'll remove boilerplate code, except call server. since filereader readasbinarystring deprecated, i'm using readasarraybuffer so:

$resource(myurl, {},              {                  post: {                      method: 'post',                      params: {},                      isarray: false,                      headers:{'content-type':'application/octet-stream'}}              });  var reader = new filereader(); reader.onload = function() {     resource.post({}, reader.result); }; 

when code runs, see in network tab data seems pass correctly. post data contains byte array example of length 400 bytes.

read file in server

i'm using code read in server

@post @path(mypath) @consumes({"application/zip"}) @produces({mediatype.application_json}) public response myrest(@queryparam("file-name") string filename, inputstream file) {     byte[] buffer = new byte[8192];     int bytesread;     bytearrayoutputstream output = new bytearrayoutputstream();     try {         while ((bytesread = file.read(buffer)) != -1) {             output.write(buffer, 0, bytesread);         }     } catch (ioexception e) {         e.printstacktrace();     }     byte[] data = output.tobytearray();      return response.ok().build(); } 

when debug, see different result byte array i've sent. example, while i've seen in browser network 400 byte length array, in server see 10 byte length array.

  1. am reading file correctly in client? readasarraybuffer method should using?
  2. any idea why getting different results in server? should consume different media type? or maybe should send different headers?

thanks!


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -