我试图将大文件上传到服务器(支持的最大文件大小为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:

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')

}

},

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