|
|
@ -1,25 +1,16 @@ |
|
|
|
package com.dxhy.oss.service; |
|
|
|
package com.dxhy.oss.service; |
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil; |
|
|
|
|
|
|
|
import com.dxhy.oss.utils.SftpPool; |
|
|
|
import com.dxhy.oss.utils.SftpPool; |
|
|
|
import com.jcraft.jsch.ChannelSftp; |
|
|
|
import com.dxhy.oss.utils.SftpPoolException; |
|
|
|
import com.jcraft.jsch.SftpException; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.net.ftp.FTP; |
|
|
|
import org.apache.commons.net.ftp.FTP; |
|
|
|
import org.apache.commons.net.ftp.FTPClient; |
|
|
|
import org.apache.commons.net.ftp.FTPClient; |
|
|
|
import org.apache.commons.net.ftp.FTPFile; |
|
|
|
import org.apache.commons.net.ftp.FTPFile; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.*; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.io.InputStream; |
|
|
|
|
|
|
|
import java.nio.file.Files; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @author jiaohongyang |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Slf4j |
|
|
|
public class FtpService { |
|
|
|
public class FtpService { |
|
|
|
|
|
|
|
|
|
|
@ -30,67 +21,21 @@ public class FtpService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 将输入流的数据上传到sftp作为文件。文件完整路径 = basePath+directory |
|
|
|
* ftp上传文件 |
|
|
|
* |
|
|
|
* |
|
|
|
* @param pathName ftp服务保存地址,完整路径 |
|
|
|
* @param serviceDec |
|
|
|
* @param fileName 上传到ftp的文件名 |
|
|
|
* @param fileName |
|
|
|
* @param originFileName 待上传文件的名称(绝对地址) |
|
|
|
* @param localDec 本地目录 |
|
|
|
|
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
public boolean uploadFile( String serviceDec, String fileName, String localDec) throws InterruptedException { |
|
|
|
public boolean uploadFile(String pathName, String fileName, String originFileName) throws InterruptedException { |
|
|
|
FTPClient ftpClient = null; |
|
|
|
log.info("开始上传文件"); |
|
|
|
InputStream inputStream = null; |
|
|
|
ChannelSftp sftp = null; |
|
|
|
boolean result =true; |
|
|
|
InputStream inputStream; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
sftp = pool.borrowObject(); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
sftp.cd(pathName); |
|
|
|
|
|
|
|
} catch (SftpException e) { |
|
|
|
|
|
|
|
// 目录不存在,则创建文件夹
|
|
|
|
|
|
|
|
String[] dirs = pathName.split("/"); |
|
|
|
|
|
|
|
String tempPath = ""; |
|
|
|
|
|
|
|
for (String dir : dirs) { |
|
|
|
|
|
|
|
if (null == dir || "".equals(dir)) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
tempPath += "/" + dir; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
sftp.cd(tempPath); |
|
|
|
|
|
|
|
} catch (SftpException ex) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
sftp.mkdir(tempPath); |
|
|
|
|
|
|
|
sftp.cd(tempPath); |
|
|
|
|
|
|
|
} catch (SftpException e1) { |
|
|
|
|
|
|
|
e1.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
File orgFile = new File(originFileName); |
|
|
|
|
|
|
|
inputStream = Files.newInputStream(orgFile.toPath()); |
|
|
|
|
|
|
|
sftp.put(inputStream, fileName); |
|
|
|
|
|
|
|
inputStream.close(); |
|
|
|
|
|
|
|
// 删除本地文件,防止服务器空间不足
|
|
|
|
|
|
|
|
FileUtil.del(orgFile); |
|
|
|
|
|
|
|
log.info("上传文件成功"); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
log.info("上传文件失败"); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (sftp != null) { |
|
|
|
|
|
|
|
log.info("回收线程"); |
|
|
|
|
|
|
|
pool.returnObject(sftp); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean uploadFile(FTPClient ftpClient, String serviceDec, String fileName, InputStream inputStream) { |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
inputStream = new FileInputStream(new File(localDec)); |
|
|
|
log.info("开始上传文件"); |
|
|
|
log.info("开始上传文件"); |
|
|
|
|
|
|
|
ftpClient = pool.borrowObject(); |
|
|
|
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); |
|
|
|
ftpClient.setFileType(FTP.BINARY_FILE_TYPE); |
|
|
|
createDirecroty(ftpClient, serviceDec); |
|
|
|
createDirecroty(ftpClient, serviceDec); |
|
|
|
ftpClient.makeDirectory(serviceDec); |
|
|
|
ftpClient.makeDirectory(serviceDec); |
|
|
@ -100,7 +45,9 @@ public class FtpService { |
|
|
|
ftpClient.logout(); |
|
|
|
ftpClient.logout(); |
|
|
|
log.info("上传文件成功"); |
|
|
|
log.info("上传文件成功"); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
log.error("上传文件失败" + e); |
|
|
|
log.error("上传文件失败" + e); |
|
|
|
|
|
|
|
result =false; |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (ftpClient.isConnected()) { |
|
|
|
if (ftpClient.isConnected()) { |
|
|
@ -111,10 +58,26 @@ public class FtpService { |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
log.error("上传文件失败" + e); |
|
|
|
log.error("上传文件失败" + e); |
|
|
|
return false; |
|
|
|
result =false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return result; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean deleteFile(String directory, String deleteFile) { |
|
|
|
|
|
|
|
FTPClient ftpClient = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
ftpClient = pool.borrowObject(); |
|
|
|
|
|
|
|
return ftpClient.deleteFile(directory + File.separator + deleteFile); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (ftpClient != null) { |
|
|
|
|
|
|
|
log.info("回收线程"); |
|
|
|
|
|
|
|
pool.returnObject(ftpClient); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static boolean createDirecroty(FTPClient ftpClient, String remote) throws IOException { |
|
|
|
private static boolean createDirecroty(FTPClient ftpClient, String remote) throws IOException { |
|
|
@ -232,20 +195,41 @@ public class FtpService { |
|
|
|
} |
|
|
|
} |
|
|
|
return list; |
|
|
|
return list; |
|
|
|
} |
|
|
|
} |
|
|
|
public File downloadFile(FTPClient ftpClient, String servicePath, String fileName, String localFilePath) { |
|
|
|
/** |
|
|
|
String name = dowFile(ftpClient, servicePath, fileName, localFilePath); |
|
|
|
* @param servicePath ftp的路径 |
|
|
|
if (name != null && !name.equals("")){ |
|
|
|
* @param fileName 文件名称 |
|
|
|
return new File(fileName); |
|
|
|
* @param localFilePath 下载到本地的文件夹 |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public boolean downloadFile(String servicePath, String fileName, String localFilePath) { |
|
|
|
|
|
|
|
FTPClient ftpClient; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
ftpClient = pool.borrowObject(); |
|
|
|
|
|
|
|
String name = dowFile(ftpClient, servicePath, fileName, localFilePath); |
|
|
|
|
|
|
|
if (name != null && !name.equals("")){ |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (SftpPoolException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param ftpClient |
|
|
|
|
|
|
|
* @param servicePath ftp的路径 |
|
|
|
|
|
|
|
* @param fileName 文件名称 |
|
|
|
|
|
|
|
* @param localFilePath 下载到本地的文件夹 |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
private static String dowFile(FTPClient ftpClient, String servicePath, String fileName, String localFilePath) { |
|
|
|
private static String dowFile(FTPClient ftpClient, String servicePath, String fileName, String localFilePath) { |
|
|
|
InputStream is = null; |
|
|
|
InputStream is = null; |
|
|
|
FileOutputStream fos = null; |
|
|
|
FileOutputStream fos = null; |
|
|
|
try { |
|
|
|
try { |
|
|
|
ftpClient.enterLocalPassiveMode(); |
|
|
|
ftpClient.enterLocalPassiveMode(); |
|
|
|
is = ftpClient.retrieveFileStream(servicePath + fileName);// 获取ftp上的文件
|
|
|
|
is = ftpClient.retrieveFileStream(servicePath + fileName);// 获取ftp上的文件
|
|
|
|
|
|
|
|
directoryIsExists(localFilePath); |
|
|
|
fos = new FileOutputStream(new File(localFilePath + fileName)); |
|
|
|
fos = new FileOutputStream(new File(localFilePath + fileName)); |
|
|
|
// 文件读取方式一
|
|
|
|
// 文件读取方式一
|
|
|
|
int i; |
|
|
|
int i; |
|
|
@ -253,16 +237,14 @@ public class FtpService { |
|
|
|
while ((i = is.read(bytes)) != -1) { |
|
|
|
while ((i = is.read(bytes)) != -1) { |
|
|
|
fos.write(bytes, 0, i); |
|
|
|
fos.write(bytes, 0, i); |
|
|
|
} |
|
|
|
} |
|
|
|
// 文件读取方式二
|
|
|
|
|
|
|
|
//ftpClient.retrieveFile(ftpFilePath, new FileOutputStream(new File(localFilePath)));
|
|
|
|
|
|
|
|
ftpClient.completePendingCommand(); |
|
|
|
ftpClient.completePendingCommand(); |
|
|
|
log.info("FTP文件下载成功!"); |
|
|
|
log.info("FTP文件下载成功!"); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("FTP文件下载失败!" + e); |
|
|
|
log.error("FTP文件下载失败!" + e); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (fos != null) fos.close(); |
|
|
|
if (fos != null) {fos.close();} |
|
|
|
if (is != null) is.close(); |
|
|
|
if (is != null) {is.close();} |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
log.error("下载流关闭失败" + e); |
|
|
|
log.error("下载流关闭失败" + e); |
|
|
|
return null; |
|
|
|
return null; |
|
|
@ -271,68 +253,8 @@ public class FtpService { |
|
|
|
return localFilePath + fileName; |
|
|
|
return localFilePath + fileName; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public boolean downloadFile(String directory, String downloadFile, String saveFile) { |
|
|
|
|
|
|
|
log.info("开始下载文件!"); |
|
|
|
|
|
|
|
ChannelSftp sftp = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
sftp = pool.borrowObject(); |
|
|
|
|
|
|
|
if (directory != null && !"".equals(directory)) { |
|
|
|
|
|
|
|
sftp.cd(directory); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
String file = saveFile + "/" + downloadFile; |
|
|
|
|
|
|
|
File fileLocal = new File(file); |
|
|
|
|
|
|
|
if (fileLocal.exists()) { |
|
|
|
|
|
|
|
boolean delete = fileLocal.delete(); |
|
|
|
|
|
|
|
log.info("删除本地路径文件:{}", delete); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this.directoryIsExists(saveFile); |
|
|
|
|
|
|
|
sftp.get(downloadFile, file); |
|
|
|
|
|
|
|
log.info("下载文件成功!"); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
log.info("下载文件失败!"); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (sftp != null) { |
|
|
|
|
|
|
|
log.info("回收线程"); |
|
|
|
|
|
|
|
pool.returnObject(sftp); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 删除文件 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param directory 要删除文件所在目录 |
|
|
|
|
|
|
|
* @param deleteFile 要删除的文件 |
|
|
|
|
|
|
|
* @return 返回是否删除成功 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean deleteFile(String directory, String deleteFile) { |
|
|
|
|
|
|
|
ChannelSftp sftp = null; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
sftp = pool.borrowObject(); |
|
|
|
|
|
|
|
sftp.cd(directory); |
|
|
|
|
|
|
|
sftp.rm(deleteFile); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} finally { |
|
|
|
|
|
|
|
if (sftp != null) { |
|
|
|
|
|
|
|
log.info("回收线程"); |
|
|
|
|
|
|
|
pool.returnObject(sftp); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void directoryIsExists(String path) { |
|
|
|
public static void directoryIsExists(String path) { |
|
|
|
File directory = new File(path); |
|
|
|
File directory = new File(path); |
|
|
|
if (directory.exists()) { |
|
|
|
if (directory.exists()) { |
|
|
|
return; |
|
|
|
return; |
|
|
|