如何使用改造将pdf上传到服务器

How to upload pdf to server using retrofit

我无法正确上传 pdf 但是当我从邮递员上传 pdf 时它正在工作 fine.When 我从我的应用程序上传 pdf 它上传但文件错误我正在使用这些代码

   Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
   chooseFile.setType("application/pdf");
   chooseFile = Intent.createChooser(chooseFile, "Choose a 
   file");
   startActivityForResult(chooseFile, 1212);

            


    Uri uri1 = Uri.parse("file://" + selectedDocPath);
    File file = new File(uri1.getPath());
    RequestBody requestFile = RequestBody.create(MediaType.parse("*/*"), 
    file.getAbsolutePath());
    multipartBody[0] = MultipartBody.Part.createFormData("file", file.getName(), 
    requestFile);

我在 onActivityResult 方法中获取路径,就像这样

 Uri uri = data.getData();
 selectedDocPath = getPath(uri);

如果您的应用面向 Android N,也许您应该通过 ContentProvider 获取文件。 见 android-7.0-changes?authuser=1#sharing-files

您可以使用 Ion 库上传 PDF、图片和 Video.Its 非常简单也不太复杂 试一次