pull/330/head
AriaLyy 7 years ago
parent d239ecc1e7
commit 1f527572e7
  1. 56
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsUploadTarget.java
  2. 74
      Aria/src/main/java/com/arialyy/aria/core/upload/FtpUploadTarget.java
  3. 14
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadEntity.java
  4. 2
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadListener.java
  5. 41
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadTarget.java
  6. 3
      Aria/src/main/java/com/arialyy/aria/core/upload/UploadUtil.java
  7. 4
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/IUploadListener.java
  8. 66
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/IUploadUtil.java
  9. 101
      Aria/src/main/java/com/arialyy/aria/core/upload/uploader/Uploader.java
  10. 8
      Aria/src/main/java/com/arialyy/aria/util/CheckUtil.java
  11. 2
      aria/src/main/java/com/arialyy/aria/core/upload/UploadTaskEntity.java

@ -15,12 +15,66 @@
*/ */
package com.arialyy.aria.core.inf; package com.arialyy.aria.core.inf;
import android.support.annotation.NonNull;
import com.arialyy.aria.core.queue.UploadTaskQueue;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.upload.UploadTask;
import com.arialyy.aria.core.upload.UploadTaskEntity;
import com.arialyy.aria.util.CheckUtil;
import java.util.regex.Pattern;
/** /**
* Created by AriaL on 2017/6/29. * Created by AriaL on 2017/6/29.
* 任务组超类 * 任务组超类
*/ */
public abstract class AbsUploadTarget<TARGET extends AbsUploadTarget, ENTITY extends AbsEntity, TASK_ENTITY extends AbsTaskEntity> public abstract class AbsUploadTarget<TARGET extends AbsUploadTarget, ENTITY extends UploadEntity, TASK_ENTITY extends UploadTaskEntity>
extends AbsTarget<TARGET, ENTITY, TASK_ENTITY> { extends AbsTarget<TARGET, ENTITY, TASK_ENTITY> {
/**
* 设置上传路径
*
* @param uploadUrl 上传路径
*/
public TARGET setUploadUrl(@NonNull String uploadUrl) {
CheckUtil.checkDownloadUrl(uploadUrl);
if (mEntity.getUploadUrl().equals(uploadUrl)) return (TARGET) this;
mEntity.setUploadUrl(uploadUrl);
mEntity.update();
return (TARGET) this;
}
/**
* 从数据中读取上传实体如果数据库查不到则新创建一个上传实体
*
* @param filePath 上传文件的文件路径
*/
protected UploadEntity getUploadEntity(String filePath) {
UploadEntity entity = UploadEntity.findFirst(UploadEntity.class, "filePath=?", filePath);
if (entity == null) {
entity = new UploadEntity();
String regex = "[/|\\\\|//]";
Pattern p = Pattern.compile(regex);
String[] strs = p.split(filePath);
String fileName = strs[strs.length - 1];
entity.setFileName(fileName);
entity.setFilePath(filePath);
entity.insert();
}
return entity;
}
/**
* 下载任务是否存在
*/
@Override public boolean taskExists() {
return UploadTaskQueue.getInstance().getTask(mEntity.getFilePath()) != null;
}
/**
* 是否在下载
*/
public boolean isUploading() {
UploadTask task = UploadTaskQueue.getInstance().getTask(mEntity);
return task != null && task.isRunning();
}
} }

@ -0,0 +1,74 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.upload;
import android.text.TextUtils;
import android.util.Log;
import com.arialyy.aria.core.inf.AbsUploadTarget;
import com.arialyy.aria.orm.DbEntity;
/**
* Created by Aria.Lao on 2017/7/27.
* ftp单任务上传
*/
public class FtpUploadTarget
extends AbsUploadTarget<FtpUploadTarget, UploadEntity, UploadTaskEntity> {
private final String TAG = "FtpUploadTarget";
FtpUploadTarget(String filePath, String targetName) {
this.mTargetName = targetName;
mTaskEntity = DbEntity.findFirst(UploadTaskEntity.class, "key=?", filePath);
if (mTaskEntity == null) {
mTaskEntity = new UploadTaskEntity();
mTaskEntity.entity = getUploadEntity(filePath);
}
if (mTaskEntity.entity == null) {
mTaskEntity.entity = getUploadEntity(filePath);
}
mEntity = mTaskEntity.entity;
}
/**
* ftp 用户登录信息
*
* @param userName ftp用户名
* @param password ftp用户密码
*/
public FtpUploadTarget login(String userName, String password) {
return login(userName, password, null);
}
/**
* ftp 用户登录信息
*
* @param userName ftp用户名
* @param password ftp用户密码
* @param account ftp账号
*/
public FtpUploadTarget login(String userName, String password, String account) {
if (TextUtils.isEmpty(userName)) {
Log.e(TAG, "用户名不能为null");
return this;
} else if (TextUtils.isEmpty(password)) {
Log.e(TAG, "密码不能为null");
return this;
}
mTaskEntity.userName = userName;
mTaskEntity.userPw = password;
mTaskEntity.account = account;
return this;
}
}

