1

I developed an Android application,where the user can upload a file to the server.

A send the file with multipart data. Althought the api creats the file, but it is corrupted, I can't open it.

If I send the file via postman, the file in the server is good, and I can open it.

I checked the original, and the newly created file, and i noticed that, the new file is bigger than the original with 2 bytes.After that I compared the 2 files byte codes, and if I skip the first 2 byte in the new one, than the 2 byte array are the same.

So why get 2 extra byte the new file?

I created the api function based on this answer: Httplistener and file upload

**Api interface**

    @Multipart
    @POST("...")
    suspend fun saveOrUpdateDocument2(@Query("name") name: String, @Query("extension") extension: String, @Part file: MultipartBody.Part): ResponseBody

**Function:** 
            context.contentResolver.openInputStream(uri)?.let {
                val bytes = it.readBytes()

                val requestFile = RequestBody.create(MediaType.parse("*/*"),bytes)
                val body = MultipartBody.Part.createFormData("file",documentUploadDto.originalFileName,requestFile)
                api.saveOrUpdateDocument2(documentUploadDto.originalFileName,documentUploadDto.extension,body)
            }

user2340612
  • 10,053
  • 4
  • 41
  • 66
Andreas1234
  • 433
  • 4
  • 25
  • What is the value of bytes.length? – blackapps Jul 29 '21 at 16:10
  • In android it is 70568 Byte – Andreas1234 Jul 29 '21 at 16:36
  • So, I puted my theory into practice. I cuted the first 2 byte from the array on server, and the file is perfect.But I still dont know why, because on the linked solution only the last 2 byte was cut off. – Andreas1234 Jul 29 '21 at 16:43
  • 70568? And? Why dont you comment? Pretty uninteresting to know only one value. Why did i ask do you think? Think! – blackapps Jul 29 '21 at 16:51
  • Sorry, I dont underatand what do you mean.It is possible, that the byte array first two byte is a white space, after I cut off the boundary? I am really would like to underatand whats going on. – Andreas1234 Jul 29 '21 at 18:26
  • Incredible. The question was where you saw that 70568 before? Or do you see it now? – blackapps Jul 29 '21 at 18:38

0 Answers0