|
@@ -1,98 +1,144 @@
|
|
package com.steerinfo.ftp.uploadfile.utils;
|
|
package com.steerinfo.ftp.uploadfile.utils;
|
|
|
|
|
|
-import org.apache.commons.net.ftp.FTP;
|
|
|
|
-import org.apache.commons.net.ftp.FTPClient;
|
|
|
|
-import org.apache.commons.net.ftp.FTPConnectionClosedException;
|
|
|
|
-import org.apache.commons.net.ftp.FTPReply;
|
|
|
|
|
|
+import com.steerinfo.framework.utils.io.IOUtils;
|
|
|
|
+import com.steerinfo.framework.utils.text.Charsets;
|
|
|
|
+import org.apache.commons.net.ftp.*;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.io.*;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+import java.util.Base64;
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * POIExcelToHtml 文件转换:
|
|
|
|
+ *
|
|
|
|
+ * @author generator
|
|
|
|
+ * @version 1.0-SNAPSHOT 2021-08-09 18:06
|
|
|
|
+ * 类描述
|
|
|
|
+ * 修订历史:
|
|
|
|
+ * 日期:2021-08-09
|
|
|
|
+ * 作者:shadow
|
|
|
|
+ * 参考:
|
|
|
|
+ * 描述:
|
|
|
|
+ * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
|
|
|
|
+ * @see null
|
|
|
|
+ */
|
|
@Component
|
|
@Component
|
|
public class FtpFileUtil {
|
|
public class FtpFileUtil {
|
|
- //ftp服务器ip地址
|
|
|
|
- @Value("${custom.config.file-server.ip}") // 相关配置放在application.properties 中
|
|
|
|
|
|
+ /**
|
|
|
|
+ * ftp服务器ip地址
|
|
|
|
+ * 相关配置放在application.properties 中
|
|
|
|
+ */
|
|
|
|
+ @Value("${custom.config.file-server.ip}")
|
|
String FTP_ADDRESS;
|
|
String FTP_ADDRESS;
|
|
- //端口号
|
|
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 端口号
|
|
|
|
+ */
|
|
@Value("${custom.config.file-server.port}")
|
|
@Value("${custom.config.file-server.port}")
|
|
Integer FTP_PORT;
|
|
Integer FTP_PORT;
|
|
- //用户名
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 用户名
|
|
|
|
+ */
|
|
@Value("${custom.config.file-ftp-user}")
|
|
@Value("${custom.config.file-ftp-user}")
|
|
String FTP_USERNAME;
|
|
String FTP_USERNAME;
|
|
- //密码
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 密码
|
|
|
|
+ */
|
|
@Value("${custom.config.file-ftp-password}")
|
|
@Value("${custom.config.file-ftp-password}")
|
|
String FTP_PASSWORD;
|
|
String FTP_PASSWORD;
|
|
|
|
|
|
private FTPClient ftpClient = new FTPClient();
|
|
private FTPClient ftpClient = new FTPClient();
|
|
- //上传
|
|
|
|
-// public boolean uploadFile(String filePath, String filename,String basePath, InputStream inputStream) {
|
|
|
|
-// boolean result = false;
|
|
|
|
-// try {
|
|
|
|
-// // 连接FTP服务器
|
|
|
|
-// ftpClient.setControlEncoding("UTF-8");
|
|
|
|
-// ftpClient.enterLocalPassiveMode();
|
|
|
|
-// ftpClient.connect(FTP_ADDRESS, FTP_PORT);
|
|
|
|
-// ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
|
|
|
|
-// int reply;
|
|
|
|
-// reply = ftpClient.getReplyCode();
|
|
|
|
-// if (!FTPReply.isPositiveCompletion(reply)) {
|
|
|
|
-// ftpClient.disconnect();
|
|
|
|
-// return result;
|
|
|
|
-// }
|
|
|
|
-// //切换到上传目录
|
|
|
|
-// boolean changed = ftpClient.changeWorkingDirectory( FTP_BASEPATH+ filePath);
|
|
|
|
-// if (!changed) {
|
|
|
|
-// //如果目录不存在创建目录
|
|
|
|
-// String[] dirs = filePath.split("/");
|
|
|
|
-// String tempPath = basePath;
|
|
|
|
-// for (String dir : dirs) {
|
|
|
|
-// if (null == dir || "".equals(dir)){
|
|
|
|
-// continue;
|
|
|
|
-// }
|
|
|
|
-// tempPath += "/" + dir;
|
|
|
|
-// if (!ftpClient.makeDirectory(tempPath)) {
|
|
|
|
-// return result;
|
|
|
|
-// } else {
|
|
|
|
-// ftpClient.changeWorkingDirectory(tempPath);
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-// System.out.println("当前目录" + ftpClient.printWorkingDirectory());
|
|
|
|
-// //设置为被动模式
|
|
|
|
-// // ftp.enterLocalPassiveMode();
|
|
|
|
-// ftpClient.setControlEncoding("UTF-8");
|
|
|
|
-// //设置上传文件的类型为二进制类型
|
|
|
|
-// ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
|
|
|
|
-// //上传文件
|
|
|
|
-// if (!ftpClient.storeFile(new String(filename.getBytes("UTF-8"), "ISO-8859-1"), inputStream)) {
|
|
|
|
-// System.out.println("上传文件失败");
|
|
|
|
-// return result;
|
|
|
|
-// }
|
|
|
|
-// ftpClient.logout();
|
|
|
|
-// result = true;
|
|
|
|
-// } catch (IOException e) {
|
|
|
|
-// e.printStackTrace();
|
|
|
|
-// } finally {
|
|
|
|
-// if (ftpClient.isConnected()) {
|
|
|
|
-// try {
|
|
|
|
-// inputStream.close();
|
|
|
|
-// ftpClient.disconnect();
|
|
|
|
-// } catch (IOException ioe) {
|
|
|
|
-// ioe.getMessage();
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-// return result;
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
- //上传
|
|
|
|
|
|
+ private static final String SPOT = ".";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 上传文件
|
|
|
|
+ * @param filePath 文件路径
|
|
|
|
+ * @param filename 文件名称
|
|
|
|
+ * @param basePath 上级目录
|
|
|
|
+ * @param inputStream 文件输入流
|
|
|
|
+ * @return 是否成功
|
|
|
|
+ */
|
|
|
|
+ public boolean uploadFile(String filePath, String filename,String basePath, InputStream inputStream) {
|
|
|
|
+ boolean result = false;
|
|
|
|
+ try {
|
|
|
|
+ // 连接FTP服务器
|
|
|
|
+ ftpClient.enterLocalPassiveMode();
|
|
|
|
+ ftpClient.connect(FTP_ADDRESS, FTP_PORT);
|
|
|
|
+ ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
|
|
|
|
+ int reply;
|
|
|
|
+ reply = ftpClient.getReplyCode();
|
|
|
|
+ if (!FTPReply.isPositiveCompletion(reply)) {
|
|
|
|
+ ftpClient.disconnect();
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ //切换到上传目录
|
|
|
|
+ boolean changed = ftpClient.changeWorkingDirectory(filePath);
|
|
|
|
+ if (!changed) {
|
|
|
|
+ //如果目录不存在创建目录
|
|
|
|
+ String[] dirs = filePath.split("/");
|
|
|
|
+ String tempPath = basePath;
|
|
|
|
+ for (String dir : dirs) {
|
|
|
|
+ if (null == dir || "".equals(dir)){
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ tempPath += "/" + dir;
|
|
|
|
+ if (!ftpClient.makeDirectory(tempPath)) {
|
|
|
|
+ return false;
|
|
|
|
+ } else {
|
|
|
|
+ ftpClient.changeWorkingDirectory(tempPath);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ System.out.println("当前目录" + ftpClient.printWorkingDirectory());
|
|
|
|
+ //设置为被动模式
|
|
|
|
+
|
|
|
|
+ //设置上传文件的类型为二进制类型
|
|
|
|
+ ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
|
|
|
|
+ //上传文件
|
|
|
|
+ if (!ftpClient.storeFile(filename, inputStream)) {
|
|
|
|
+ System.out.println("上传文件失败");
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ ftpClient.logout();
|
|
|
|
+ result = true;
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ if (ftpClient.isConnected()) {
|
|
|
|
+ try {
|
|
|
|
+ inputStream.close();
|
|
|
|
+ ftpClient.disconnect();
|
|
|
|
+ } catch (IOException ioe) {
|
|
|
|
+ ioe.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /***
|
|
|
|
+ * 上传
|
|
|
|
+ * @param buffIn 文件流
|
|
|
|
+ * @param filePath 文件路径
|
|
|
|
+ * @param fileName 文件名称
|
|
|
|
+ * @param needDelete 是否删除
|
|
|
|
+ * @return 是否成功
|
|
|
|
+ * @throws FTPConnectionClosedException ftp错误
|
|
|
|
+ * @throws IOException io流错误
|
|
|
|
+ * @throws Exception 普通错误
|
|
|
|
+ */
|
|
public boolean uploadToFtp(InputStream buffIn,String filePath, String fileName, boolean needDelete)
|
|
public boolean uploadToFtp(InputStream buffIn,String filePath, String fileName, boolean needDelete)
|
|
throws FTPConnectionClosedException, IOException, Exception{
|
|
throws FTPConnectionClosedException, IOException, Exception{
|
|
- boolean result = false;
|
|
|
|
|
|
+ boolean result;
|
|
try {
|
|
try {
|
|
//建立连接
|
|
//建立连接
|
|
connectToServer();
|
|
connectToServer();
|
|
|
|
+ ftpClient.changeWorkingDirectory("/");
|
|
setFileType(FTP.BINARY_FILE_TYPE);
|
|
setFileType(FTP.BINARY_FILE_TYPE);
|
|
int reply = ftpClient.getReplyCode();
|
|
int reply = ftpClient.getReplyCode();
|
|
if (!FTPReply.isPositiveCompletion(reply)) {
|
|
if (!FTPReply.isPositiveCompletion(reply)) {
|
|
@@ -100,13 +146,13 @@ public class FtpFileUtil {
|
|
throw new IOException("failed to connect to the FTP Server:" + FTP_ADDRESS);
|
|
throw new IOException("failed to connect to the FTP Server:" + FTP_ADDRESS);
|
|
}
|
|
}
|
|
//进入文件目录
|
|
//进入文件目录
|
|
- boolean changeWork = ftpClient.changeWorkingDirectory( filePath);
|
|
|
|
|
|
+ boolean changeWork = ftpClient.changeWorkingDirectory(filePath);
|
|
if(!changeWork){
|
|
if(!changeWork){
|
|
if(!createDirectory(filePath)){
|
|
if(!createDirectory(filePath)){
|
|
- return result;
|
|
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- result = ftpClient.storeFile(new String(fileName.getBytes("UTF-8"), "ISO-8859-1"),buffIn);
|
|
|
|
|
|
+ result = ftpClient.storeFile(fileName,buffIn);
|
|
if(needDelete){
|
|
if(needDelete){
|
|
ftpClient.deleteFile(fileName);
|
|
ftpClient.deleteFile(fileName);
|
|
}
|
|
}
|
|
@@ -116,12 +162,10 @@ public class FtpFileUtil {
|
|
} else {
|
|
} else {
|
|
System.out.println("uploadToFtp INFO: upload file to ftp : failed!");
|
|
System.out.println("uploadToFtp INFO: upload file to ftp : failed!");
|
|
}
|
|
}
|
|
- buffIn.close();
|
|
|
|
} catch (FTPConnectionClosedException e) {
|
|
} catch (FTPConnectionClosedException e) {
|
|
System.out.println("ftp连接被关闭!");
|
|
System.out.println("ftp连接被关闭!");
|
|
throw e;
|
|
throw e;
|
|
}catch (Exception e){
|
|
}catch (Exception e){
|
|
- result = false;
|
|
|
|
System.out.println("ERR : upload file to ftp : failed! ");
|
|
System.out.println("ERR : upload file to ftp : failed! ");
|
|
throw e;
|
|
throw e;
|
|
} finally {
|
|
} finally {
|
|
@@ -139,10 +183,16 @@ public class FtpFileUtil {
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
- //下载
|
|
|
|
- public InputStream downloadFile(String filename)
|
|
|
|
- throws IOException {
|
|
|
|
- InputStream inputStream = null;
|
|
|
|
|
|
+ /***
|
|
|
|
+ * 预览
|
|
|
|
+ * @param fileName 文件名
|
|
|
|
+ * @param filePath 文件路径
|
|
|
|
+ * @return 返回HTML
|
|
|
|
+ * @throws IOException io流错误
|
|
|
|
+ */
|
|
|
|
+ public String downloadFile(String fileName, String filePath) throws IOException {
|
|
|
|
+ InputStream inputStream;
|
|
|
|
+ String data = fileName + "预览失败";
|
|
try {
|
|
try {
|
|
connectToServer();
|
|
connectToServer();
|
|
ftpClient.enterLocalPassiveMode();
|
|
ftpClient.enterLocalPassiveMode();
|
|
@@ -153,31 +203,197 @@ public class FtpFileUtil {
|
|
ftpClient.disconnect();
|
|
ftpClient.disconnect();
|
|
throw new IOException("failed to connect to the FTP Server:" + FTP_ADDRESS);
|
|
throw new IOException("failed to connect to the FTP Server:" + FTP_ADDRESS);
|
|
}
|
|
}
|
|
- ftpClient.changeWorkingDirectory("/ems/file/2021/07/27");
|
|
|
|
- File file = new File("D:\\LRL"+File.separatorChar+filename);
|
|
|
|
- OutputStream os = new FileOutputStream(file);
|
|
|
|
- boolean b = ftpClient.retrieveFile(filename, os);
|
|
|
|
- if(b == true){
|
|
|
|
- os.close();
|
|
|
|
- closeConnect();
|
|
|
|
|
|
+
|
|
|
|
+ String directory = filePath.substring(0,filePath.lastIndexOf("/")+1);
|
|
|
|
+ // 进入文件所在目录,注意编码格式,以能够正确识别中文目录
|
|
|
|
+ boolean cdStatus = ftpClient.changeWorkingDirectory(directory);
|
|
|
|
+ if (!cdStatus) {
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+ String file = filePath.substring(filePath.lastIndexOf("/")+1);
|
|
|
|
+ // 检验文件是否存在
|
|
|
|
+ inputStream =ftpClient.retrieveFileStream(file);
|
|
|
|
+ String suffixName;
|
|
|
|
+ if(inputStream != null){
|
|
|
|
+ if (fileName != null && fileName.contains(SPOT)) {
|
|
|
|
+ POIWordToHtml poiWordToHtml = new POIWordToHtml();
|
|
|
|
+ suffixName = fileName.substring(fileName.indexOf(".")+1);
|
|
|
|
+ switch (suffixName) {
|
|
|
|
+ case "docx": data = poiWordToHtml.docxToHtml(inputStream);break;
|
|
|
|
+ case "doc" : data = poiWordToHtml.docToHtml(inputStream); break;
|
|
|
|
+ case "xlsx":
|
|
|
|
+ case "xls" :
|
|
|
|
+ data = POIExcelToHtml.excelToHtml(inputStream);break;
|
|
|
|
+ case "pptx":
|
|
|
|
+ case "ppt" :
|
|
|
|
+ data = POIPptToHtml.pptToHtml(inputStream,suffixName);break;
|
|
|
|
+ case "jpg" :
|
|
|
|
+ case "gif" :
|
|
|
|
+ case "png" :
|
|
|
|
+ case "jpeg":
|
|
|
|
+ case "jpe" :
|
|
|
|
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
|
|
|
+ IOUtils.copy(inputStream,stream);
|
|
|
|
+ String imgStr = Base64.getEncoder().encodeToString(stream.toByteArray());
|
|
|
|
+ data = "<img src=" + "\"data:image/"+suffixName+";base64," + imgStr + "\"" + "/>";
|
|
|
|
+ stream.close();break;
|
|
|
|
+ case "txt" :
|
|
|
|
+ ByteArrayOutputStream txtStream = new ByteArrayOutputStream();
|
|
|
|
+ IOUtils.copy(inputStream,txtStream);
|
|
|
|
+ data = txtStream.toString();break;
|
|
|
|
+ default : data = fileName + "文件暂时不支持预览。"; break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ inputStream.close();
|
|
|
|
+ ftpClient.completePendingCommand();
|
|
}
|
|
}
|
|
} catch (FTPConnectionClosedException e){
|
|
} catch (FTPConnectionClosedException e){
|
|
System.out.println("ftp连接被关闭!");
|
|
System.out.println("ftp连接被关闭!");
|
|
throw e;
|
|
throw e;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- System.out.println("ERR : upload file " + filename + " from ftp : failed!");
|
|
|
|
|
|
+ System.out.println("ERR : upload file " + fileName + " from ftp : failed!"+e.getMessage());
|
|
|
|
+ } finally {
|
|
|
|
+ if(ftpClient.isConnected()){
|
|
|
|
+ closeConnect();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 下载
|
|
|
|
+ * @param response 响应头
|
|
|
|
+ * @param fileName 文件名
|
|
|
|
+ * @param filePath 文件路径
|
|
|
|
+ * @return 返回下载是否成功
|
|
|
|
+ */
|
|
|
|
+ public boolean download(HttpServletResponse response, String fileName, String filePath) {
|
|
|
|
+ boolean flag = false;
|
|
|
|
+ try{
|
|
|
|
+ connectToServer();
|
|
|
|
+ ftpClient.enterLocalPassiveMode();
|
|
|
|
+ // 设置传输二进制文件
|
|
|
|
+ setFileType(FTP.BINARY_FILE_TYPE);
|
|
|
|
+ int reply = ftpClient.getReplyCode();
|
|
|
|
+ if(!FTPReply.isPositiveCompletion(reply)){
|
|
|
|
+ ftpClient.disconnect();
|
|
|
|
+ throw new IOException("failed to connect to the FTP Server:" + FTP_ADDRESS);
|
|
|
|
+ }
|
|
|
|
+ String transferName = new String(fileName.getBytes(Charsets.UTF_8),Charsets.ISO_8859_1);
|
|
|
|
+ String directory = filePath.substring(0,filePath.lastIndexOf("/")+1);
|
|
|
|
+ String file = filePath.substring(filePath.lastIndexOf("/")+1);
|
|
|
|
+ // 进入文件所在目录,注意编码格式,以能够正确识别中文目录
|
|
|
|
+ ftpClient.changeWorkingDirectory(directory);
|
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
|
+ response.setContentType("multipart/form-data;charset=UTF-8");
|
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=\"" + transferName + "\"");
|
|
|
|
+ FTPFile[] fs = ftpClient.listFiles();
|
|
|
|
+ for (FTPFile ff : fs) {
|
|
|
|
+ if (ff.getName().equals(file)) {
|
|
|
|
+ OutputStream os = response.getOutputStream();
|
|
|
|
+ flag = ftpClient.retrieveFile(ff.getName(), os);
|
|
|
|
+ os.flush();
|
|
|
|
+ os.close();
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ closeConnect();
|
|
|
|
+
|
|
|
|
+ }catch (FTPConnectionClosedException ignored){
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return flag;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 下载返回流
|
|
|
|
+ * @param fileName 文件名称
|
|
|
|
+ * @param filePath 文件路径
|
|
|
|
+ * @return 返回是否成功
|
|
|
|
+ */
|
|
|
|
+ public ByteArrayOutputStream download(String fileName, String filePath) {
|
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
|
+ try{
|
|
|
|
+ connectToServer();
|
|
|
|
+ ftpClient.enterLocalPassiveMode();
|
|
|
|
+ // 设置传输二进制文件
|
|
|
|
+ setFileType(FTP.BINARY_FILE_TYPE);
|
|
|
|
+ int reply = ftpClient.getReplyCode();
|
|
|
|
+ if(!FTPReply.isPositiveCompletion(reply)){
|
|
|
|
+ ftpClient.disconnect();
|
|
|
|
+ throw new IOException("failed to connect to the FTP Server:" + FTP_ADDRESS);
|
|
|
|
+ }
|
|
|
|
+ String directory = filePath.substring(0,filePath.lastIndexOf("/")+1);
|
|
|
|
+ String file = filePath.substring(filePath.lastIndexOf("/")+1);
|
|
|
|
+ // 进入文件所在目录,注意编码格式,以能够正确识别中文目录
|
|
|
|
+ ftpClient.changeWorkingDirectory(directory);
|
|
|
|
+ FTPFile[] fs = ftpClient.listFiles();
|
|
|
|
+ for (FTPFile ff : fs) {
|
|
|
|
+ if (ff.getName().equals(file)) {
|
|
|
|
+ os.close();
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ closeConnect();
|
|
|
|
+
|
|
|
|
+ }catch (FTPConnectionClosedException ignored){
|
|
|
|
+
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return os;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public boolean deleteFile(String filePath) throws Exception {
|
|
|
|
+ boolean result;
|
|
|
|
+ try {
|
|
|
|
+ //建立连接
|
|
|
|
+ connectToServer();
|
|
|
|
+ ftpClient.changeWorkingDirectory("/");
|
|
|
|
+ setFileType(FTP.BINARY_FILE_TYPE);
|
|
|
|
+ int reply = ftpClient.getReplyCode();
|
|
|
|
+ if (!FTPReply.isPositiveCompletion(reply)) {
|
|
|
|
+ ftpClient.disconnect();
|
|
|
|
+ throw new IOException("failed to connect to the FTP Server:" + FTP_ADDRESS);
|
|
|
|
+ }
|
|
|
|
+ String directory = filePath.substring(0,filePath.lastIndexOf("/")+1);
|
|
|
|
+ String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
|
|
|
|
+ //进入文件目录
|
|
|
|
+ boolean changeWork = ftpClient.changeWorkingDirectory(directory);
|
|
|
|
+ if(!changeWork){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ result = ftpClient.deleteFile(fileName);
|
|
|
|
+ } catch (FTPConnectionClosedException e) {
|
|
|
|
+ System.out.println("ftp连接被关闭!");
|
|
|
|
+ throw e;
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ System.out.println("ERR : delete file to ftp : failed! ");
|
|
|
|
+ throw e;
|
|
|
|
+ } finally {
|
|
|
|
+ if(ftpClient.isConnected()){
|
|
|
|
+ closeConnect();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- return inputStream;
|
|
|
|
|
|
+ return result;
|
|
}
|
|
}
|
|
|
|
|
|
- //建立连接
|
|
|
|
|
|
+ /***
|
|
|
|
+ * 建立连接
|
|
|
|
+ * @throws FTPConnectionClosedException ftp错误
|
|
|
|
+ * @throws Exception 普通错误
|
|
|
|
+ */
|
|
private void connectToServer() throws FTPConnectionClosedException, Exception {
|
|
private void connectToServer() throws FTPConnectionClosedException, Exception {
|
|
if(!ftpClient.isConnected()){
|
|
if(!ftpClient.isConnected()){
|
|
int reply;
|
|
int reply;
|
|
try {
|
|
try {
|
|
//建立连接
|
|
//建立连接
|
|
ftpClient = new FTPClient();
|
|
ftpClient = new FTPClient();
|
|
- ftpClient.setControlEncoding("UTF-8");
|
|
|
|
|
|
+ ftpClient.setControlEncoding("GBK");
|
|
ftpClient.enterLocalPassiveMode();
|
|
ftpClient.enterLocalPassiveMode();
|
|
ftpClient.connect(FTP_ADDRESS, FTP_PORT);
|
|
ftpClient.connect(FTP_ADDRESS, FTP_PORT);
|
|
ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
|
|
ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
|
|
@@ -196,7 +412,10 @@ public class FtpFileUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- //设置传输文件类型
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 设置传输文件类型
|
|
|
|
+ * @param fileType 文件类型
|
|
|
|
+ */
|
|
private void setFileType(int fileType) {
|
|
private void setFileType(int fileType) {
|
|
try {
|
|
try {
|
|
ftpClient.setFileType(fileType);
|
|
ftpClient.setFileType(fileType);
|
|
@@ -206,7 +425,9 @@ public class FtpFileUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- //关闭连接
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 关闭连接
|
|
|
|
+ */
|
|
public void closeConnect() {
|
|
public void closeConnect() {
|
|
try {
|
|
try {
|
|
if (ftpClient != null) {
|
|
if (ftpClient != null) {
|
|
@@ -218,10 +439,12 @@ public class FtpFileUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- //创建文件夹
|
|
|
|
- public boolean createDirectory(String directory) throws IOException {
|
|
|
|
- boolean isSuccess = false;
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 创建文件夹
|
|
|
|
+ */
|
|
|
|
+ public boolean createDirectory(String directory) {
|
|
try {
|
|
try {
|
|
|
|
+ ftpClient.changeWorkingDirectory("/");
|
|
String[] dirs = directory.split("/");
|
|
String[] dirs = directory.split("/");
|
|
String tempPath = "";
|
|
String tempPath = "";
|
|
for (String dir : dirs) {
|
|
for (String dir : dirs) {
|
|
@@ -230,7 +453,9 @@ public class FtpFileUtil {
|
|
}
|
|
}
|
|
tempPath += "/" + dir;
|
|
tempPath += "/" + dir;
|
|
if (!ftpClient.makeDirectory(tempPath)) {
|
|
if (!ftpClient.makeDirectory(tempPath)) {
|
|
- return isSuccess;
|
|
|
|
|
|
+ if(!ftpClient.changeWorkingDirectory(tempPath)){
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
ftpClient.changeWorkingDirectory(tempPath);
|
|
ftpClient.changeWorkingDirectory(tempPath);
|
|
}
|
|
}
|
|
@@ -240,8 +465,4 @@ public class FtpFileUtil {
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
- //获取类型地址
|
|
|
|
-// public String getFtpaddress (String type) {
|
|
|
|
-// if(){}
|
|
|
|
-// }
|
|
|
|
}
|
|
}
|