package zn.ccfccb.util
import hkrt.b2b.view.util.Log
import hkrt.b2b.view.util.ViewUtil
import java.io.ByteArrayOutputStream
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.InputStream
import org.apache.commons.net.ftp.FTPClient
import org.apache.commons.net.ftp.FTPFile
public class CCFCCBFTP {
/**
* 上传文件
*
* @param fileName
* @param plainFilePath 明文文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null
ByteArrayOutputStream bos = null
FTPClient ftpClient = new FTPClient()
String bl = "false"
try {
fis = new FileInputStream(plainFilePath)
bos = new ByteArrayOutputStream(fis.available())
byte[] buffer = new byte[1024]
int count = 0
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count)
}
bos.flush()
Log.info("加密上传文件开始")
Log.info("连接远程上传服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22)
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22)
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD)
//Log.info("连接远程上传服务器"+"192.168.54.106:"+2021)
//ftpClient.connect("192.168.54.106", 2021)
//ftpClient.login("hkrt-CCFCCBHK", "3OLJheziiKnkVcu7Sigz")
FTPFile[] fs
fs = ftpClient.listFiles()
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true"
ftpClient.changeWorkingDirectory("/"+filepath+"")
}
}
Log.info("检查文件路径是否存在:/"+filepath)
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath)
return bl
}
ftpClient.setBufferSize(1024)
ftpClient.setControlEncoding("GBK")
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)
ftpClient.storeFile(fileName, fis)
Log.info("上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/")
return bl
} catch (Exception e) {
throw e
} finally {
if (fis != null) {
try {
fis.close()
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e)
}
}
if (bos != null) {
try {
bos.close()
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e)
}
}
}
}
/**
*下载并解压文件
*
* @param localFilePath
* @param fileName
* @param routeFilepath
* @return
* @throws Exception
*/
public static String fileDownloadByFtp(String localFilePath, String fileName,String routeFilepath) throws Exception {
FileInputStream fis = null
ByteArrayOutputStream bos = null
FileOutputStream fos = null
FTPClient ftpClient = new FTPClient()
String SFP = System.getProperty("file.separator")
String bl = "false"
try {
Log.info("下载并解密文件开始")
Log.info("连接远程下载服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22)
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22)
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD)
//ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021)
//ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD)
FTPFile[] fs
ftpClient.makeDirectory(routeFilepath)
ftpClient.changeWorkingDirectory(routeFilepath)
bl = "false"
fs = ftpClient.listFiles()
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
bl = "true"
Log.info("下载文件开始。")
ftpClient.setBufferSize(1024)
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)
InputStream is = ftpClient.retrieveFileStream(fileName)
bos = new ByteArrayOutputStream(is.available())
byte[] buffer = new byte[1024]
int count = 0
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count)
}
bos.flush()
fos = new FileOutputStream(localFilePath+SFP+fileName)
fos.write(bos.toByteArray())
Log.info("下载文件结束:"+localFilePath)
}
}
Log.info("检查文件是否存:"+fileName+" "+bl)
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon("查询无结果,请稍后再查询。")
return bl
}
return bl
} catch (Exception e) {
throw e
} finally {
if (fis != null) {
try {
fis.close()
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e)
}
}
if (bos != null) {
try {
bos.close()
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e)
}
}
if (fos != null) {
try {
fos.close()
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e)
}
你用的FTPClient引入不对吧,我们项目上都是用的
import org.apache.commons.net.ftp.FTPClient
import org.apache.commons.net.ftp.FTPFile
import org.apache.commons.net.ftp.FTPReply
下面是我们项目上用到的FTP的实现代码(FTP需要先连接,再登录,之后就是校验登录是否成功),具体代码如下:
/*** 获取FTPClient对象
*
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
* @return FTPClient
* @throws Exception
*/
public static FTPClient getFTPClient(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort) throws Exception {
try {
FTPClient ftpClient = new FTPClient()
ftpClient.connect(ftpHost, ftpPort)// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword)// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
logger.error("未连接到FTP,用户名或密码错误!")
ftpClient.disconnect()
return null
} else {
logger.info("FTP连接成功!")
return ftpClient
}
} catch (SocketException socketException) {
logger.error("FTP的IP地址可能错误,请正确配置!")
throw socketException
} catch (IOException ioException) {
logger.error("FTP的端口错误,请正确配置!")
throw ioException
}
}