@ -29,6 +29,8 @@ public class UploadEntity extends AbsNormalEntity implements Parcelable {
@Primary @Primary
private String filePath; //文件路径 private String filePath; //文件路径
private String uploadUrl; //文件上传地址
public String getFilePath() { public String getFilePath() {
return filePath; return filePath;
@ -42,6 +44,14 @@ public class UploadEntity extends AbsNormalEntity implements Parcelable {
return filePath; return filePath;
} }
public String getUploadUrl() {
return uploadUrl;
}
public void setUploadUrl(String uploadUrl) {
this.uploadUrl = uploadUrl;
}
public UploadEntity() { public UploadEntity() {
} }
@ -52,14 +62,16 @@ public class UploadEntity extends AbsNormalEntity implements Parcelable {
@Override public void writeToParcel(Parcel dest, int flags) { @Override public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags); super.writeToParcel(dest, flags);
dest.writeString(this.filePath); dest.writeString(this.filePath);
dest.writeString(this.uploadUrl);
} }
protected UploadEntity(Parcel in) { protected UploadEntity(Parcel in) {
super(in); super(in);
this.filePath = in.readString(); this.filePath = in.readString();
this.uploadUrl = in.readString();
} }
@Ignore public static final Creator<UploadEntity> CREATOR = new Creator<UploadEntity>() { public static final Creator<UploadEntity> CREATOR = new Creator<UploadEntity>() {
@Override public UploadEntity createFromParcel(Parcel source) { @Override public UploadEntity createFromParcel(Parcel source) {
return new UploadEntity(source); return new UploadEntity(source);
} }

@ -15,6 +15,8 @@
*/ */
package com.arialyy.aria.core.upload; package com.arialyy.aria.core.upload;
import com.arialyy.aria.core.upload.uploader.IUploadListener;
/** /**
* Created by lyy on 2017/2/23. * Created by lyy on 2017/2/23.
*/ */

@ -24,6 +24,7 @@ import java.util.regex.Pattern;
/** /**
* Created by lyy on 2017/2/28. * Created by lyy on 2017/2/28.
* http 当文件上传
*/ */
public class UploadTarget extends AbsUploadTarget<UploadTarget, UploadEntity, UploadTaskEntity> { public class UploadTarget extends AbsUploadTarget<UploadTarget, UploadEntity, UploadTaskEntity> {
@ -40,21 +41,6 @@ public class UploadTarget extends AbsUploadTarget<UploadTarget, UploadEntity, Up
mEntity = mTaskEntity.entity; mEntity = mTaskEntity.entity;
} }
private UploadEntity getUploadEntity(String filePath) {
UploadEntity entity = UploadEntity.findFirst(UploadEntity.class, "filePath=?", filePath);
if (entity == null) {
entity = new UploadEntity();
String regex = "[/|\\\\|//]";
Pattern p = Pattern.compile(regex);
String[] strs = p.split(filePath);
String fileName = strs[strs.length - 1];
entity.setFileName(fileName);
entity.setFilePath(filePath);
entity.insert();
}
return entity;
}
/** /**
* 设置userAgent * 设置userAgent
*/ */
@ -63,16 +49,6 @@ public class UploadTarget extends AbsUploadTarget<UploadTarget, UploadEntity, Up
return this; return this;
} }
/**
* 设置上传路径
*
* @param uploadUrl 上传路径
*/
public UploadTarget setUploadUrl(@NonNull String uploadUrl) {
mTaskEntity.uploadUrl = uploadUrl;
return this;
}
/** /**
* 设置服务器需要的附件key * 设置服务器需要的附件key
* *
@ -92,19 +68,4 @@ public class UploadTarget extends AbsUploadTarget<UploadTarget, UploadEntity, Up
mTaskEntity.contentType = contentType; mTaskEntity.contentType = contentType;
return this; return this;
} }
/**
* 下载任务是否存在
*/
@Override public boolean taskExists() {
return UploadTaskQueue.getInstance().getTask(mEntity.getFilePath()) != null;
}
/**
* 是否在下载
*/
public boolean isUploading() {
UploadTask task = UploadTaskQueue.getInstance().getTask(mEntity);
return task != null && task.isRunning();
}
} }

