繁体   English   中英

使用commons net 3.0 api(org.apache.commons.net)在ftp服务器上上传文件时出现问题

[英]Problem in Uploading file on ftp server using commons net 3.0 api (org.apache.commons.net )

我正在尝试将文本文件保存在驱动器中的ftp服务器上,该文件具有数据,该文件已成功存储在该文件中,但是即使包含数据,该文件也完全为空。

以下是我的宽松尝试。

FTPClient client = new FTPClient();
    FileInputStream fis = null;


    try {
        client.connect("ftp.drivehq.com");
    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        client.login("zaheerkth", "mypassword");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String filename = "c://ss.txt";
    try {
        fis = new FileInputStream(filename);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        client.storeFile(filename, fis);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        client.logout();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fis.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }

如果您在防火墙或NAT之后运行,则可能需要执行client.enterLocalPassiveMode()

编辑:本地被动可能是您想要的。

这是我的代码,请在此检查它。我想在数据库中存储文件名,并在此代码中将文件存储在ftp服务器中,我能够存储该文件,该文件中没有内容,它存储为空白文件。

     String email="";    
     int i=0;    

 int count1=0;
     boolean isMultipart = ServletFileUpload.isMultipartContent(request);
     if (!isMultipart) {
      } else {
   FileItemFactory factory = new DiskFileItemFactory();
   ServletFileUpload upload = new ServletFileUpload(factory);
   List items = null;
   try {
   items = upload.parseRequest(request);
   } catch (FileUploadException e) {
   e.printStackTrace();
   }
   Iterator itr = items.iterator();
   while (itr.hasNext()) 
   {
   FileItem item = (FileItem) itr.next();
   if (item.isFormField())
   {
      String name = item.getFieldName();
      String value = item.getString();
      if(name.equals("email"))
           {
           email=value;
                }




     } else
   {
     try {

     String itemName = item.getName();
     itemName = new java.io.File(itemName).getName(); 

     String fname="";
     String ext="";
     int mid= itemName.lastIndexOf(".");
     fname=itemName.substring(0,mid);
     ext=itemName.substring(mid+1,itemName.length()); 

     if(ext.equals("doc") || ext.equals("docx") ||ext.equals("txt")    ||ext.equals("pdf")){  
     count1=1;
     FileInputStream fis = null;


    Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/beehivej_beehivejobs", "root", "root");
                Statement stmt = conn.createStatement();
                String s="SELECT idfile FROM file ORDER BY idfile DESC LIMIT 1";
                ResultSet rs=stmt.executeQuery(s);
                while(rs.next()){i=Integer.parseInt(rs.getString(1));}
                String sql="insert into file (filename,emailid) Values ('"+itemName+"','"+email+"')";
                int in=stmt.executeUpdate(sql);

      FTPClient client = new FTPClient();

      try {

      client.connect("ftp.xyz.com");

      } catch (SocketException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      try {
      client.login("username", "password");


      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }


      try {
      fis = new FileInputStream(itemName);
      count1=1;
      } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
      e.printStackTrace();
      }
      try {

      String name="Beehive_Resume"+i+"."+ext;
      client.enterLocalPassiveMode();
      client.storeFile(itemName, fis);

     count1=1;
     } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
     }
    try {
    client.enterLocalActiveMode();
    client.logout();
    count1=1;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    try {
    fis.close();
    count1=1;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    }else
    {
      response.sendRedirect("Account/reg4.jsp?email="+email+"&msg=This is not Supported  File");
    }
    }catch (Exception e) {
    e.printStackTrace();
    count1=1;
    }
    }
    } 
    }
    if(count1==1)
    {
     response.sendRedirect("Account/Home.jsp?email="+email);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM