简体   繁体   English

将视频文件从 android 设备上传到 SpringBoot Rest API 的问题

[英]Issue to Upload video file from android device to SpringBoot Rest API

I am stuck to achieve below functionality in android actually I dont have much working experience in android new APIs (API 29)我坚持在 android 中实现以下功能实际上我在 android 新 API(API 29)方面没有太多工作经验

1- Picked video from gallery, it gave me URI and I created IputeStreamRequestBody as per this thread 1- 从图库中挑选视频,它给了我 URI,我根据这个线程创建了 IputeStreamRequestBody

Here is my code for that:这是我的代码:

public class InputStreamRequestBody extends RequestBody {

private final MediaType contentType;
private final ContentResolver contentResolver;
private final Uri uri;

public InputStreamRequestBody(MediaType contentType, ContentResolver contentResolver, Uri uri) {
    if (uri == null) throw new NullPointerException("uri == null");
    this.contentType = contentType;
    this.contentResolver = contentResolver;
    this.uri = uri;
}

@Nullable
@Override
public MediaType contentType() {
    return contentType;
}

@Override
public long contentLength() {
    return -1;
}

@Override
public void writeTo(@NonNull BufferedSink sink) throws IOException {
    try (Source source = Okio.source(contentResolver.openInputStream(uri))) {
        sink.writeAll(source);
    }
}

} }

Step: 3- Here is my retrofit code:步骤: 3- 这是我的 retrofit 代码:

public interface PlatformGroupPostService {

@Multipart
@POST("brainy/api/v0/post")
Observable<NewGroupAPIResponse> createPost(@Part("postData") RequestBody postData,
                                           @Part MultipartBody.Part attachments);

} }

Request hit the server with null instance of file here is my Rest call:请求使用 null 文件实例到达服务器,这是我的 Rest 调用:

//attachments always coming null
    public ResponseEntity<?> createPost(@RequestParam String postData,
                                        @RequestPart(value = "attachments", required = false)
                                                MultipartFile attachments) {
        PostEntity postEntity = getPostEntity(postData);
        return new ResponseEntity<>(postService.createPost(postEntity, attachments), HttpStatus.CREATED);
    }

above rest API is working fine with PostMan request but from Retrofit call its not working.以上 rest API 在 PostMan 请求下工作正常,但来自 ZB9794896D699A785421Z2D376 的调用不工作。 I think I am doing something wrong.我想我做错了什么。

I am struggling to achieve this feature since 4-5 days, applies multiple solution, Like自 4-5 天以来,我一直在努力实现此功能,应用多种解决方案,例如

1- store android file from gallery to cache and then tries to upload it, but same issue. 1-将 android 文件从画廊存储到缓存,然后尝试上传,但同样的问题。 Now kind of overflow for me现在对我来说有点溢出

fun prepareFilePart(partName: String, uri: Uri, context: Context, name: String): MultipartBody.Part {
    val inputStream = context.contentResolver.openInputStream(uri)
    val file = File(context.cacheDir, name+".mp4")
    val fos = FileOutputStream(file)


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        if (inputStream != null) {
            FileUtils.copy(inputStream, fos)
        }
    } else {

    }

    fos.flush();
    fos.getFD().sync();
    fos.close();
    val requestBody: RequestBody =
            RequestBody.create(MediaType.parse(context.contentResolver.getType(uri)), file)
    return MultipartBody.Part.createFormData(partName, name, requestBody)
}

在此处输入图像描述

any help would be really appreciable任何帮助都会非常显着

Hi after long digging I found solution and its working now:嗨,经过长时间的挖掘,我找到了解决方案并且现在可以使用:

Spring Boot multipartfile always null Spring 始终引导多部分文件 null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM