"
用 java 上传 ftp 文件如果是在 window 环境执行,就可以上传成功,但将同样的代码部署到 linux 上传就不成功,但也没有报错
boolean b = ftp.storeFile(new String(file2.getName().getBytes("UTF-8"),"iso-8859-1"), input);
windows 执行的时候 b = true
上传成功
linux 同样的代码 执行时 b = false
下面是完整的代码 ,这和操作系统有关系吗?
import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FtpClientUtil {
private static FTPClient ftp;
private static final Log LOG = LogFactory.getLog(FtpClientUtil.class);
static {
String username = AppConfig.get("ftp.information.username").toString();
String password = AppConfig.get("ftp.information.password").toString();
String server = AppConfig.get("ftp.information.server").toString();
String port = AppConfig.get("ftp.information.port").toString();
String path = AppConfig.get("ftp.information.path").toString();
try {
// LOG.info("path\t" + path);
// LOG.info("password\t" + password);
// LOG.info("server\t" + server);
// LOG.info("port\t" + port);
// LOG.info("path\t" + path);
connect(path, server, Integer.parseInt(port), username, password);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param path 上传到ftp服务器哪个路径下
* @param addr 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
*/
public static boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftp.connect(addr,port);
ftp.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
// ftp.enterLocalPassiveMode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
LOG.info("-------------------------1");
return result;
}
LOG.info("-------------------------2");
ftp.changeWorkingDirectory(path);
result = true;
return result;
}
/**
*
* @param file 上传的文件或文件夹
* @throws Exception
*/
public static void upload(File file) throws Exception{
if(file.isDirectory()){
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
}else{
// LOG.info(file.getPath());
File file2 = new File(file.getPath());
FileInputStream input = new FileInputStream(file2);
LOG.info(input);
ftp.setControlEncoding("UTF-8");
boolean b = ftp.storeFile(new String(file2.getName().getBytes("UTF-8"),"iso-8859-1"), input);
// boolean b = ftp.storeFile(file.getPath(), input);
ftp.enterLocalPassiveMode();
LOG.info("storeFile:\t" + b);
input.close();
}
}
public static void main(String[] args) throws Exception{
FtpClientUtil.upload(new File("C:/Users/Jilinwula/Desktop/bing/update_wfportal_source_20160604.zip"));
}
}
" ![image.png](https://ucc.alicdn.com/pic/developer-ecology/a2a1a078b5dc41cb9f973cc62ec2ffb6.png)
"
注意路径分隔符,windows和linux下不一致,会导致文件找不到!
######尝试调整下代码顺序
ftp.enterLocalPassiveMode(); boolean b = ftp.storeFile(new String(file2.getName().getBytes("UTF-8"),"iso-8859-1"), input);
" ![image.png](https://ucc.alicdn.com/pic/developer-ecology/8b6c62b249284e49afe26817abe7c5d9.png)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。