Android 正在发送包含波斯字符的文件名为的改装2

Android 正在发送包含波斯字符的文件名为的改装2,android,retrofit,retrofit2,multipart,Android,Retrofit,Retrofit2,Multipart,我正在发送带有改装的多部分请求,效果很好。但当我的文件名为contain posian character时,我的应用程序崩溃,我出现以下错误: java.lang.IllegalArgumentException:35英寸处的意外字符0x62f 内容配置值:表单数据;name=“photo”; filename=“دوچخه.jpg” 这是我发送多部分请求的方式: File imageFile = new File(imagePath); ProgressRequestBod

我正在发送带有改装的
多部分
请求,效果很好。但当我的文件名为contain posian character时,我的应用程序崩溃,我出现以下错误:

java.lang.IllegalArgumentException:35英寸处的意外字符0x62f 内容配置值:表单数据;name=“photo”; filename=“دوچخه.jpg”

这是我发送多部分请求的方式:

    File imageFile = new File(imagePath);

    ProgressRequestBody fileBody = new ProgressRequestBody(imageFile, this);
    MultipartBody.Part filePart = MultipartBody.Part.createFormData("photo", imageFile.getName(), fileBody);

    RetroInterface retroInterface = RetrofitClientInstance.getRetrofitInstance().create(RetroInterface.class);
    Call<SendFileResponse> call = retroInterface.sendPhoto(token, myHashmap, filePart);
File imageFile=新文件(imagePath);
ProgressRequestBody fileBody=新的ProgressRequestBody(imageFile,this);
MultipartBody.Part filePart=MultipartBody.Part.createFormData(“photo”,imageFile.getName(),fileBody);
RetroInterface RetroInterface=RefughtClientInstance.GetRefughtInstance().create(RetroInterface.class);
Call Call=retroInterface.sendPhoto(令牌、myHashmap、filePart);

我如何解决这个问题

这只是一个建议,但作为一种解决方法,您可以尝试将文件重命名为文件名的id(时间戳/任何其他内容),并添加一个名称字段,如果文件

public class Object {
  private String filename;
  private File actualFile;
}

这样,当以后检索时,您仍然可以始终引用所需的文件。

我的解决方案并不完美,因为它可以更改一些字符,就是将
URLEncoder.encode(file.name,“utf-8”)
替换为普通名称。

我们可以使用
addUnsafeNonAscii()
来自
Header.Builder
类的方法,通过该方法,我们可以添加具有指定名称和值的头。对标头名称进行验证,允许使用非ASCII值。因此,我们可以将任何语言字符作为值传递,包括波斯语字符

val fileName = "your file name with extention"
val reqFile = RequestBody.create(MediaType.parse("image/*"), File(imageDir))

val header = Headers.Builder()
header.addUnsafeNonAscii("Content-Disposition", "form-data; name=\"image\"; filename=\"$fileName")

val body = MultipartBody.Part.create(header.build(), reqFile)
myApi.uploadItem(body)

我用过你的方法,但我想一定有一个合适的方法来处理它!此解决方案适用于任何包含通配符的文件名吗?此解决方案适用于发送请求的改装,但服务器可能有编码问题,我也有同样的问题。