标签:FTP String encoding 上传下载 文件 reply new ftpClient
/** * 从FTP下载文件 */ public static boolean downFileFroFTP(String url, int port, String username, String password, String remotePath, String fileName, String localPath) { FTPClient ftpClient = new FTPClient(); String encoding = System.getProperty("file.encoding"); boolean result = false; try { int reply; ftpClient.setControlEncoding(encoding); ftpClient.connect(url, port); // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 ftpClient.login(username, password);// 登录 // 设置文件传输类型为二进制 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // 获取ftp登录应答代码 reply = ftpClient.getReplyCode(); // 验证是否登陆成功 if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); System.err.println("FTP server refused connection."); return result; } System.out.println(remotePath); ftpClient.sendCommand("OPTS UTF8", "ON"); // 转移到FTP服务器目录至指定的目录下 ftpClient.changeWorkingDirectory(new String(remotePath .getBytes(encoding), "UTF-8")); // 获取文件列表 FTPFile[] fs = ftpClient.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName)) { File localFile = new File(localPath + "/" + ff.getName()); System.out.println("文件名是:"+localFile.getName()); OutputStream is = new FileOutputStream(localFile); ftpClient.retrieveFile(ff.getName(), is); is.close(); } } ftpClient.logout(); result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException ioe) { } } } return result; } /** * Description: 向FTP服务器上传文件 * */ public static boolean uploadFile(String url, int port, String username, String password, String path, String filename, InputStream input) { FTPClient ftpClient = new FTPClient(); String encoding = System.getProperty("file.encoding"); boolean result = false; try { int reply; // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 ftpClient.connect(url); // 登录 ftpClient.login(username, password); ftpClient.setControlEncoding(encoding); // 检验是否连接成功 reply = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { System.out.println("连接失败"); ftpClient.disconnect(); return result; } // 转移工作目录至指定目录不成功,创建该目录 if(!ftpClient.changeWorkingDirectory(path)){ ftpClient.makeDirectory(path); } ftpClient.changeWorkingDirectory(path); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.sendCommand("OPTS UTF8", "ON"); String filenameNew = new String(filename.getBytes(encoding),"ISO-8859-1"); // 获取文件列表 FTPFile[] fs = ftpClient.listFiles(); int len=fs.length; String[] fnArr=new String[len]; for (int i=0;i<len;i++){ fnArr[i]=fs[i].getName(); } boolean isContain=useList(fnArr,filename); if (isContain) { System.out.println("该文件已经存在!!"); }else{ result = ftpClient.storeFile(filenameNew, input); } input.close(); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException ioe) { ioe.printStackTrace(); } } } return result; } public static boolean useList(String[] arr,String targetValue){ return Arrays.asList(arr).contains(targetValue); }
downFileFroFTP("192.168.x.xxx", 21, "xxxxxx", "xxxxx", path,filename, savePath);
flag = uploadFile("192.168.x.xxx", 21, "xxxxxx", "xxxxx", path, filenameNew, input)
标签:FTP,String,encoding,上传下载,文件,reply,new,ftpClient 来源: https://www.cnblogs.com/xidianlxf/p/11250290.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。