Android
音频分片上传
/**
* 切割MP3方法
*/
public void splitMp3() {
File file1 = new File("/sdcard/" + i + ".mp3");
int size = 1024 * 1024;
byte[] temp = new byte[size];
try {
RandomAccessFile raf = new RandomAccessFile("/sdcard/recorder.mp3", "rw");
raf.seek((i - 1) * size + 1);
int len = raf.read(temp);
if (len == -1) {
return;
}
FileOutputStream fos = new FileOutputStream(file1);
fos.write(temp, 0, len);
raf.close();
fos.close();
i++;
splitMp3();
} catch (Exception e) {
e.printStackTrace();
}
}
OkHttpClient mOkHttpClient;
private void uploadAudio() {
mOkHttpClient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
diGui();
}
private int j = 1;
private void diGui() {
if (j > i - 1) {
return;
}
final File file1 = new File("/sdcard/" + j + ".mp3");
int user_id = 0;
MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
if (file1 != null) {
// MediaType.parse() 里面是上传的文件类型。
RequestBody body = RequestBody.create(MediaType.parse("audio/mp3"), file1);
String filename = file1.getName();
// 上传的参数可以跟你们后台协商,参数分别为, 请求key ,文件名称 , RequestBody
requestBody.addFormDataPart("upType", "record");
requestBody.addFormDataPart("lastModified", String.valueOf(file1.lastModified()) + user_id);
requestBody.addFormDataPart("total", String.valueOf(i - 1));
requestBody.addFormDataPart("index", String.valueOf(j));
requestBody.addFormDataPart("fileName", filename);
requestBody.addFormDataPart("data", filename, body);
}
Request request = new Request.Builder()
.url(“”)
.post(requestBody.build())
.tag(this)
.build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//请求失败的处理
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
for (int a = 0; a < i; a++) {
File file = new File("/sdcard/" + a + ".mp3");
if (file.exists()) {
file.delete();
}
}
i = 1;
j = 1;
// webview.loadUrl("javascript:reciveUpload('" + "error" + "');");
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//请求成功的处理
j++;
final String result = response.body().string();
String mes = "error";
try {
JSONObject jsonObject = new JSONObject(result);
if (!jsonObject.optString("url").isEmpty()) {
mes = jsonObject.optString("url");
} else {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
file1.delete();
diGui();
}
});
return;
}
final String finalMes = "/" + mes;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
file1.delete();
for (int a = 0; a < i; a++) {
File file = new File("/sdcard/" + a + ".mp3");
if (file.exists()) {
file.delete();
}
}
i = 1;
j = 1;
// webview.loadUrl("javascript:reciveUpload('" + finalMes + "');");
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
视频分片上传
/**
* 切割视频的方法
*/
public void splitVideo(String videoPath) {
int size = 512 * 1024;
byte[] temp = new byte[size];
try {
RandomAccessFile raf = new RandomAccessFile(videoPath, "rw");
if (i == 1) {
raf.seek(0);
} else {
raf.seek((i - 1) * size);
}
int len = raf.read(temp);
if (len == -1) {
LogUtils.showLog( "切片结束");
return;
}
File file1 = new File("/sdcard/" + i + ".mp4");
LogUtils.showLog("创建:" + i + ".mp4");
FileOutputStream fos = new FileOutputStream(file1);
fos.write(temp, 0, len);
raf.close();
fos.close();
i++;
splitVideo(videoPath);
} catch (Exception e) {
e.printStackTrace();
}
}
OkHttpClient mOkHttpClient;
/**
* 上传video
*/
private void uploadVideo() {
mOkHttpClient = new OkHttpClient.Builder()
.connectTimeout(1000, TimeUnit.SECONDS)
.readTimeout(1000, TimeUnit.SECONDS)
.writeTimeout(1000, TimeUnit.SECONDS)
.build();
diGui();
}
private int i = 1;
private int j = 1;
private String videoOnline="";
private void diGui() {
if (j > i - 1) {
return;
}
int user_id = 0;
final File file1 = new File("/sdcard/" + j + ".mp4");
LogUtils.showLog("上传文件" + j);
MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
if (file1 != null) {
RequestBody body = RequestBody.create(MediaType.parse("video/MP4"), file1);
String filename = file1.getName();
// 参数分别为, 请求key ,文件名称 , RequestBody
requestBody.addFormDataPart("type", "1");
requestBody.addFormDataPart("lastModified", String.valueOf(file1.lastModified())+user_id);
requestBody.addFormDataPart("total", String.valueOf(i - 1));
requestBody.addFormDataPart("index", String.valueOf(j));
requestBody.addFormDataPart("fileName", filename);
requestBody.addFormDataPart("data", filename, body);
}
String uploadUrl= "服务器地址";
Request request = new Request.Builder().url(uploadUrl).post(requestBody.build()).tag(this).build();
Call call = mOkHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
LogUtils.showLog("上传失败");
dialog.dismiss();
for (int a = 0; a < i; a++) {
File file = new File("/sdcard/" + a + ".mp4");
if (file.exists()) {
file.delete();
}
}
i = 1;
j = 1;
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
j++;
final String result = response.body().string();
LogUtils.showLog("result" + result);
try {
final JSONObject jsonObject = new JSONObject(result);
runOnUiThread(new Runnable() {
@Override
public void run() {
LogUtils.showLog("上传:"+TextUtils.isEmpty(jsonObject.optString("url")));
if (TextUtils.isEmpty(jsonObject.optString("url"))) {
file1.delete();
LogUtils.showLog("文件上传完成,删除");
diGui();
}else{
videoOnline=jsonObject.optString("url");
file1.delete();
LogUtils.showLog("videoOnline:"+videoOnline);
i = 1;
j = 1;
// jumpSendActivity();
}
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
有问题可以在下面留言
如果有帮助请帮忙点个赞,3Q