zlb

VFS 上传文件到sftp 报错 包含中文路径 或者中文文件名称

之前用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);
	}
}

posted on 2018-11-16 10:31  zlb  阅读(1764)  评论(0编辑  收藏  举报

导航