说下断点下载原理:
设置httpURLConnection的setRequestProperty()方法中的Range参数为已下载量即可,如果不设置默认会从0开始下载
断点上传原理:
数据库实时记录上传的状态,如果中断,记录下已上传的大小,当再次上传时FileInputStream有个skip方法,把已上传大小传进去,就会跳过之前下载的,实现断点上传
断点下载代码:
httpURLConnection.setRequestProperty("Range", "bytes=" + mDownLoadTask.getDownloadSize() + "-");
总码
URL url = new URL(mDownLoadTask.getUrl());
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setRequestProperty("Range", "bytes=" + mDownLoadTask.getDownloadSize() + "-");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
InputStream inputStream = httpURLConnection.getInputStream();
断点上传代码:
FileInputStream fileInputStream = new FileInputStream(mUploadTask.getFilePath());
fileInputStream.skip(mUploadTask.getUploadSize());
byte[] bs = new byte[1024 * 300];
int total = 0;
if((total = fileInputStream.read(bs)) != -1) {
mUploadTask= mUploadManager.getTask(mUploadTask.getId());
if (mUploadTask.getState()==UploadConfig.STATE_PAUSE){
System.out.println("当前任务已经暂停: "+mUploadTask.getName());
return;
}
outputStream.write(bs, 0, total);
mUploadManager.updateTaskUploadSize(mUploadTask,total);
}
全
//sha1=??&size=??&pid=??
URL url = new URL(Constant.UPLOAD_BREAK_URL + "?sha1=" + mUploadTask.getSha1() +
"&size=" + mUploadTask.getTotalSize() + "&pid=" + mUploadTask.getPid());
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
String fuid = mSharePLogin.getData("fuid");
String token = mSharePLogin.getData("token");
httpURLConnection.setRequestProperty("Cookie", "fuid=" + fuid + "; token=" + token);
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
//这个头需要,因为要告诉服务器分割线和接收方式为二进制字节数据
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + fgx);
OutputStream outputStream = httpURLConnection.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(mUploadTask.getFilePath());
fileInputStream.skip(mUploadTask.getUploadSize());
byte[] bs = new byte[1024 * 300];
//上传数据
StringBuilder stringBuilder = new StringBuilder();
//aid
stringBuilder.append(fgx);
stringBuilder.append("Content-Disposition: form-data; name=\"aid\"");
stringBuilder.append(hh);
stringBuilder.append(hh);
stringBuilder.append("1");
stringBuilder.append(hh);
stringBuilder.append(fgx);
//pid
stringBuilder.append("Content-Disposition: form-data; name=\"pid\"");
stringBuilder.append(hh);
stringBuilder.append(hh);
stringBuilder.append(mUploadTask.getPid());
stringBuilder.append(hh);
stringBuilder.append(fgx);
//name
stringBuilder.append("Content-Disposition: form-data; name=\"name\"");
stringBuilder.append(hh);
stringBuilder.append(hh);
stringBuilder.append(mUploadTask.getName());
stringBuilder.append(hh);
stringBuilder.append(fgx);
//文件数据
stringBuilder.append("Content-Disposition: form-data; name=\"Filedata\"; filename=\"" + mUploadTask.getName() + "\"");
stringBuilder.append(hh);
stringBuilder.append("Content-Type: text/plain");
stringBuilder.append(hh);
stringBuilder.append(hh);
int count = 0;
//先把上面这些写出去
outputStream.write(stringBuilder.toString().getBytes());
int total = 0;
if((total = fileInputStream.read(bs)) != -1) {
mUploadTask= mUploadManager.getTask(mUploadTask.getId());
if (mUploadTask.getState()==UploadConfig.STATE_PAUSE){
System.out.println("当前任务已经暂停: "+mUploadTask.getName());
return;
}
outputStream.write(bs, 0, total);
mUploadManager.updateTaskUploadSize(mUploadTask,total);
}
System.out.println("文件总大小:" + mUploadTask.getTotalSizeFormat() + " " + mUploadTask.getTotalSize());
System.out.println("上传了300K,总共上传了:" + mUploadTask.getuploadFormat() + " " + mUploadTask.getUploadSize());
outputStream.write(hh.getBytes());
outputStream.write(gg.getBytes());
InputStream inputStream = httpURLConnection.getInputStream();
inputStream.read();
fileInputStream.close();
outputStream.close();
httpURLConnection.disconnect();