sftp上传实现
This commit is contained in:
@@ -30,7 +30,6 @@ import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
@@ -136,6 +135,18 @@ public class CommonUtil {
|
||||
return new String(str.getBytes(charSet), SERVER_CHARSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转换为Ftp服务器默认的ISO-8859-1编码
|
||||
*
|
||||
* @param charSet 字符串编码
|
||||
* @param str 需要转换的字符串
|
||||
* @return 转换后的字符串
|
||||
*/
|
||||
public static String convertSFtpChar(String charSet, String str)
|
||||
throws UnsupportedEncodingException {
|
||||
return new String(str.getBytes(), charSet);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取某包下所有类
|
||||
*
|
||||
@@ -481,7 +492,6 @@ public class CommonUtil {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 校验文件MD5码
|
||||
*/
|
||||
|
||||
@@ -40,6 +40,7 @@ public class ComponentUtil {
|
||||
public static final int COMPONENT_TYPE_HTTP = 1;
|
||||
public static final int COMPONENT_TYPE_FTP = 2;
|
||||
public static final int COMPONENT_TYPE_M3U8 = 3;
|
||||
public static final int COMPONENT_TYPE_SFTP = 4;
|
||||
|
||||
private String TAG = CommonUtil.getClassName(getClass());
|
||||
private static volatile ComponentUtil INSTANCE = null;
|
||||
@@ -82,6 +83,10 @@ public class ComponentUtil {
|
||||
className = "com.arialyy.aria.http.HttpTaskOption";
|
||||
errorStr = "http插件不存在,请添加http插件";
|
||||
break;
|
||||
case COMPONENT_TYPE_SFTP:
|
||||
className = "com.arialyy.aria.sftp.SFtpTaskOption";
|
||||
errorStr = "sftp插件不存在,请添加sftp插件";
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -130,6 +135,9 @@ public class ComponentUtil {
|
||||
case ITaskWrapper.D_SFTP:
|
||||
className = "com.arialyy.aria.sftp.download.SFtpDLoaderUtil";
|
||||
break;
|
||||
case ITaskWrapper.U_SFTP:
|
||||
className = "com.arialyy.aria.sftp.upload.SFtpULoaderUtil";
|
||||
break;
|
||||
}
|
||||
if (className == null) {
|
||||
ALog.e(TAG, "不识别的类名:" + className);
|
||||
@@ -178,6 +186,7 @@ public class ComponentUtil {
|
||||
break;
|
||||
case ITaskWrapper.U_FTP:
|
||||
case ITaskWrapper.U_HTTP:
|
||||
case ITaskWrapper.U_SFTP:
|
||||
className = "com.arialyy.aria.core.listener.BaseUListener";
|
||||
break;
|
||||
case ITaskWrapper.DG_HTTP:
|
||||
|
||||
@@ -37,17 +37,13 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.SequenceInputStream;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -342,21 +338,12 @@ public class FileUtil {
|
||||
|
||||
return false;
|
||||
}
|
||||
streams.add(new FileInputStream(subPath));
|
||||
FileInputStream fis = new FileInputStream(subPath);
|
||||
FileChannel fic = fis.getChannel();
|
||||
foc.transferFrom(fic, fileLen, f.length());
|
||||
fileLen += f.length();
|
||||
fis.close();
|
||||
}
|
||||
Enumeration<FileInputStream> en = Collections.enumeration(streams);
|
||||
SequenceInputStream sis = new SequenceInputStream(en);
|
||||
ReadableByteChannel fic = Channels.newChannel(sis);
|
||||
//ByteBuffer bf = ByteBuffer.allocate(8196);
|
||||
//while (fic.read(bf) != -1) {
|
||||
// bf.flip();
|
||||
// foc.write(bf);
|
||||
// bf.compact();
|
||||
//}
|
||||
foc.transferFrom(fic, 0, fileLen);
|
||||
fic.close();
|
||||
sis.close();
|
||||
ALog.d(TAG, String.format("合并文件耗时:%sms", (System.currentTimeMillis() - startTime)));
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
@@ -405,7 +392,6 @@ public class FileUtil {
|
||||
int i = 0;
|
||||
int threadNum = subPaths.size();
|
||||
long tempLen = targetFileSize / threadNum;
|
||||
ALog.d(TAG, "fileSize = " + targetFileSize);
|
||||
for (String subPath : subPaths) {
|
||||
File f = new File(subPath);
|
||||
if (!f.exists()) {
|
||||
@@ -421,14 +407,13 @@ public class FileUtil {
|
||||
long blockLen = i == (threadNum - 1) ? targetFileSize - tempLen * i : tempLen;
|
||||
FileInputStream fis = new FileInputStream(subPath);
|
||||
FileChannel fic = fis.getChannel();
|
||||
ALog.d(TAG, "blcokLen = " + blockLen);
|
||||
long rLen = foc.transferFrom(fic, 0, blockLen);
|
||||
ALog.d(TAG, "writeLen = " + rLen);
|
||||
foc.transferFrom(fic, blockLen * i, blockLen);
|
||||
fis.close();
|
||||
i ++;
|
||||
i++;
|
||||
}
|
||||
|
||||
ALog.d(TAG, String.format("合并文件耗时:%sms", (System.currentTimeMillis() - startTime)));
|
||||
ALog.d(TAG, String.format("合并文件耗时:%sms,合并后的文件长度:%s", (System.currentTimeMillis() - startTime),
|
||||
file.length()));
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user