| public boolean upLoadFile(File file, String path, String fileName) throws IOException { boolean result = false;
 FTPClient ftpClient = new FTPClient();
 try {
 ftpClient.connect("www.myftp.com",21);  //指定FTP的IP地址或域名,以及端口
 ftpClient.login("username", "password");  //指定登录FTP服务器的用户名和口令
 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
 
 // 创建多级目录
 if (path != null && !"".equals(path.trim())) {
 String[] pathes = path.split("/");
 for (String onepath : pathes) {
 if (onepath == null || "".equals(onepath.trim())) {
 continue;
 }
 
 onepath=new String(onepath.getBytes("GBK"),"iso-8859-1");
 if (!ftpClient.changeWorkingDirectory(onepath)) {
 ftpClient.makeDirectory(onepath); //创建目录
 ftpClient.changeWorkingDirectory(onepath);  //转到当前工作目录
 }
 }
 }
 
 //在当前工作目录中上传文件
 result = ftpClient.storeFile(new String(fileName.getBytes("GBK"),"iso-8859-1"), new FileInputStream(file));
 } catch (Exception e) {
 e.printStackTrace();
 } finally {
 ftpClient.logout();
 }
 return result;
 }
 |