|
@@ -42,13 +42,14 @@ public class FtpUtil {
|
|
|
*/
|
|
|
private static ObjectPool<FTPClient> ftpClientPool;
|
|
|
|
|
|
- private static boolean IS_CREATE_TBL;
|
|
|
+ private static boolean IS_CREATE_TBL;
|
|
|
|
|
|
public static void setIS_CREATE_TBL(boolean isCreateTbl) {
|
|
|
IS_CREATE_TBL = isCreateTbl;
|
|
|
}
|
|
|
|
|
|
- public static String pre_tbl="thumbnail-";
|
|
|
+ public static String pre_tbl = "thumbnail-";
|
|
|
+
|
|
|
|
|
|
* 初始化ftpClientPool
|
|
|
*
|
|
@@ -124,15 +125,15 @@ public class FtpUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public static void retrieveAllFTPFiles(String remotePath,List<FTPFile> ftpFileList) throws IOException {
|
|
|
+ public static void retrieveAllFTPFiles(String remotePath, List<FTPFile> ftpFileList) throws IOException {
|
|
|
FTPClient ftpClient = getFtpClient();
|
|
|
try {
|
|
|
FTPFile[] ftpFiles = ftpClient.listFiles(encodingPath(remotePath + "/"),
|
|
|
- file -> file != null && file.getSize() > 0 );
|
|
|
- for(FTPFile ftpFile:ftpFiles){
|
|
|
- if(ftpFile.isDirectory()){
|
|
|
- retrieveAllFTPFiles(remotePath+"/"+ftpFile.getName(),ftpFileList);
|
|
|
- }else{
|
|
|
+ file -> file != null && file.getSize() > 0);
|
|
|
+ for (FTPFile ftpFile : ftpFiles) {
|
|
|
+ if (ftpFile.isDirectory()) {
|
|
|
+ retrieveAllFTPFiles(remotePath + "/" + ftpFile.getName(), ftpFileList);
|
|
|
+ } else {
|
|
|
ftpFileList.add(ftpFile);
|
|
|
}
|
|
|
}
|
|
@@ -222,6 +223,7 @@ public class FtpUtil {
|
|
|
|
|
|
|
|
|
* 上传Excel文件到FTP
|
|
|
+ *
|
|
|
* @param workbook
|
|
|
* @param remoteFilePath
|
|
|
* @throws IOException
|
|
@@ -244,7 +246,7 @@ public class FtpUtil {
|
|
|
}
|
|
|
|
|
|
@SneakyThrows
|
|
|
- public static boolean uploadFile2Ftp(byte[] bytes,String remoteFilePath) {
|
|
|
+ public static boolean uploadFile2Ftp(byte[] bytes, String remoteFilePath) {
|
|
|
Assert.notNull(bytes, "bytes cannot be null.");
|
|
|
Assert.hasText(remoteFilePath, "remoteFilePath cannot be null or blank.");
|
|
|
FTPClient ftpClient = getFtpClient();
|
|
@@ -261,13 +263,14 @@ public class FtpUtil {
|
|
|
|
|
|
|
|
|
* 文件上传,上传字节数组
|
|
|
- * @param bytes 字节数组
|
|
|
+ *
|
|
|
+ * @param bytes 字节数组
|
|
|
* @param folderName 文件路径
|
|
|
- * @param fileName 文件名称
|
|
|
+ * @param fileName 文件名称
|
|
|
* @return
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
- public static boolean uploadFile2Ftp(byte[] bytes,String folderName,String fileName) {
|
|
|
+ public static boolean uploadFile2Ftp(byte[] bytes, String folderName, String fileName) {
|
|
|
Assert.notNull(bytes, "bytes cannot be null.");
|
|
|
Assert.hasText(folderName, "folderName cannot be null or blank.");
|
|
|
Assert.hasText(fileName, "fileName cannot be null or blank.");
|
|
@@ -275,17 +278,17 @@ public class FtpUtil {
|
|
|
FTPClient ftpClient = FtpUtil.getFtpClient();
|
|
|
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);) {
|
|
|
if (!ftpClient.changeWorkingDirectory(folderName)) {
|
|
|
- log.info("创建文件夹"+ftpClient.makeDirectory(folderName));
|
|
|
- log.info("切换到文件夹下"+ftpClient.changeWorkingDirectory(folderName));
|
|
|
+ log.info("创建文件夹" + ftpClient.makeDirectory(folderName));
|
|
|
+ log.info("切换到文件夹下" + ftpClient.changeWorkingDirectory(folderName));
|
|
|
}
|
|
|
if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) {
|
|
|
flag = ftpClient.storeFile(fileName, bis);
|
|
|
}
|
|
|
- log.info("flag:::::::::"+flag);
|
|
|
+ log.info("flag:::::::::" + flag);
|
|
|
return flag;
|
|
|
- } catch (Exception e){
|
|
|
- log.error("异常",e);
|
|
|
- }finally {
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("异常", e);
|
|
|
+ } finally {
|
|
|
|
|
|
FtpUtil.releaseFtpClient(ftpClient);
|
|
|
}
|
|
@@ -293,57 +296,62 @@ public class FtpUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
* 上传文件到FTP
|
|
|
+ *
|
|
|
* @param file
|
|
|
* @param remoteFilePath
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
- public static boolean uploadFile2Ftp(MultipartFile file, String remoteFilePath){
|
|
|
+ public static boolean uploadFile2Ftp(MultipartFile file, String remoteFilePath) {
|
|
|
Assert.notNull(file, "file cannot be null.");
|
|
|
Assert.hasText(remoteFilePath, "remoteFilePath cannot be null or blank.");
|
|
|
FTPClient ftpClient = getFtpClient();
|
|
|
- try{
|
|
|
+ try {
|
|
|
InputStream is = file.getInputStream();
|
|
|
- String fileDir=remoteFilePath.substring(0,remoteFilePath.lastIndexOf("/"));
|
|
|
- mkDir(fileDir,ftpClient);
|
|
|
- boolean b=ftpClient.storeFile(encodingPath(remoteFilePath),is);
|
|
|
- log.info("上传文件结果:{}",b);
|
|
|
+ String fileDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf("/"));
|
|
|
+ mkDir(fileDir, ftpClient);
|
|
|
+ boolean b = ftpClient.storeFile(encodingPath(remoteFilePath), is);
|
|
|
+ log.info("上传文件结果:{}", b);
|
|
|
is.close();
|
|
|
|
|
|
- if(IS_CREATE_TBL){
|
|
|
- BufferedImage image=getImage(file);
|
|
|
- upLoadTbl(ftpClient,image,remoteFilePath);
|
|
|
+ if (IS_CREATE_TBL) {
|
|
|
+ BufferedImage image = getImage(file);
|
|
|
+ upLoadTbl(ftpClient, image, remoteFilePath);
|
|
|
}
|
|
|
-
|
|
|
- return true;
|
|
|
- }finally {
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("", e);
|
|
|
+ return false;
|
|
|
+ } finally {
|
|
|
|
|
|
releaseFtpClient(ftpClient);
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
- public static boolean upLoadTbl(FTPClient ftpClient,BufferedImage image, String remoteFilePath){
|
|
|
- if(image!=null){
|
|
|
- String fileDir=remoteFilePath.substring(0,remoteFilePath.lastIndexOf("/"));
|
|
|
- double v =(double) 64/(double) image.getWidth();
|
|
|
+ public static boolean upLoadTbl(FTPClient ftpClient, BufferedImage image, String remoteFilePath) {
|
|
|
+ if (image != null) {
|
|
|
+ String fileDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf("/"));
|
|
|
+ double v = (double) 64 / (double) image.getWidth();
|
|
|
try {
|
|
|
BufferedImage thumbnail = Thumbnails.of(image)
|
|
|
.scale(v)
|
|
|
.asBufferedImage();
|
|
|
- String fileName=pre_tbl+remoteFilePath.replace(fileDir+"/","");
|
|
|
+ String fileName = pre_tbl + remoteFilePath.replace(fileDir + "/", "");
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
ImageIO.write(thumbnail, "png", os);
|
|
|
InputStream input = new ByteArrayInputStream(os.toByteArray());
|
|
|
- ftpClient.storeFile(encodingPath(fileName),input);
|
|
|
+ ftpClient.storeFile(encodingPath(fileName), input);
|
|
|
input.close();
|
|
|
return true;
|
|
|
} catch (IOException e) {
|
|
|
- log.error("生成缩略图失败:{}",e);
|
|
|
+ log.error("生成缩略图失败:{}", e);
|
|
|
+ return false;
|
|
|
+ } catch (Exception exception) {
|
|
|
+ log.error("", exception);
|
|
|
return false;
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
releaseFtpClient(ftpClient);
|
|
|
}
|
|
|
}
|
|
@@ -351,16 +359,17 @@ public class FtpUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
- * 判断文件为图片
|
|
|
- * @param file
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static BufferedImage getImage(MultipartFile file){
|
|
|
+
|
|
|
+ * 判断文件为图片
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static BufferedImage getImage(MultipartFile file) {
|
|
|
try {
|
|
|
- BufferedImage bufferedImage=ImageIO.read(file.getInputStream());
|
|
|
+ BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
|
|
|
return bufferedImage;
|
|
|
- } catch(IOException ex) {
|
|
|
+ } catch (IOException ex) {
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
@@ -368,22 +377,23 @@ public class FtpUtil {
|
|
|
|
|
|
|
|
|
* 上传文件流到FTP
|
|
|
+ *
|
|
|
* @param inputStream
|
|
|
* @param remoteFilePath
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
- public static boolean uploadFile2Ftp(InputStream inputStream, String remoteFilePath){
|
|
|
+ public static boolean uploadFile2Ftp(InputStream inputStream, String remoteFilePath) {
|
|
|
Assert.hasText(remoteFilePath, "remoteFilePath cannot be null or blank.");
|
|
|
FTPClient ftpClient = getFtpClient();
|
|
|
- try{
|
|
|
- String fileDir=remoteFilePath.substring(0,remoteFilePath.lastIndexOf("/"));
|
|
|
- mkDir(fileDir,ftpClient);
|
|
|
+ try {
|
|
|
+ String fileDir = remoteFilePath.substring(0, remoteFilePath.lastIndexOf("/"));
|
|
|
+ mkDir(fileDir, ftpClient);
|
|
|
|
|
|
- boolean b=ftpClient.storeFile(encodingPath(remoteFilePath),inputStream);
|
|
|
- log.info("上传文件结果:{}",b);
|
|
|
+ boolean b = ftpClient.storeFile(encodingPath(remoteFilePath), inputStream);
|
|
|
+ log.info("上传文件结果:{}", b);
|
|
|
return true;
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
|
|
|
releaseFtpClient(ftpClient);
|
|
|
}
|
|
@@ -391,32 +401,31 @@ public class FtpUtil {
|
|
|
|
|
|
|
|
|
* 上传文件流到FTP
|
|
|
+ *
|
|
|
* @param url
|
|
|
* @param remoteFilePath
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
@SneakyThrows
|
|
|
- public static boolean uploadFile2Ftp(String url, String remoteFilePath){
|
|
|
- byte[] bytes= HttpURL.getContentInputStream(url,null);
|
|
|
- return uploadFile2Ftp(bytes,remoteFilePath);
|
|
|
+ public static boolean uploadFile2Ftp(String url, String remoteFilePath) {
|
|
|
+ byte[] bytes = HttpURL.getContentInputStream(url, null);
|
|
|
+ return uploadFile2Ftp(bytes, remoteFilePath);
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
@SneakyThrows
|
|
|
- public static void downloadFile(String remoteFilePath,HttpServletResponse response){
|
|
|
+ public static void downloadFile(String remoteFilePath, HttpServletResponse response) {
|
|
|
FTPClient ftpClient = getFtpClient();
|
|
|
- if(StringUtil.isNotEmpty(remoteFilePath)){
|
|
|
+ if (StringUtil.isNotEmpty(remoteFilePath)) {
|
|
|
ftpClient.changeWorkingDirectory(remoteFilePath);
|
|
|
- log.info("file success");
|
|
|
+ log.info("file success");
|
|
|
}
|
|
|
|
|
|
String fileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
|
|
|
fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1");
|
|
|
response.setContentType("application/octet-stream");
|
|
|
response.setContentType("application/OCTET-STREAM;charset=UTF-8");
|
|
|
- response.setHeader("Content-Disposition","attachment;filename="+fileName);
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
|
|
|
|
|
|
|
|
|
for (FTPFile file : files) {
|
|
@@ -459,16 +468,16 @@ public class FtpUtil {
|
|
|
}
|
|
|
|
|
|
@SneakyThrows
|
|
|
- public static InputStream getInputStremforFile(String remoteFilePath){
|
|
|
+ public static InputStream getInputStremforFile(String remoteFilePath) {
|
|
|
FTPClient ftpClient = getFtpClient();
|
|
|
|
|
|
|
|
|
InputStream in;
|
|
|
try {
|
|
|
- in = ftpClient.retrieveFileStream(encodingPath(remoteFilePath));
|
|
|
+ in = ftpClient.retrieveFileStream(encodingPath(remoteFilePath));
|
|
|
in.close();
|
|
|
ftpClient.completePendingCommand();
|
|
|
- }finally {
|
|
|
+ } finally {
|
|
|
|
|
|
releaseFtpClient(ftpClient);
|
|
|
}
|
|
@@ -476,14 +485,14 @@ public class FtpUtil {
|
|
|
}
|
|
|
|
|
|
@SneakyThrows
|
|
|
- public static byte[] getByteDataforFile(String remoteFilePath){
|
|
|
- byte[] flieByte=null;
|
|
|
+ public static byte[] getByteDataforFile(String remoteFilePath) {
|
|
|
+ byte[] flieByte = null;
|
|
|
FTPClient ftpClient = getFtpClient();
|
|
|
- if(StringUtil.isNotEmpty(remoteFilePath)){
|
|
|
- try{
|
|
|
+ if (StringUtil.isNotEmpty(remoteFilePath)) {
|
|
|
+ try {
|
|
|
ftpClient.changeWorkingDirectory(remoteFilePath);
|
|
|
- }catch (Exception e){
|
|
|
- log.info("e:",e.getMessage());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("e:", e.getMessage());
|
|
|
|
|
|
releaseFtpClient(ftpClient);
|
|
|
return null;
|
|
@@ -510,6 +519,7 @@ public class FtpUtil {
|
|
|
|
|
|
|
|
|
* 从ftp下载excel文件
|
|
|
+ *
|
|
|
* @param remoteFilePath
|
|
|
* @param response
|
|
|
* @throws IOException
|
|
@@ -542,11 +552,10 @@ public class FtpUtil {
|
|
|
|
|
|
* 选择上传的目录,没有创建目录
|
|
|
*
|
|
|
- * @param ftpPath
|
|
|
- * 需要上传、创建的目录
|
|
|
+ * @param ftpPath 需要上传、创建的目录
|
|
|
* @return
|
|
|
*/
|
|
|
- public static boolean mkDir(String ftpPath,FTPClient ftpClient) {
|
|
|
+ public static boolean mkDir(String ftpPath, FTPClient ftpClient) {
|
|
|
if (!ftpClient.isConnected()) {
|
|
|
return false;
|
|
|
}
|
|
@@ -584,15 +593,16 @@ public class FtpUtil {
|
|
|
|
|
|
|
|
|
* 获取指定路径下FTP文件 并 写入临时文件的outputStream中
|
|
|
+ *
|
|
|
* @param remotePath
|
|
|
* @param outputStream
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- public static Boolean retrieveFile(String remotePath, OutputStream outputStream){
|
|
|
+ public static Boolean retrieveFile(String remotePath, OutputStream outputStream) {
|
|
|
FTPClient ftpClient = getFtpClient();
|
|
|
try {
|
|
|
- return ftpClient.retrieveFile(encodingPath(remotePath),outputStream);
|
|
|
+ return ftpClient.retrieveFile(encodingPath(remotePath), outputStream);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
log.error(e.getMessage());
|
|
@@ -605,12 +615,13 @@ public class FtpUtil {
|
|
|
|
|
|
|
|
|
* 删除文件
|
|
|
+ *
|
|
|
* @param remotePath
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- public static Boolean deleteFile(String remotePath){
|
|
|
- if(StringUtil.isEmpty(remotePath)){
|
|
|
+ public static Boolean deleteFile(String remotePath) {
|
|
|
+ if (StringUtil.isEmpty(remotePath)) {
|
|
|
return false;
|
|
|
}
|
|
|
FTPClient ftpClient = getFtpClient();
|