@POST
@Path("/")
@PreAuthorize("isFullyAuthenticated()")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public Response addAsset(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail,
@FormDataParam("draftId") Long draftId) throws IOException
{
}
when used the chrome browser to look at the file upload request, it has the below in the "Form Data" section
------WebKitFormBoundaryaQvAp0c0jbhLq4af Content-Disposition: form-data; name="draftId"
------WebKitFormBoundaryaQvAp0c0jbhLq4af Content-Disposition: form-data; name="file"; filename="image1.png" Content-Type: image/png
------WebKitFormBoundaryaQvAp0c0jbhLq4af--
does this mean the value of "draftId" is null ?
should i send "draftId" and "file" as multipart ?
the retrofit code i have tried in Android Studio is provided below
retrofit API for upload document request:
@POST("resource/asset")
@Multipart
@Headers("Content-Type: multipart/form-data")
fun uploadFile(
@Part draftId: MultipartBody.Part,
@Part file: MultipartBody.Part
): Call<ResponseBody>
the code that sends the upload request is below:
private fun uploadFile1(fileUri: Uri?) {
file = com.example.myapplication.utils.FileUtils.getFile(mContext, fileUri)
// create RequestBody instance from file
val filePart: RequestBody = RequestBody.create(
MediaType.parse(activity!!.contentResolver.getType(fileUri!!)!!),
file
)
// MultipartBody.Part is used to send also the actual file name
val body = MultipartBody.Part.createFormData("file", file!!.name, filePart)
val draft = MultipartBody.Part.createFormData("draftId",null, draftpp)
val call = apiInterface.uploadFile(draft, body)
call.enqueue(object : Callback<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
Log.i("Upload", "success")
}
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
Log.i("Upload error:", t.message)
}
})
}
i am getting response code 422, can someone please help me fix the issue ?
com.example.myapplication.utils.FileUtils.getFile()
probably is incorrect. AUri
is not a file. See this blog post and this blog post for instructions for how to post a multipart form body using aUri
. – CommonsWare