本文由动哒公众号(tongmi5g),QQ群(58621899)提供,欢迎关注获取技术支持,有任何问题群里都会回复。
在开发的本地电脑向服务器上传文件没问题,但是,部署之后,在服务器之间上传文件,ftp连接没问题,但是上传死活不成功,ftpClient.storeFile一直返回失败。
原因:
网上找资料原因可能是防火墙策略阻止了java测试服务的端口连接,FTP服务器使用的有可能是被动模式;
注意:
FTP的PORT(主动模式)和PASV(被动模式)
(1) PORT(主动模式)
PORT中文称为主动模式,工作的原理: FTP客户端连接到FTP服务器的21端口,发送用户名和密码登录,登录成功后要list列表或者读取数据时,客户端随机开放一个端口(1024以上),发送 PORT命令到FTP服务器,告诉服务器客户端采用主动模式并开放端口;FTP服务器收到PORT主动模式命令和端口号后,通过服务器的20端口和客户端开放的端口连接,发送数据。
(2) PASV(被动模式)
PASV是Passive的缩写,中文成为被动模式,工作原理:FTP客户端连接到FTP服务器的21端口,发送用户名和密码登录,登录成功后要list列表或者读取数据时,发送PASV命令到FTP服务器, 服务器在本地随机开放一个端口(1024以上),然后把开放的端口告诉客户端, 客户端再连接到服务器开放的端口进行数据传输。
那由于安全的原因,我们在传输的时候使用被动模式。
/** * ftp.enterLocalPassiveMode(); * 这个方法的意思就是每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。 * 为什么要这样做呢,因为ftp server可能每次开启不同的端口来传输数据, * 但是在linux上或者其他服务器上面,由于安全限制,可能某些端口没有开启,所以就出现阻塞。 */ ftpClient.enterLocalPassiveMode(); ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); flag = ftpClient.storeFile(fileName, inputStream);好了,完整的一个例子如下:
package com.wx.utils.ftp; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; import java.io.*; import java.net.MalformedURLException; public class FtpUtils { //ftp服务器地址 public String hostname = "106.106.106.16"; //ftp服务器端口号默认为21 public Integer port = 21 ; //ftp登录账号 public String username = "ftpuser"; //ftp登录密码 public String password = "ftpuser"; public FTPClient ftpClient = null; /** * 初始化ftp服务器 */ public void initFtpClient() { ftpClient = new FTPClient(); ftpClient.setControlEncoding("utf-8"); try { System.out.println("connecting...ftp服务器:"+this.hostname+":"+this.port); ftpClient.connect(hostname, port); //连接ftp服务器 ftpClient.login(username, password); //登录ftp服务器 int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器 if(!FTPReply.isPositiveCompletion(replyCode)){ System.out.println("connect failed...ftp服务器:"+this.hostname+":"+this.port); } System.out.println("connect successfu...ftp服务器:"+this.hostname+":"+this.port); }catch (MalformedURLException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } } /** * 上传文件 * @param pathname ftp服务保存地址 * @param fileName 上传到ftp的文件名 * @param originfilename 待上传文件的名称(绝对地址) * * @return */ public boolean uploadFile( String pathname, String fileName,String originfilename){ boolean flag = false; InputStream inputStream = null; try{ System.out.println("开始上传文件"); inputStream = new FileInputStream(new File(originfilename)); System.out.println(inputStream); initFtpClient(); ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE); CreateDirecroty(pathname); ftpClient.makeDirectory(pathname); boolean changeFlag = ftpClient.changeWorkingDirectory(pathname); System.out.println("切换目录:" + changeFlag); /** * ftp.enterLocalPassiveMode(); * 这个方法的意思就是每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据。 * 为什么要这样做呢,因为ftp server可能每次开启不同的端口来传输数据, * 但是在linux上或者其他服务器上面,由于安全限制,可能某些端口没有开启,所以就出现阻塞。 */ ftpClient.enterLocalPassiveMode(); ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); flag = ftpClient.storeFile(fileName, inputStream); inputStream.close(); ftpClient.logout(); System.out.println("上传文件成功:" + flag); }catch (Exception e) { System.out.println("上传文件失败"); e.printStackTrace(); }finally{ if(ftpClient.isConnected()){ try{ ftpClient.disconnect(); }catch(IOException e){ e.printStackTrace(); } } if(null != inputStream){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } /** * 上传文件 * @param pathname ftp服务保存地址 * @param fileName 上传到ftp的文件名 * @param inputStream 输入文件流 * @return */ public boolean uploadFile( String pathname, String fileName,InputStream inputStream){ boolean flag = false; try{ System.out.println("开始上传文件"); initFtpClient(); ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE); CreateDirecroty(pathname); ftpClient.makeDirectory(pathname); boolean changeFlag = ftpClient.changeWorkingDirectory(pathname); System.out.println("切换目录:" + changeFlag); flag = ftpClient.storeFile(fileName, inputStream); inputStream.close(); ftpClient.logout(); System.out.println("上传文件结果:" + flag); }catch (Exception e) { System.out.println("上传文件失败"); e.printStackTrace(); }finally{ if(ftpClient.isConnected()){ try{ ftpClient.disconnect(); }catch(IOException e){ e.printStackTrace(); } } if(null != inputStream){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; } //改变目录路径 public boolean changeWorkingDirectory(String directory) { boolean flag = true; try { flag = ftpClient.changeWorkingDirectory(directory); if (flag) { System.out.println("进入文件夹" + directory + " 成功!"); } else { System.out.println("进入文件夹" + directory + " 失败!开始创建文件夹"); } } catch (IOException ioe) { ioe.printStackTrace(); } return flag; } //创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建 public boolean CreateDirecroty(String remote) throws IOException { boolean success = true; String directory = remote + "/"; // 如果远程目录不存在,则递归创建远程服务器目录 if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) { int start = 0; int end = 0; if (directory.startsWith("/")) { start = 1; } else { start = 0; } end = directory.indexOf("/", start); String path = ""; String paths = ""; while (true) { String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1"); path = path + "/" + subDirectory; if (!existFile(path)) { if (makeDirectory(subDirectory)) { changeWorkingDirectory(subDirectory); } else { System.out.println("创建目录[" + subDirectory + "]失败"); changeWorkingDirectory(subDirectory); } } else { changeWorkingDirectory(subDirectory); } paths = paths + "/" + subDirectory; start = end + 1; end = directory.indexOf("/", start); // 检查所有目录是否创建完毕 if (end <= start) { break; } } } return success; } //判断ftp服务器文件是否存在 public boolean existFile(String path) throws IOException { boolean flag = false; FTPFile[] ftpFileArr = ftpClient.listFiles(path); if (ftpFileArr.length > 0) { flag = true; } return flag; } //创建目录 public boolean makeDirectory(String dir) { boolean flag = true; try { flag = ftpClient.makeDirectory(dir); if (flag) { System.out.println("创建文件夹" + dir + " 成功!"); } else { System.out.println("创建文件夹" + dir + " 失败!"); } } catch (Exception e) { e.printStackTrace(); } return flag; } /** * 下载文件 * * @param pathname FTP服务器文件目录 * * @param filename 文件名称 * * @param localpath 下载后的文件路径 * * @return */ public boolean downloadFile(String pathname, String filename, String localpath){ boolean flag = false; OutputStream os=null; try { System.out.println("开始下载文件"); initFtpClient(); //切换FTP目录 ftpClient.changeWorkingDirectory(pathname); FTPFile[] ftpFiles = ftpClient.listFiles(); for(FTPFile file : ftpFiles){ if(filename.equalsIgnoreCase(file.getName())){ File localFile = new File(localpath + "/" + file.getName()); os = new FileOutputStream(localFile); ftpClient.retrieveFile(file.getName(), os); os.close(); } } ftpClient.logout(); flag = true; System.out.println("下载文件成功"); } catch (Exception e) { System.out.println("下载文件失败"); e.printStackTrace(); } finally{ if(ftpClient.isConnected()){ try{ ftpClient.disconnect(); }catch(IOException e){ e.printStackTrace(); } } if(null != os){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } return flag; } /** * 删除文件 * * @param pathname FTP服务器保存目录 * * @param filename 要删除的文件名称 * * @return */ public boolean deleteFile(String pathname, String filename){ boolean flag = false; try { System.out.println("开始删除文件"); initFtpClient(); //切换FTP目录 ftpClient.changeWorkingDirectory(pathname); ftpClient.dele(filename); ftpClient.logout(); flag = true; System.out.println("删除文件成功"); } catch (Exception e) { System.out.println("删除文件失败"); e.printStackTrace(); } finally { if(ftpClient.isConnected()){ try{ ftpClient.disconnect(); }catch(IOException e){ e.printStackTrace(); } } } return flag; } public static void main(String[] args) { FtpUtils ftp =new FtpUtils(); ftp.uploadFile("vehicle/2020-07-03", "yyyyy1.jpg", "C:\\Users\\Administrator\\Desktop\\代金券测试\\yyyyy1.jpg"); //ftp.downloadFile("ftpFile/data", "123.docx", "F://"); // ftp.deleteFile("ftpFile/data", "123.docx"); System.out.println("ok"); } }参考:https://freexyz.cn/dev/44351.html
动哒APP Flutter 欢迎关注动哒公众号,微信搜索公众号dongda_5g,即可关注。您有任何问题可以在公众号进行回复,动哒客服会为您进行专业的解答。