@ -16,6 +16,7 @@
package com.arialyy.aria.core.upload; package com.arialyy.aria.core.upload;
import android.util.Log; import android.util.Log;
import com.arialyy.aria.core.upload.uploader.IUploadListener;
import com.arialyy.aria.util.CheckUtil; import com.arialyy.aria.util.CheckUtil;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -80,7 +81,7 @@ final class UploadUtil implements Runnable {
mListener.onPre(); mListener.onPre();
URL url; URL url;
try { try {
url = new URL(mTaskEntity.uploadUrl); url = new URL(mUploadEntity.getUploadUrl());
mHttpConn = (HttpURLConnection) url.openConnection(); mHttpConn = (HttpURLConnection) url.openConnection();
mHttpConn.setUseCaches(false); mHttpConn.setUseCaches(false);
mHttpConn.setDoOutput(true); mHttpConn.setDoOutput(true);

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.arialyy.aria.core.upload; package com.arialyy.aria.core.upload.uploader;
import com.arialyy.aria.core.inf.IEventListener; import com.arialyy.aria.core.inf.IEventListener;
@ -21,6 +21,6 @@ import com.arialyy.aria.core.inf.IEventListener;
* Created by lyy on 2017/2/9. * Created by lyy on 2017/2/9.
* 上传监听 * 上传监听
*/ */
interface IUploadListener extends IEventListener { public interface IUploadListener extends IEventListener {
} }

@ -0,0 +1,66 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.upload.uploader;
/**
* Created by lyy on 2016/10/31.
* 抽象的下载接口
*/
public interface IUploadUtil {
/**
* 获取文件大小
*/
long getFileSize();
/**
* 获取当前上传位置
*/
long getCurrentLocation();
/**
* 是否正在下载
*
* @return true, 正在下载
*/
boolean isUploading();
/**
* 取消下载
*/
void cancelUpload();
/**
* 停止下载
*/
void stopUpload();
/**
* 开始下载
*/
void startUpload();
/**
* 从上次断点恢复下载
*/
void resumeUpload();
/**
* 设置最大下载速度
*/
void setMaxSpeed(double maxSpeed);
}

@ -0,0 +1,101 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.upload.uploader;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.core.upload.UploadTaskEntity;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by Aria.Lao on 2017/7/27.
* 文件上传器
*/
public class Uploader implements Runnable, IUploadUtil {
private IUploadListener mListener;
private UploadTaskEntity mTaskEntity;
private UploadEntity mEntity;
private long mCurrentLocation = 0;
private Timer mTimer;
private boolean isRunning;
Uploader(IUploadListener listener, UploadTaskEntity taskEntity) {
mListener = listener;
mTaskEntity = taskEntity;
mEntity = mTaskEntity.getEntity();
}
@Override public long getFileSize() {
return mEntity.getFileSize();
}
@Override public long getCurrentLocation() {
return mCurrentLocation;
}
@Override public boolean isUploading() {
return isRunning;
}
@Override public void cancelUpload() {
isRunning = false;
}
@Override public void stopUpload() {
isRunning = false;
}
@Override public void startUpload() {
new Thread(this).start();
}
@Override public void resumeUpload() {
startUpload();
}
@Override public void setMaxSpeed(double maxSpeed) {
}
@Override public void run() {
isRunning = true;
}
/**
* 启动进度获取定时器
*/
private void startTimer() {
mTimer = new Timer(true);
mTimer.schedule(new TimerTask() {
@Override public void run() {
//if (mConstance.isComplete() || !mConstance.isDownloading) {
// closeTimer();
//} else if (mConstance.CURRENT_LOCATION >= 0) {
// mListener.onProgress(mConstance.CURRENT_LOCATION);
//}
}
}, 0, 1000);
}
private void closeTimer() {
if (mTimer != null) {
mTimer.purge();
mTimer.cancel();
}
}
}

@ -77,6 +77,14 @@ public class CheckUtil {
if (TextUtils.isEmpty(downloadUrl)) throw new IllegalArgumentException("下载链接不能为null"); if (TextUtils.isEmpty(downloadUrl)) throw new IllegalArgumentException("下载链接不能为null");
} }
/**
* 检测下载链接是否为null
*/
public static void checkUploadUrl(String downloadUrl) {
if (TextUtils.isEmpty(downloadUrl)) throw new IllegalArgumentException("上传地址不能为null");
}
/** /**
* 检测下载链接组是否为null * 检测下载链接组是否为null
*/ */

@ -15,7 +15,6 @@
*/ */
package com.arialyy.aria.core.upload; package com.arialyy.aria.core.upload;
import com.arialyy.aria.core.download.DownloadEntity;
import com.arialyy.aria.core.inf.AbsTaskEntity; import com.arialyy.aria.core.inf.AbsTaskEntity;
import com.arialyy.aria.orm.OneToOne; import com.arialyy.aria.orm.OneToOne;
import java.util.HashMap; import java.util.HashMap;
@ -26,7 +25,6 @@ import java.util.Map;
* 上传任务实体 * 上传任务实体
*/ */
public class UploadTaskEntity extends AbsTaskEntity<UploadEntity> { public class UploadTaskEntity extends AbsTaskEntity<UploadEntity> {
public String uploadUrl; //上传路径
public String attachment; //文件上传需要的key public String attachment; //文件上传需要的key
public String contentType = "multipart/form-data"; //上传的文件类型 public String contentType = "multipart/form-data"; //上传的文件类型
public String userAgent = "User-Agent"; public String userAgent = "User-Agent";

Loading…
Cancel
Save