speed
This commit is contained in:
@ -39,6 +39,7 @@ import com.arialyy.aria.orm.DbEntity;
|
||||
import com.arialyy.aria.orm.DbUtil;
|
||||
import com.arialyy.aria.util.CAConfiguration;
|
||||
import com.arialyy.aria.util.Configuration;
|
||||
import com.arialyy.aria.util.Speed;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
@ -79,6 +80,13 @@ import java.util.Map;
|
||||
return mReceivers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置最大下载速度
|
||||
*/
|
||||
public void setMaxSpeed(Speed speed) {
|
||||
Configuration.getInstance().setMaxSpeed(speed);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置命令
|
||||
*/
|
||||
|
@ -18,6 +18,7 @@ package com.arialyy.aria.core.download;
|
||||
import android.util.Log;
|
||||
import com.arialyy.aria.util.BufferedRandomAccessFile;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import com.arialyy.aria.util.Configuration;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -31,12 +32,13 @@ import java.util.Properties;
|
||||
* 下载线程
|
||||
*/
|
||||
final class SingleThreadTask implements Runnable {
|
||||
private static final int BUF_SIZE = 8192;
|
||||
private static final String TAG = "SingleThreadTask";
|
||||
private DownloadUtil.ConfigEntity mConfigEntity;
|
||||
private String mConfigFPath;
|
||||
private long mChildCurrentLocation = 0;
|
||||
private static final Object LOCK = new Object();
|
||||
private int mBufSize = 8192;
|
||||
//private int mBufSize = 64;
|
||||
private IDownloadListener mListener;
|
||||
private DownloadStateConstance mConstance;
|
||||
|
||||
@ -48,6 +50,7 @@ final class SingleThreadTask implements Runnable {
|
||||
if (mConfigEntity.isSupportBreakpoint) {
|
||||
mConfigFPath = downloadInfo.CONFIG_FILE_PATH;
|
||||
}
|
||||
//mBufSize = Configuration.getInstance().getMaxSpeed();
|
||||
}
|
||||
|
||||
@Override public void run() {
|
||||
@ -77,10 +80,10 @@ final class SingleThreadTask implements Runnable {
|
||||
is = conn.getInputStream();
|
||||
//创建可设置位置的文件
|
||||
BufferedRandomAccessFile file =
|
||||
new BufferedRandomAccessFile(mConfigEntity.TEMP_FILE, "rwd", BUF_SIZE);
|
||||
new BufferedRandomAccessFile(mConfigEntity.TEMP_FILE, "rwd", mBufSize);
|
||||
//设置每条线程写入文件的位置
|
||||
file.seek(mConfigEntity.START_LOCATION);
|
||||
byte[] buffer = new byte[BUF_SIZE];
|
||||
byte[] buffer = new byte[mBufSize];
|
||||
int len;
|
||||
//当前子线程的下载位置
|
||||
mChildCurrentLocation = mConfigEntity.START_LOCATION;
|
||||
|
@ -29,8 +29,8 @@ public class UploadTaskEntity extends ITaskEntity {
|
||||
public String uploadUrl; //上传路径
|
||||
public String attachment; //文件上传需要的key
|
||||
public String contentType = "multipart/form-data"; //上传的文件类型
|
||||
public String charset = "utf-8";
|
||||
public String userAgent = "User-Agent";
|
||||
public String charset = "utf-8";
|
||||
|
||||
/**
|
||||
* 文件上传表单
|
||||
|
@ -35,7 +35,7 @@ import java.util.UUID;
|
||||
* Created by Aria.Lao on 2017/2/9.
|
||||
* 上传工具
|
||||
*/
|
||||
public class UploadUtil implements Runnable {
|
||||
final class UploadUtil implements Runnable {
|
||||
private static final String TAG = "UploadUtil";
|
||||
private final String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成
|
||||
private final String PREFIX = "--", LINE_END = "\r\n";
|
||||
@ -46,8 +46,9 @@ public class UploadUtil implements Runnable {
|
||||
private long mCurrentLocation = 0;
|
||||
private boolean isCancel = false;
|
||||
private boolean isRunning = false;
|
||||
private OutputStream mOutputStream;
|
||||
|
||||
public UploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) {
|
||||
UploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) {
|
||||
mTaskEntity = taskEntity;
|
||||
CheckUtil.checkUploadEntity(taskEntity.uploadEntity);
|
||||
mUploadEntity = taskEntity.uploadEntity;
|
||||
@ -100,9 +101,9 @@ public class UploadUtil implements Runnable {
|
||||
mHttpConn.setRequestProperty(key, mTaskEntity.headers.get(key));
|
||||
}
|
||||
|
||||
OutputStream outputStream = mHttpConn.getOutputStream();
|
||||
mOutputStream = mHttpConn.getOutputStream();
|
||||
PrintWriter writer =
|
||||
new PrintWriter(new OutputStreamWriter(outputStream, mTaskEntity.charset), true);
|
||||
new PrintWriter(new OutputStreamWriter(mOutputStream, mTaskEntity.charset), true);
|
||||
|
||||
//添加文件上传表单字段
|
||||
keys = mTaskEntity.formFields.keySet();
|
||||
@ -110,7 +111,7 @@ public class UploadUtil implements Runnable {
|
||||
addFormField(writer, key, mTaskEntity.formFields.get(key));
|
||||
}
|
||||
mListener.onStart(uploadFile.length());
|
||||
uploadFile(writer, outputStream, mTaskEntity.attachment, uploadFile);
|
||||
uploadFile(writer, mTaskEntity.attachment, uploadFile);
|
||||
Log.d(TAG, finish(writer) + "");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@ -149,8 +150,8 @@ public class UploadUtil implements Runnable {
|
||||
* @param attachment 文件上传attachment
|
||||
* @throws IOException
|
||||
*/
|
||||
private void uploadFile(PrintWriter writer, OutputStream outputStream, String attachment,
|
||||
File uploadFile) throws IOException {
|
||||
private void uploadFile(PrintWriter writer, String attachment, File uploadFile)
|
||||
throws IOException {
|
||||
Log.e(TAG, "uploadFile");
|
||||
writer.append(PREFIX).append(BOUNDARY).append(LINE_END);
|
||||
writer.append("Content-Disposition: form-data; name=\"")
|
||||
@ -171,7 +172,7 @@ public class UploadUtil implements Runnable {
|
||||
int bytesRead;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
mCurrentLocation += bytesRead;
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
mOutputStream.write(buffer, 0, bytesRead);
|
||||
if (isCancel) {
|
||||
break;
|
||||
}
|
||||
@ -179,7 +180,7 @@ public class UploadUtil implements Runnable {
|
||||
mListener.onProgress(mCurrentLocation);
|
||||
}
|
||||
|
||||
outputStream.flush();
|
||||
mOutputStream.flush();
|
||||
//outputStream.close(); //不能调用,否则服务器端异常
|
||||
inputStream.close();
|
||||
writer.append(LINE_END);
|
||||
@ -219,7 +220,7 @@ public class UploadUtil implements Runnable {
|
||||
|
||||
writer.flush();
|
||||
writer.close();
|
||||
|
||||
mOutputStream.close();
|
||||
return response.toString();
|
||||
}
|
||||
}
|
||||
|
@ -29,33 +29,38 @@ import java.util.WeakHashMap;
|
||||
* 信息配置
|
||||
*/
|
||||
public class Configuration {
|
||||
private static final String TAG = "Configuration";
|
||||
private static final String CONFIG_FILE = "/Aria/ADConfig.properties";
|
||||
private static final String TAG = "Configuration";
|
||||
private static final String CONFIG_FILE = "/Aria/ADConfig.properties";
|
||||
/**
|
||||
* 当前调度器最大下载数,默认最大下载数为 “2”
|
||||
*/
|
||||
private static final String DOWNLOAD_NUM = "DOWNLOAD_NUM";
|
||||
private static final String DOWNLOAD_NUM = "DOWNLOAD_NUM";
|
||||
/**
|
||||
* 失败重试次数,默认最多重试 10 次
|
||||
*/
|
||||
private static final String RE_TRY_NUM = "RE_TRY_NUM";
|
||||
private static final String RE_TRY_NUM = "RE_TRY_NUM";
|
||||
/**
|
||||
* 是否打开下载广播,默认 false
|
||||
*/
|
||||
private static final String OPEN_BROADCAST = "OPEN_BROADCAST";
|
||||
private static final String OPEN_BROADCAST = "OPEN_BROADCAST";
|
||||
/**
|
||||
* 失败重试间隔时间,默认 4000 ms
|
||||
*/
|
||||
private static final String RE_TRY_INTERVAL = "RE_TRY_INTERVAL";
|
||||
private static final String RE_TRY_INTERVAL = "RE_TRY_INTERVAL";
|
||||
/**
|
||||
* 超时时间,默认 10000 ms
|
||||
*/
|
||||
private static final String DOWNLOAD_TIME_OUT = "DOWNLOAD_TIME_OUT";
|
||||
/**
|
||||
* 设置最大速度
|
||||
*/
|
||||
private static final String MAX_SPEED = "MAX_SPEED";
|
||||
|
||||
public static boolean isOpenBreadCast = false;
|
||||
|
||||
private static Configuration INSTANCE = null;
|
||||
private File mConfigFile = null;
|
||||
private static final Object LOCK = new Object();
|
||||
private static Configuration INSTANCE = null;
|
||||
private File mConfigFile = null;
|
||||
private static final Object LOCK = new Object();
|
||||
|
||||
public static Configuration getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
@ -73,7 +78,7 @@ public class Configuration {
|
||||
mConfigFile.getParentFile().mkdirs();
|
||||
mConfigFile.createNewFile();
|
||||
init();
|
||||
}else {
|
||||
} else {
|
||||
isOpenBreadCast = isOpenBroadcast();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
@ -88,6 +93,7 @@ public class Configuration {
|
||||
config.put(OPEN_BROADCAST, false + "");
|
||||
config.put(RE_TRY_INTERVAL, 4000 + "");
|
||||
config.put(DOWNLOAD_TIME_OUT, 10000 + "");
|
||||
config.put(MAX_SPEED, 64 + "");
|
||||
saveConfig(config);
|
||||
}
|
||||
|
||||
@ -95,8 +101,8 @@ public class Configuration {
|
||||
if (config == null || config.size() == 0) {
|
||||
return;
|
||||
}
|
||||
Properties properties = CommonUtil.loadConfig(mConfigFile);
|
||||
Set<String> keys = config.keySet();
|
||||
Properties properties = CommonUtil.loadConfig(mConfigFile);
|
||||
Set<String> keys = config.keySet();
|
||||
for (String key : keys) {
|
||||
properties.setProperty(key, config.get(key));
|
||||
}
|
||||
@ -109,6 +115,20 @@ public class Configuration {
|
||||
saveConfig(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置最大下载速度
|
||||
*/
|
||||
public void setMaxSpeed(Speed speed) {
|
||||
save(MAX_SPEED, speed.buf + "");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取最大速度
|
||||
*/
|
||||
public int getMaxSpeed() {
|
||||
return Integer.parseInt(CommonUtil.loadConfig(mConfigFile).getProperty(MAX_SPEED, "8192"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下载超时时间
|
||||
*
|
||||
|
@ -1,144 +0,0 @@
|
||||
package com.arialyy.aria.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This utility class provides an abstraction layer for sending multipart HTTP
|
||||
* POST requests to a web server.
|
||||
*
|
||||
* @author www.codejava.net
|
||||
*/
|
||||
public class MultipartUtility {
|
||||
private final String boundary;
|
||||
private static final String LINE_FEED = "\r\n";
|
||||
private HttpURLConnection httpConn;
|
||||
private String charset;
|
||||
private OutputStream outputStream;
|
||||
private PrintWriter writer;
|
||||
|
||||
/**
|
||||
* This constructor initializes a new HTTP POST request with content type
|
||||
* is set to multipart/form-data
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public MultipartUtility(String requestURL, String charset) throws IOException {
|
||||
this.charset = charset;
|
||||
|
||||
// creates a unique boundary based on time stamp
|
||||
boundary = "===" + System.currentTimeMillis() + "===";
|
||||
|
||||
URL url = new URL(requestURL);
|
||||
httpConn = (HttpURLConnection) url.openConnection();
|
||||
httpConn.setUseCaches(false);
|
||||
httpConn.setDoOutput(true); // indicates POST method
|
||||
httpConn.setDoInput(true);
|
||||
httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
|
||||
//httpConn.setRequestProperty("User-Agent", "CodeJava Agent");
|
||||
//httpConn.setRequestProperty("Test", "Bonjour");
|
||||
outputStream = httpConn.getOutputStream();
|
||||
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a form field to the request
|
||||
*
|
||||
* @param name field name
|
||||
* @param value field value
|
||||
*/
|
||||
public void addFormField(String name, String value) {
|
||||
writer.append("--" + boundary).append(LINE_FEED);
|
||||
writer.append("Content-Disposition: form-data; name=\"" + name + "\"").append(LINE_FEED);
|
||||
writer.append("Content-Type: text/plain; charset=" + charset).append(LINE_FEED);
|
||||
writer.append(LINE_FEED);
|
||||
writer.append(value).append(LINE_FEED);
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a upload file section to the request
|
||||
*
|
||||
* @param fieldName name attribute in <input type="file" name="..." />
|
||||
* @param uploadFile a File to be uploaded
|
||||
* @throws IOException
|
||||
*/
|
||||
public void addFilePart(String fieldName, File uploadFile) throws IOException {
|
||||
String fileName = uploadFile.getName();
|
||||
writer.append("--" + boundary).append(LINE_FEED);
|
||||
writer.append(
|
||||
"Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"")
|
||||
.append(LINE_FEED);
|
||||
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName))
|
||||
.append(LINE_FEED);
|
||||
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
|
||||
writer.append(LINE_FEED);
|
||||
writer.flush();
|
||||
|
||||
FileInputStream inputStream = new FileInputStream(uploadFile);
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead = -1;
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
outputStream.flush();
|
||||
inputStream.close();
|
||||
|
||||
writer.append(LINE_FEED);
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a header field to the request.
|
||||
*
|
||||
* @param name - name of the header field
|
||||
* @param value - value of the header field
|
||||
*/
|
||||
public void addHeaderField(String name, String value) {
|
||||
writer.append(name + ": " + value).append(LINE_FEED);
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Completes the request and receives response from the server.
|
||||
*
|
||||
* @return a list of Strings as response in case the server returned
|
||||
* status OK, otherwise an exception is thrown.
|
||||
* @throws IOException
|
||||
*/
|
||||
public List<String> finish() throws IOException {
|
||||
List<String> response = new ArrayList<String>();
|
||||
|
||||
writer.append(LINE_FEED).flush();
|
||||
writer.append("--" + boundary + "--").append(LINE_FEED);
|
||||
writer.close();
|
||||
|
||||
// checks server's status code first
|
||||
int status = httpConn.getResponseCode();
|
||||
if (status == HttpURLConnection.HTTP_OK) {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
|
||||
String line = null;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
response.add(line);
|
||||
}
|
||||
reader.close();
|
||||
httpConn.disconnect();
|
||||
} else {
|
||||
throw new IOException("Server returned non-OK status: " + status);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
33
Aria/src/main/java/com/arialyy/aria/util/Speed.java
Normal file
33
Aria/src/main/java/com/arialyy/aria/util/Speed.java
Normal file
@ -0,0 +1,33 @@
|
||||
package com.arialyy.aria.util;
|
||||
|
||||
/**
|
||||
* Created by Aria.Lao on 2017/3/6.
|
||||
*/
|
||||
public enum Speed {
|
||||
/**
|
||||
* 最大速度为256kb
|
||||
*/
|
||||
KB_256(64),
|
||||
/**
|
||||
* 最大速度为512kb
|
||||
*/
|
||||
KB_512(128),
|
||||
/**
|
||||
* 最大速度为1mb
|
||||
*/
|
||||
MB_1(256),
|
||||
/**
|
||||
* 最大速度为2mb
|
||||
*/
|
||||
MB_2(1024),
|
||||
/**
|
||||
* 最大速度为10mb
|
||||
*/
|
||||
MAX(8192);
|
||||
int buf;
|
||||
|
||||
Speed(int buf) {
|
||||
this.buf = buf;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user