2011-05-21 39 views
0

我想在ftp服務器上保存ftp服務器上的文本文件,該文件位於我的驅動器中,它有數據,文件成功存儲在那裏,但即使它有數據,文件也是完全空的。上傳文件在ftp服務器上使用commons net 3.0 api時出現問題(org.apache.commons.net)

以下是我的嘗試。

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(); 
    } 
    } 

回答

2

如果您在防火牆或NAT後面運行,則可能需要執行client.enterLocalPassiveMode()

編輯:本地被動可能是你想要的。

+0

thankyou。有用 :)。所以我得到解決方案後3天上傳文件:(。 – 2011-05-22 00:05:34

+0

[Sarif Sissddharth](http://stackoverflow.com/users/1223381/saraf-sissddharth)[請求更多詳細信息](http://stackoverflow.com/suggested-edits/226263):他應該在哪裏打這個電話? – Rup 2012-03-23 09:56:03

+0

在你調用'storeFile'之前調用它,它應該可以工作,假設你沒有任何其他網絡問題。 – Femi 2012-03-23 10:04:48

1

這是我的代碼,請檢查它在這我想存儲數據庫中的文件名&存儲文件在這個代碼中的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); 
    } 
相關問題