本文整理汇总了Java中org.apache.commons.net.ftp.FTPClient.setControlEncoding方法的典型用法代码示例。如果您正苦于以下问题:Java FTPClient.setControlEncoding方法的具体用法?Java FTPClient.setControlEncoding怎么用?Java FTPClient.setControlEncoding使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.net.ftp.FTPClient
的用法示例。
在下文中一共展示了FTPClient.setControlEncoding方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: download
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
public static byte[] download(String url, int port, String username, String password, String remotePath,
String fileName) throws IOException {
FTPClient ftp = new FTPClient();
ftp.setConnectTimeout(5000);
ftp.setAutodetectUTF8(true);
ftp.setCharset(CharsetUtil.UTF_8);
ftp.setControlEncoding(CharsetUtil.UTF_8.name());
try {
ftp.connect(url, port);
ftp.login(username, password);// 登录
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
throw new IOException("login fail!");
}
ftp.changeWorkingDirectory(remotePath);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
try (ByteArrayOutputStream is = new ByteArrayOutputStream();) {
ftp.retrieveFile(ff.getName(), is);
byte[] result = is.toByteArray();
return result;
}
}
}
ftp.logout();
} finally {
if (ftp.isConnected()) {
ftp.disconnect();
}
}
return null;
}
示例2: initFtpClient
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
private FTPClient initFtpClient(String remoteDir) throws IOException {
FTPClient ftp = new FTPClient();
// 设置连接超时时间
ftp.setConnectTimeout(CONNECT_TIMEOUT);
// 设置传输文件名编码方式
ftp.setControlEncoding(CONTROL_ENCODING);
ftp.connect(host, ftpPort);
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
logger.debug("无法连接FTP");
return null;
}
boolean loginRet = ftp.login(ftpUsername, ftpPassword);
if (!loginRet) {
logger.debug("FTP登录失败");
return null;
}
// 进入被动模式
ftp.enterLocalPassiveMode();
boolean changeDirResult = MKDAndCWD(ftp, remoteDir);
if (!changeDirResult) {
logger.debug("创建/切换文件夹失败");
return null;
}
// 传输二进制文件
ftp.setFileType(FTP.BINARY_FILE_TYPE);
return ftp;
}
示例3: connectFTPServer
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 连接FTP服务器
*
* @author wangwei
*/
public FTPClient connectFTPServer() throws Exception {
ftpClient = new FTPClient();
try {
ftpClient.configure(getFTPClientConfig());
ftpClient.connect(ftpConstant.getHost(), ftpConstant.getPort());
ftpClient.login(ftpConstant.getUsername(), ftpConstant.getPassword());
// 设置以二进制方式传输
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置被动模式
ftpClient.enterLocalPassiveMode();
ftpClient.setControlEncoding("GBK");
// 响应信息
int replyCode = ftpClient.getReplyCode();
if ((!FTPReply.isPositiveCompletion(replyCode))) {
// 关闭Ftp连接
closeFTPClient();
// 释放空间
ftpClient = null;
throw new Exception("登录FTP服务器失败,请检查!");
} else {
return ftpClient;
}
} catch (Exception e) {
ftpClient.disconnect();
ftpClient = null;
throw e;
}
}
示例4: getFTPClient
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
protected FTPClient getFTPClient() {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.setConnectTimeout(connectTimeout);
ftpClient.connect(hostname);
ftpClient.login(userName, passWord);
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
FtpUtils.disconnect(ftpClient);
log.warn("FTP登陆失败,账号或者密码有误");
} else {
ftpClient.setSoTimeout(soTimeout);
/* 设置缓冲区大小 */
ftpClient.setBufferSize(bufferSize);
/* 设置服务器编码 */
ftpClient.setControlEncoding(encoding);
/* 设置以二进制方式传输 */
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
/* 设置服务器目录 */
// ftpClient.changeWorkingDirectory(directory);
ftpClient.enterLocalPassiveMode();
log.debug("成功连接并登录FTP服务器。。。");
}
} catch (Exception e) {
FtpUtils.disconnect(ftpClient);
log.error("error", e);
}
return ftpClient;
}
示例5: upload
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 上传文件到FTP服务器,支持断点续传
*
* @param localFile 本地文件
* @param remoteFilePath 远程文件路径,使用/home/directory1/subdirectory/file.ext
* 按照Linux上的路径指定方式,支持多级目录嵌套,支持递归创建不存在的目录结构
* @return 上传结果
* @throws IOException
*/
public UploadStatus upload(FTPClient ftpClient, File localFile, String remoteFilePath) throws IOException {
// 设置PassiveMode传输
ftpClient.enterLocalPassiveMode();
// 设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding(DEAFULT_REMOTE_CHARSET);
UploadStatus result;
// 对远程目录的处理
String remoteFileName = remoteFilePath;
if (remoteFilePath.contains("/")) {
remoteFileName = remoteFilePath.substring(remoteFilePath.lastIndexOf("/") + 1);
// 创建服务器远程目录结构,创建失败直接返回
if (createDirecroty(remoteFilePath, ftpClient) == UploadStatus.Create_Directory_Fail) {
return UploadStatus.Create_Directory_Fail;
}
}
// 检查远程是否存在文件
FTPFile[] files = ftpClient.listFiles(new String(remoteFileName
.getBytes(DEAFULT_REMOTE_CHARSET), DEAFULT_LOCAL_CHARSET));
if (files.length == 1) {
long remoteSize = files[0].getSize();
// File f = new File(localFilePath)
long localSize = localFile.length();
if (remoteSize == localSize) { // 文件存在
return UploadStatus.File_Exits;
} else if (remoteSize > localSize) {
return UploadStatus.Remote_Bigger_Local;
}
// 尝试移动文件内读取指针,实现断点续传
result = uploadFile(remoteFileName, localFile, ftpClient, remoteSize);
// 如果断点续传没有成功,则删除服务器上文件,重新上传
if (result == UploadStatus.Upload_From_Break_Failed) {
if (!ftpClient.deleteFile(remoteFileName)) {
return UploadStatus.Delete_Remote_Faild;
}
result = uploadFile(remoteFileName, localFile, ftpClient, 0);
}
} else {
result = uploadFile(remoteFileName, localFile, ftpClient, 0);
}
return result;
}
示例6: execute
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 执行FTP回调操作的方法
*
* @param <T> the type parameter
* @param callback 回调的函数
* @return the t
* @throws IOException the io exception
*/
public <T> T execute(FTPClientCallback<T> callback) throws IOException {
FTPClient ftp = new FTPClient();
try {
//登录FTP服务器
try {
//设置超时时间
ftp.setDataTimeout(7200);
//设置默认编码
ftp.setControlEncoding(DEAFULT_REMOTE_CHARSET);
//设置默认端口
ftp.setDefaultPort(DEAFULT_REMOTE_PORT);
//设置是否显示隐藏文件
ftp.setListHiddenFiles(false);
//连接ftp服务器
if (StringUtils.isNotEmpty(port) && NumberUtils.isDigits(port)) {
ftp.connect(host, Integer.valueOf(port));
} else {
ftp.connect(host);
}
} catch (ConnectException e) {
logger.error("连接FTP服务器失败:" + ftp.getReplyString() + ftp.getReplyCode());
throw new IOException("Problem connecting the FTP-server fail", e);
}
//得到连接的返回编码
int reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
//登录失败权限验证失败
if (!ftp.login(getUsername(), getPassword())) {
ftp.quit();
ftp.disconnect();
logger.error("连接FTP服务器用户或者密码失败::" + ftp.getReplyString());
throw new IOException("Cant Authentificate to FTP-Server");
}
if (logger.isDebugEnabled()) {
logger.info("成功登录FTP服务器:" + host + " 端口:" + port);
}
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//回调FTP的操作
return callback.doTransfer(ftp);
} finally {
//FTP退出
ftp.logout();
//断开FTP连接
if (ftp.isConnected()) {
ftp.disconnect();
}
}
}
示例7: connect
import org.apache.commons.net.ftp.FTPClient; //导入方法依赖的package包/类
/**
* 连接到FTP服务器
*
* @param hostname 主机名
* @param port 端口
* @param username 用户名
* @param password 密码
* @return 是否连接成功
* @throws IOException
*/
public boolean connect(FTPClient ftpClient, String hostname, int port, String username, String password) throws IOException {
ftpClient.connect(hostname, port);
ftpClient.setControlEncoding(DEAFULT_REMOTE_CHARSET);
if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()) && ftpClient.login(username, password)) {
return true;
}
disconnect(ftpClient);
return false;
}