2011-09-26 35 views
1

以下是我在ftp位置编写一个csv文件的java函数。该文件被创建为ftp位置,但文件大小为0字节,文件为空。很好的帮助,因为我卡住了在ftp位置创建0字节的文件

public int WriteFileToFtp(String FileName, String FileData) { 

    //get these details for the version?? 
    //?? 

    FTPClient ftp= new FTPClient(); 
    try { 
     InputStream is = new ByteArrayInputStream(FileData.getBytes("ISO-8859-1")); 
     ftp.connect(ftpIP); 
     boolean isConnection = ftp.login(userName,password);   
     if(!isConnection){ 
      logger.error("Connection failed"); 
      return -1; 
     } 
     ftp.enterLocalActiveMode();               
     ftp.setFileType(FTP.BINARY_FILE_TYPE);//setting fileType 
     //?? go to directory using the circle code 
     if(ftpDirectoryToBeUpdate!=null && ftpDirectoryToBeUpdate.trim().length()>0) 
     { 
      logger.error("Changing directory for write="+ftpDirectoryToBeUpdate+" lcd="+ftp.printWorkingDirectory()); 
      ftp.changeWorkingDirectory(ftpDirectoryToBeUpdate); 
      logger.error("Changed directory for write="+ftpDirectoryToBeUpdate+" lcd="+ftp.printWorkingDirectory()); 
     }else 
     { 
      logger.error("Changed directory for write failed lcd="+ftp.printWorkingDirectory()); 
      logger.error("DirectoryToReadFrom="+ftpDirectoryToBeUpdate); 
     } 

     ftp.storeFile(FileName,is) ; 
     logger.error(ftp.getReplyString()); 
     is.close(); 

     if(ftp.isConnected()) 
      ftp.disconnect(); 
    } catch (SocketException e) { 
     //logger.error(LoggerKeyWord.ERROR_ALERT + " FTP WRITE ERROR"); 
     logger.error(e,e); 
     e.printStackTrace(); 
     return -1; 
    } catch (IOException e) { 
    // logger.error(LoggerKeyWord.ERROR_ALERT + " FTP WRITE ERROR"); 
     logger.error(e,e); 
     e.printStackTrace(); 
     return -1; 
    } 
    try { 
     if(ftp.isConnected()) 
      ftp.disconnect(); 
    } catch (IOException e) { 
     logger.error (e,e); 
     e.printStackTrace(); 
     return -1; 
    } 
    return 1; 
} 
+0

你看过网络上发生了什么? 'FileData'的大小是多少? (你也应该看看Java的命名约定......) –

+0

你是否在日志中出现错误? –

+0

变量名称应该以小写字母开头。你的'FileName'和'FileData'看起来像是分散注意力的类名。 –

回答

2

你试图关闭输入流之前保存文件?