将大文件上传到服务器android

时间:2018-09-12 18:56:25

标签: android retrofit chunks

我尝试将大文件上传到服务器(支持的最大文件大小为128Mb)。如果我正确理解,则服务器可以接收大文件(例如500Mb),但前提是这些文件是以发送的。 我写了下面的方法,希望它可以将中的视频文件上传到服务器。但是,我从服务器收到以下错误:

  

413请求实体太大

以下是我的方法:

    public static void uploadFile(Context context, Uri videoUri, String fileName, File file) throws FileNotFoundException {
        int CHUNK_SIZE = 2048;
        ContentResolver contentResolver = context.getContentResolver();
        final String contentType = contentResolver.getType(videoUri);
        final AssetFileDescriptor fd = contentResolver.openAssetFileDescriptor(videoUri, "r");
        if (fd == null) {
            try {
                throw new FileNotFoundException("could not open file descriptor");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        RequestBody videoFile = new RequestBody() {
            @Override
            public long contentLength() {
                return fd.getDeclaredLength();
            }

            @Override
            public MediaType contentType() {
                return MediaType.parse(contentType);
            }



            @Override
            public void writeTo(BufferedSink sink) throws IOException {

                Source source = null;
                try {
                    source = Okio.source(file);
                    long total = 0;
                    long read;

                    while ((read = source.read(sink.buffer(), CHUNK_SIZE)) != -1) {
                        total += read;
                        sink.flush();
                        Log.e("progress", String.valueOf(total/100/100/100));
                        //this.listener.transferred(total);
                    }
                } finally {
                    Util.closeQuietly(source);
                }
            }
        };

        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("video/*", fileName, videoFile)
                .build();

        Request request = new Request.Builder()
                .url("https://test.ua/api/upload")
                .post(requestBody)
                .header("Authorization", Utils.getHeaderToken())
                .build();

        OkHttpClient client = new OkHttpClient.Builder().build();

        client.newCall(request).enqueue(new Callback() {
            @Override public void onFailure(Call call, IOException e) {
                try {
                    fd.close();
                } catch (IOException ex) {
                    e.addSuppressed(ex);
                }
                Log.e("asdasd", "failed", e);
            }
            @Override public void onResponse(Call call, Response response) throws IOException {
                Log.e("success", String.valueOf(response.code()));
                InputStream in = response.body().byteStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String result, line = reader.readLine();
                result = line;
                while((line = reader.readLine()) != null) {
                    result += line;
                }
                Log.e("success", String.valueOf(result));


                fd.close();
            }
        });
    }

UPD:

服务器使用laravel-chunk-upload,并且它们在前端具有以下设置:

dropzoneOptions: {
                   url: '/upload',
                   dictDefaultMessage: 'Click here',
                   chunksUploaded: function (file, done) {
                       done()
                   },
                   thumbnailWidth: 150,
                   maxFilesize: 250,
                   chunking: false,
                   chunkSize: 1000000, // Bytes
                   required: true,
                   maxFiles: 1,
                   acceptedFiles: 'video/*',
                   headers: {
                       'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                   }
               },

0 个答案:

没有答案