简体   繁体   中英

Upload XLSX or ZIP file using Apache CXF corrupts the file

I am trying to upload an XLSX file and the file gets corrupted once it reaches the server

Good file N c A wo]6 9)} "r F1 6 a8 E p , i 9J ~ 6ʒ 2 y b ɵz` E ne

Bad file ( after upload) N c A wo]6 9)} "r F1 6 a8 E p , i 9J ~ 6ʒ 2 y b ɵz` E ne

Client side JS to upload this file

 var formData = new FormData(); formData.append('file',$('input[type=file]')[0].files[0]); $.ajax({ url: 'upload', data: formData, type: 'POST', enctype: "multipart/form-data", processData: false }); 

and at the server side using apache CXF

 @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public void upload (@Multipart("file") Attachment attachment){ InputStream inputStream = attachment.getDataHandler().getInputStream(); File targetFile = new File("D://test.xlsx"); OutputStream outStream = new FileOutputStream(targetFile); int read = 0; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outStream.write(bytes, 0, read); } outStream.close(); } 

  1. There is no issue with CXF
  2. No issue with JS library
  3. we had a converter which does that encoding so from the above comment by @Galigator it is clear its an encoding issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM