(java) java ftp上传文件

用 java 上传 ftp 文件如果是在 window 环境执行,就可以上传成功,但将同样的代码部署到 linux 上传就不成功,但也没有报错

boolean b = ftp.storeFilenew Stringfile2.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.getLogFtpClientUtil.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	" + path;
// LOG.info"password	" + password;
// LOG.info"server	" + server;
// LOG.info"port	" + port;
// LOG.info"path	" + path; connectpath, server, Integer.parseIntport, username, password; } catch Exception e { e.printStackTrace; } } /** * * @param path 上传到ftp服务器哪个路径下 * @param addr 地址 * @param port 端口号 * @param username 用户名 * @param password 密码 * @return * @throws Exception */ public static boolean connectString path,String addr,int port,String username,String password throws Exception { boolean result = false; ftp = new FTPClient; int reply; ftp.connectaddr,port; ftp.loginusername,password; ftp.setFileTypeFTPClient.BINARY_FILE_TYPE; reply = ftp.getReplyCode;
// ftp.enterLocalPassiveMode; if !FTPReply.isPositiveCompletionreply { ftp.disconnect; LOG.info"-------------------------1"; return result; } LOG.info"-------------------------2"; ftp.changeWorkingDirectorypath; result = true; return result; } /** * * @param file 上传的文件或文件夹 * @throws Exception */ public static void uploadFile file throws Exception{ iffile.isDirectory{ ftp.makeDirectoryfile.getName; ftp.changeWorkingDirectoryfile.getName; String[] files = file.list; for int i = 0; i < files.length; i++ { File file1 = new Filefile.getPath+"\"+files[i] ; iffile1.isDirectory{ uploadfile1; ftp.changeToParentDirectory; }else{ File file2 = new Filefile.getPath+"\"+files[i]; FileInputStream input = new FileInputStreamfile2; ftp.storeFilefile2.getName, input; input.close; } } }else{
// LOG.infofile.getPath; File file2 = new Filefile.getPath; FileInputStream input = new FileInputStreamfile2; LOG.infoinput; ftp.setControlEncoding"UTF-8"; boolean b = ftp.storeFilenew Stringfile2.getName.getBytes"UTF-8","iso-8859-1", input;
// boolean b = ftp.storeFilefile.getPath, input; ftp.enterLocalPassiveMode; LOG.info"storeFile:	" + b; input.close; } } public static void mainString[] args throws Exception{ FtpClientUtil.uploadnew File"C:/Users/Jilinwula/Desktop/bing/update_wfportal_source_20160604.zip"; }
}

注意路径分隔符,windows和linux下不一致,会导致文件找不到!

发表评论

电子邮件地址不会被公开。 必填项已用*标注