android 上传文件到 c 服务器,android - 将大文件上传到服务器android - 堆栈内存溢出...

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

}

},

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值