From:
Today, such a problem some time ago written test procedures, when sending files on Sftp, can be sent to the successful, but could not find the directory, is a small problem, but a headache, after careful testing found by logging in split directory that place a little problem.
Attach Code:
/**
* 将输入流的数据上传到sftp作为文件。文件完整路径=basePath+directory
*
* @param basePath
* 服务器的基础路径
* @param directory
* 上传到该目录
* @param sftpFileName
* sftp端文件名
* @param input
* 输入流 throws SftpException
*/
public boolean upload(String directory, String sftpFileName,
InputStream input) {
boolean ret = false;
try {
// sftp.cd(basePath);
sftp.cd(directory);
ret = true;
} catch (SftpException e) {
Log4jBean.logger.error("对方目录可能不存在[" + directory + "],需要创建目录,异常信息为["
+ e.getMessage() + "]");
// 目录不存在,则创建文件夹
String[] dirs = directory.split("/");
String tempPath = "";
for (String dir : dirs) {
if (null == dir || "".equals(dir))
continue;
tempPath+="/"+dir;
//tempPath = dir;
try {
Log4jBean.logger.info("检测目录[" + tempPath + "]");
sftp.cd(tempPath);
ret = true;
} catch (SftpException ex) {
try {
Log4jBean.logger.error("创建目录[" + tempPath + "]");
sftp.mkdir(tempPath);
sftp.cd(tempPath);
Log4jBean.logger.error("进入目录[" + tempPath + "]");
ret = true;
} catch (SftpException e1) {
Log4jBean.logger.error("创建目录[" + tempPath
+ "]失败1,异常信息[" + e1.getMessage() + "]");
ret = false;
return ret;
} catch (Exception e1) {
Log4jBean.logger.error("创建目录[" + tempPath
+ "]失败2,异常信息[" + e1.getMessage() + "]");
ret = false;
return ret;
}
} catch (Exception e1) {
Log4jBean.logger.error("创建目录[" + tempPath + "]失败3,异常信息["
+ e1.getMessage() + "]");
ret = false;
return ret;
}
}
Log4jBean.logger.info("创建目录完成");
ret = true;
} catch (Exception e1) {
Log4jBean.logger.error("进入目录[" + directory + "]失败,异常信息["
+ e1.getMessage() + "]");
ret = false;
return ret;
}
try {
sftp.put(input, sftpFileName);// 上传文件
ret = true;
} catch (SftpException e) {
Log4jBean.logger.error("Sftp文件处理异常,异常信息为[" + e.getMessage() + "]");
ret = false;
}
return ret;
}
Error place:
When this is not changed before, after checking the directory is empty, exit loop, execute the following program, then the question is, under linux, using a super administrator user is not connected to the Sftp go to send the file, hit when the relative path with a time of cycle paths directly determine each layer, it is easy to cause air to create a successful path, and will put less than the original file location. Because you use the "/" is split up, such as the relative path is file / Grgz / XXX.txt, split finish in front of the file you will find this directory is empty, did not take the lead, but if you add a file in front of / words, not divided as to Oh, because that will cause more directory create a directory from the beginning.
So the best way is to add the absolute path, so the safest, best
Here is the way I get the absolute path (absolute path that can be written according to their actual situation)
Here is a case before the judge directory empty exit changed to continue to the next judge sentenced a catalog of space-time, which would check a level, then it will not leak. This is very important
Renderings:
In this case, it will create a level to check and, very good O (∩_∩) O haha ~.