Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected char 0x662 at 42 in Content-Disposition value: form-data; name="userfile"; filename="IMG_٢٠١٩١١١٥_٠٩٠٢٠٤١١٤.jpg"

I am using localization in my android app

while language is set to Arabic, in the file uploading process of okhttp (okhttp:3.12.0) I am getting this error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.prominere.health1, PID: 7502
    java.lang.IllegalArgumentException: Unexpected char 0x662 at 42 in Content-Disposition value: form-data; name="userfile"; filename="IMG_٢٠١٩١١١٥_٠٩٠٢٠٤١١٤.jpg"
        at okhttp3.Headers.checkValue(Headers.java:272)
        at okhttp3.Headers.of(Headers.java:224)
        at okhttp3.MultipartBody$Part.createFormData(MultipartBody.java:259)
        at okhttp3.MultipartBody$Builder.addFormDataPart(MultipartBody.java:324)
        at com.prominere.health1.ProfileActivity.onValidationSucceeded(ProfileActivity.java:222)
        at com.mobsandgeeks.saripaar.Validator.triggerValidationListenerCallback(Validator.java:719)
        at com.mobsandgeeks.saripaar.Validator.validateFieldsWithCallbackTill(Validator.java:697)
        at com.mobsandgeeks.saripaar.Validator.validateUnorderedFieldsWithCallbackTill(Validator.java:679)
        at com.mobsandgeeks.saripaar.Validator.validate(Validator.java:334)
        at com.mobsandgeeks.saripaar.Validator.validate(Validator.java:295)
        at com.prominere.health1.ProfileActivity.onClick(ProfileActivity.java:139)
        at com.prominere.health1.ProfileActivity_ViewBinding$4.doClick(ProfileActivity_ViewBinding.java:81)
        at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:18)
        at android.view.View.performClick(View.java:5675)
        at android.view.View$PerformClick.run(View.java:22641)
        at android.os.Handler.handleCallback(Handler.java:836)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6251)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

and my block of code is

final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/*");
            formBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("action", "updateprofile")
                    .addFormDataPart("userfile", file.getName(), RequestBody.create(MEDIA_TYPE_PNG, file))
                    .addFormDataPart("userid", user.getId())
                    .addFormDataPart("name", name.getText().toString())
                    .build();

how can I resolve this issue

like image 484
Dhanu K Avatar asked Nov 15 '19 03:11

Dhanu K


1 Answers

try encoding the file name like this :

URLEncoder.encode(file.getName(), "utf-8")

So that your code becomes

final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/*");
        formBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("action", "updateprofile")
                .addFormDataPart("userfile", URLEncoder.encode(file.getName(), "utf-8"), RequestBody.create(MEDIA_TYPE_PNG, file))
                .addFormDataPart("userid", user.getId())
                .addFormDataPart("name", name.getText().toString())
                .build();
like image 74
Navneet Krishna Avatar answered Oct 17 '22 10:10

Navneet Krishna