Commit 07e51ac9 by 雷紫添

Merge remote-tracking branch 'remotes/origin/master'

parents 3290889e f5f5f43a
......@@ -4,12 +4,9 @@ import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.net.SocketException;
/**
* ftp上传下载工具类
......@@ -40,8 +37,8 @@ public class FtpUtil {
String filePath, String filename, InputStream input) {
boolean result = false;
String LOCAL_CHARSET = "GBK";
String SERVER_CHARSET = "ISO-8859-1";
String LOCAL_CHARSET = "GBK";
String SERVER_CHARSET = "ISO-8859-1";
FTPClient ftp = new FTPClient();
try {
int reply;
......@@ -199,28 +196,275 @@ public class FtpUtil {
}
public static boolean uploadFtp(InputStream in,String wjmc) {
/**
* 上传文件到ft,支持断点续传,文件名称和ftp路径应使用utf-8编码,避免乱码
* @param host
* @param port
* @param username
* @param password
* @param remotePath 远程ftp文件路径,,支持多级文件路径,格式如"/dir1/dir2/"或者"/dir/",必须以"/"结尾
* @param file
* @return
*/
public static boolean uploadFileToFtp(String host, int port, String username, String password, String remotePath, File file) {
FTPClient ftpClient = new FTPClient();
String LOCAL_CHARSET = "GBK";
boolean result = false;
try {
ftpClient.connect(host,port);
if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {
LOCAL_CHARSET = "UTF-8";
}
ftpClient.setControlEncoding(LOCAL_CHARSET);
ftpClient.login(username,password);
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
System.out.println("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
return false;
} else {
System.out.println("FTP连接成功。");
}
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
//ftpClient.enterLocalActiveMode();
ftpClient.setRemoteVerificationEnabled(false);//本地部署时注释掉该行
} catch (SocketException e) {
e.printStackTrace();
System.out.println("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
System.out.println("FTP的端口错误,请正确配置。");
}
String remoteFileName = file.getName();
try {
remoteFileName = new String(remoteFileName.getBytes("UTF-8"),"iso-8859-1");
remotePath = new String(remotePath.getBytes("UTF-8"),"iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 对远程目录的处理
if (remotePath.contains("/")) {
// 创建服务器远程目录结构,创建失败直接返回
String directory = remotePath.substring(0, remotePath.lastIndexOf("/") + 1);
try {
if (!directory.equalsIgnoreCase("/")&& !ftpClient.changeWorkingDirectory(directory)) {
// 如果远程目录不存在,则递归创建远程服务器目录
int start = 0;
int end = 0;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
while (true) {
String subDirectory = remotePath.substring(start, end);
if (!ftpClient.changeWorkingDirectory(subDirectory)) {
if (ftpClient.makeDirectory(subDirectory)) {
ftpClient.changeWorkingDirectory(subDirectory);
}
else
{
System.out.println("创建目录失败");
return false;
}
}
start = end + 1;
end = directory.indexOf("/", start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
// 检查远程是否存在文件
try {
FTPFile[] files = ftpClient.listFiles(remoteFileName);
if (files.length == 1) {
long remoteSize = files[0].getSize();
long localSize = file.length();
System.out.println("远程文件大小:"+remoteSize+",本地文件大小:"+localSize);
if (remoteSize == localSize) {
System.out.println("文件已上传,无需重复上传");
return true;
} else if (remoteSize > localSize) {
System.out.println("远程文件大小大于本地文件");
return true;
}
// 尝试移动文件内读取指针,实现断点续传
result = uploadFile(remoteFileName,file,ftpClient,remoteSize);
// 如果断点续传没有成功,则删除服务器上文件,重新上传
if(!result){
if (!ftpClient.deleteFile(remoteFileName)) {
return false;
}
result = uploadFile(remoteFileName, file, ftpClient, 0);
}
}else{
result = uploadFile(remoteFileName, file, ftpClient, 0);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return result;
}
private static boolean uploadFile(String remoteFileName, File localFile,FTPClient ftpClient,long remoteSize) throws IOException {
boolean result = false;
// 显示进度的上传
long step = localFile.length() / 100;
long process = 0;
long localreadbytes = 0L;
RandomAccessFile raf = new RandomAccessFile(localFile, "r");
OutputStream out = ftpClient.appendFileStream(remoteFileName);
// 断点续传
if (remoteSize > 0) {
ftpClient.setRestartOffset(remoteSize);
process = remoteSize / step;
raf.seek(remoteSize);
localreadbytes = remoteSize;
System.out.println("进行断点续传");
}
byte[] bytes = new byte[10240];
int c;
while ((c = raf.read(bytes)) != -1) {
out.write(bytes, 0, c);
localreadbytes += c;
if (localreadbytes / step != process) {
process = localreadbytes / step;
System.out.println("上传进度:" + process);
}
}
out.flush();
raf.close();
out.close();
result = ftpClient.completePendingCommand();
if (remoteSize > 0) {
//status = result ? UploadStatus.Upload_From_Break_Success : UploadStatus.Upload_From_Break_Failed;
System.out.println(result ? "Upload_From_Break_Success" : "Upload_From_Break_Failed");
} else {
//status = result ? UploadStatus.Upload_New_File_Success : UploadStatus.Upload_New_File_Failed;
System.out.println(result ? "Upload_New_File_Success" : "Upload_New_File_Failed");
}
return result;
}
//上传文件到ftp文件服务器begin20180806
/* String ip = ftpConfig.getServerip();
int port = Integer.parseInt(ftpConfig.getPortid());
String username = ftpConfig.getUser();
String password = ftpConfig.getPass();
String basepath = ftpConfig.getBathPath();*/
SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
String dateString = sf.format(new Date());
/**
* 删除远程ftp服务器上文件,文件名称和ftp路径应使用utf-8编码,避免乱码
* @param host
* @param port
* @param username
* @param password
* @param remotePath 远程ftp文件路径,,支持多级文件路径,格式如"/dir1/dir2/"或者"/dir/",必须以"/"结尾
* @param file
*/
public static void deleteFtpFile(String host, int port, String username, String password, String remotePath, File file){
FTPClient ftpClient = new FTPClient();
String LOCAL_CHARSET = "GBK";
boolean result = false;
try {
ftpClient.connect(host,port);
if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {
LOCAL_CHARSET = "UTF-8";
}
ftpClient.setControlEncoding(LOCAL_CHARSET);
ftpClient.login(username,password);
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
System.out.println("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
return ;
} else {
System.out.println("FTP连接成功。");
}
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
//ftpClient.enterLocalActiveMode();
ftpClient.setRemoteVerificationEnabled(false);//本地部署时注释掉该行
} catch (SocketException e) {
e.printStackTrace();
System.out.println("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
System.out.println("FTP的端口错误,请正确配置。");
}
//boolean flag = FtpUtil.uploadFile(ip, port, username, password, basepath,"/"+dateString, wjmc, in);
//上传文件到ftp文件服务器end
return true;
String remoteFileName = file.getName();
try {
remoteFileName = new String(remoteFileName.getBytes("UTF-8"),"iso-8859-1");
remotePath = new String(remotePath.getBytes("UTF-8"),"iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 对远程目录的处理
if (remotePath.contains("/")) {
// 创建服务器远程目录结构,创建失败直接返回
String directory = remotePath.substring(0, remotePath.lastIndexOf("/") + 1);
try {
if (!directory.equalsIgnoreCase("/")&& !ftpClient.changeWorkingDirectory(directory)) {
// 如果远程目录不存在,则递归创建远程服务器目录
int start = 0;
int end = 0;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
while (true) {
String subDirectory = remotePath.substring(start, end);
if (!ftpClient.changeWorkingDirectory(subDirectory)) {
if (ftpClient.makeDirectory(subDirectory)) {
ftpClient.changeWorkingDirectory(subDirectory);
}
else
{
System.out.println("创建目录失败");
return ;
}
}
start = end + 1;
end = directory.indexOf("/", start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
System.out.println("删除文件");
ftpClient.deleteFile(remoteFileName);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
}
}
public static void main(String[] args) {
try {
// FileInputStream in=new FileInputStream(new File("D:\\JCCJ-R1100000500002014070001.xml"));
// boolean flag = uploadFile("47.92.129.99", 4546, "ftpuser", "fou3rfnder4SD1", "/home/ftpuser/","/2016/", "JCCJ-两岸三地R1100000500002014070001.xml", in);
boolean flag = downloadFile("47.92.129.99", 4546, "ftpuser", "fou3rfnder4SD1", "2016/", "appone.zip","C:/tmp");
System.out.println(flag);
//boolean flag = downloadFile("47.92.129.99", 4546, "ftpuser", "fou3rfnder4SD1", "2016/", "appone.zip","C:/tmp");
File file = new File(new String("C:/Users/bt/Downloads/ideaIU-2019.3.3 (1).exe".getBytes("UTF-8"),"UTF-8"));
String remptePath = new String("/2017/10/测试1/".getBytes("UTF-8"),"UTF-8");
boolean flag = uploadFileToFtp("47.92.129.99", 4546, "ftpuser", "fou3rfnder4SD1", remptePath, file);
//deleteFtpFile("47.92.129.99", 4546, "ftpuser", "fou3rfnder4SD1", remptePath, file);
System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment