当前位置: 首页 > 知识库问答 >
问题:

Android:改版2多文件上传怎么做?

华恩
2023-03-14
File file = new File(path, "theimage");
File file2 = new File(path2, "theimage");
RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
RequestBody requestBody2 = RequestBody.create(MediaType.parse("image/png"), file2);
Map<String, RequestBody> params = new HashMap<>();
params.put("image2", requestBody2 );

Call<ResponseBody> call = service.upload(requestBody, params);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
    Log.v("Upload", "success");
}
public interface FileUploadService {

    @Multipart
    @POST("/upload")
    Call<ResponseBody> upload(
        //@Part("image_logo\"; filename=\"image.png\" ") RequestBody file,
        @Part("file") RequestBody file,
        @PartMap Map<String, RequestBody> params
     //  @Part("description") String description
);

content_type:multipart/form-data;边界=50FbFeB3-3ABC-4F15-B130-CdCB7E3A0E4F

内容帖子:数组([文件]=>PNG IHDR L AlotofBinaryGibberish......snip[文件2]=>PNG IHDR L更多二进制Gibberish...

有人能给我指个正确的方向吗?

HashMap<String, RequestBody> partMap = new HashMap<String, RequestBody>();
partMap.put("file\"; filename=\"" + file.getName(), requestBody);
partMap.put("file\"; filename=\"" + file2.getName(), requestBody);
Call<ResponseBody> call = service.upload(partMap);
@Multipart
@POST("/upload")
Call<ResponseBody> upload(
    @PartMap() Map<String, RequestBody> partMap,

我尝试了这个改版(2.0beta2)多部分文件上传不起作用的解决方案,但得到了一个错误,即@body不能与Multipart一起使用:java.lang.IllegalArgumentException:@body参数不能与表单或多部分编码一起使用。(参数#1)

        for (String key : keys) {
            Bitmap bm = selectedImages.get(key);
            File f = new File(saveToInternalStorage(bm, key), key);
            if (f.exists()) {
                buildernew.addFormDataPart(key, key + ".png", RequestBody.create(MEDIA_TYPE, f));
            }
        }
        RequestBody requestBody = buildernew.build();

-

Call<ResponseBody> upload(
    @Body RequestBody requestBody

共有1个答案

习华灿
2023-03-14
            final MediaType MEDIA_TYPE=MediaType.parse("image/png");
            HashMap<String,RequestBody> map=new HashMap<>(selectedImages.size());
            RequestBody file=null;
            File f=null;
            Set<String> keys = selectedImages.keySet();
            for (String key : keys) {
                try {
                    Bitmap bitmap = selectedImages.get(key);
                    f = new File(saveToInternalStorage(bitmap, key), key);

                    FileOutputStream fos = new FileOutputStream(f);
                    if(bitmap!=null){
                        bitmap.compress(Bitmap.CompressFormat.PNG, 0 , fos);
                        fos.flush();
                        fos.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }

                file=RequestBody.create(MEDIA_TYPE, f);
                map.put(""+key+"\"; filename=\""+key+".jpg",file);
                Log.i("##MYLOG###", "### MAP PUT:" + key + " filename:"+key+".jpg file:" + file.toString() +" type:"+ file.contentType() );
                file=null;
                f = null;
            }

--

Call<ResponseBody> upload(
        @PartMap() Map<String,RequestBody> mapFileAndName //for sending multiple images

--

注意:在使用HttpClient.Interceptors()调试时,我只看到了一个上传,但是当检查endpoint本身以查看它实际得到了什么时,它确实得到了多个上传!

 类似资料:
  • 操作方法:上方文件按钮-上传按钮

  • 我确信,所有GET参数都正确写入。我想,问题在于我如何发送文件上传。 接口: 有人能帮我吗?

  • 我有以下错误: 我为http请求设置了服务器,仍然得到错误:

  • 当以字符串形式发送文件时,如何显示上载进度?我了解了关于这个问题的所有可用帖子,但都使用了带有文件的多部分帖子。但我没有这个文件。我有一根非常非常大的绳子。请帮帮我,谢谢。 我添加了拦截器: 此自定义请求主体: 改装服务: 和上传方法: 但进展只有一次100%发射

  • 这是我的密码。它正常工作。但我想上传其他图像类型,如png,jpeg等。因此,我想更改filename=\“file1.jpeg” 我也想同时发送不同数量的文件。 请帮我解决这个问题。谢谢你。

  • Android版本和代码版本号Android Studio怎么改?我想在Google Play上更改apk文件(app)并且我需要更改Android版本和代码版本号。我在Android Studio中的AndroidManifest.xml文件中尝试了这种方法: 但它不起作用。当我试图在Google Play上发布它时,它显示我必须更改Android版本的名称和代码。