之前用Apache commons-vfs工具進行ftp操作(FTP服務器是 FileZilla Server)
上傳本地文件 到 ftp服務器上,如果文件名稱 包含 中文 報錯
org.apache.commons.vfs2.FileSystemException: Could not put FTP file to “e:\紅黃藍股價暴跌.docx” to
sftp://dsideal:***@192.168.1.168/紅黃藍股價暴跌.docx
1、有可能是 登錄FTP用戶名沒有權限 創建文件 需要登錄用戶有創建文件的權限
2、需要 修改編碼 從utf-8 到 ISO-8859-1
private static FileSystemManager fsManager = null;
static {
try {
fsManager = VFS.getManager();
} catch (Exception e) {
e.printStackTrace();
}
}
//需要 ftp 服務器用戶有 添加,修改 刪除文件的權限
public void upfile(File file) {
try {
initConfig();
String inFilePath=ftpPathFull;
// Create local file object
FileObject localFile = fsManager.resolveFile(file.getAbsolutePath());
// Create remote file object
String sftpUri= inFilePath+file.getName();
String sftpUriiso= new String(sftpUri.getBytes("UTF-8"),"ISO-8859-1");
FileObject remoteFile = fsManager.resolveFile(sftpUriiso);
// 如果文件不存在,則創建文件
if (!remoteFile.exists()) {
remoteFile.createFile();
}
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
logger.info("上傳文件成功:"+file.getName());
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage());
logger.error(e.getLocalizedMessage(),e);
}
}