2014-05-13 1 views
1

업로드 응용 프로그램을 만들고 있습니다.
업로드는 FTP를 통해 작동하지만 FTP 서버에서 파일의 이름을 바꾸고 싶지만 기존 연결과 작동하지 않습니다.
새 연결을 만드는 함수를 만들면 이름을 바꿀 수 있습니다 서버 (FTPUtility 클래스의 함수)에있는 파일이지만 FTP에서 연결을 끊고 새 연결을 만들고 싶지 않습니다.

기존 연결을 닫지 않은 상태로 사용할 수 있다고 생각했는데 코드가 동일한 클래스와 변수 내에 있습니다.

도움이 될만한 정보가 있으면 알려 주시기 바랍니다.FTPUtility 클래스에서 ftpClient.rename 함수가 작동하지 않습니다. apache.commons.net을 사용합니다.

UploadTask.java

public Void uploadFile() throws Exception { 
     FTPUtility util = new FTPUtility(host, port, username, password); 
     try { 
      util.connect();    
      util.uploadFile(uploadFile, destDir); 

      FileInputStream inputStream = new FileInputStream(uploadFile); 
      byte[] buffer = new byte[BUFFER_SIZE]; 
      int bytesRead = -1; 
      long totalBytesRead = 0; 
      int percentCompleted = 0; 
      long fileSize = uploadFile.length(); 

      while ((bytesRead = inputStream.read(buffer)) != -1) { 
       util.writeFileBytes(buffer, 0, bytesRead); 
       totalBytesRead += bytesRead; 
       percentCompleted = (int) (totalBytesRead * 100/fileSize); 
       barUpdate1.setValue(percentCompleted); 
      } 
      String strFileName = uploadFile.getName();    
      System.out.println("the filename in uploadtask is: " + strFileName); 
      boolean bolRenamed = false; 

//FILE RENAME - first line doesn’t work, second line does work as creates new connection 
     bolRenamed = util.renameFileOnServer("test", "testrename.txt"); 
     // util.testFTPrenameWORKSseperateApp(); 

      inputStream.close(); 
      util.finish(); 

     } catch (FTPException ex) { 
      JOptionPane.showMessageDialog(null, "Error uploading file: " + ex.getMessage(), 
        "Error", JOptionPane.ERROR_MESSAGE); 
      ex.printStackTrace(); 
      barUpdate1.setValue(0); 
     // cancel(true); 
     } finally { 
      util.disconnect(); 

FTPUtility.Java

public void uploadFile(File uploadFile, String destDir) throws FTPException { 
     try { 
      boolean success = ftpClient.changeWorkingDirectory(destDir); 
      if (!success) { 
       throw new FTPException("Could not change working directory to " 
         + destDir + ". The directory may not exist."); 
      } 

      success = ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
      if (!success) { 
       throw new FTPException("Could not set binary file type."); 
      } 

      outputStream = ftpClient.storeFileStream(uploadFile.getName()); 

//TESTNIG TESTING TESTING   
    if (outputStream != null) 
    { 
     System.out.println("file exists");} 
    else 
    { 
     System.out.println("file doesnt exist"); 
    } 

     } catch (IOException ex) { 
      throw new FTPException("uploadFile :@ Error uploading file: " + ex.getMessage()); 
     } 
    } 

// // WORKS가

일들이 잘 작동 기존 연결

public boolean renameFileOnServer(String fileName, String fileNameTo) throws FTPException 
    { 
       boolean result = false; 
       try {    
         result = ftpClient.rename("B100.PDF", "newFile100.pdf"); 
         if (result == true) 
         { 
           System.out.println("File renamed !"); 
         } 
         else 
         { 

       throw new FTPException("FTP error in rename file on server"); 

          // System.out.println("File renaming failed "); 
         } 
       } 
       catch (IOException e) 
       { 
         System.err.println(e); 
       } 

    return result; 
    } 

를 사용하여 작동하지 않습니다,363,210

public void testFTPrenameWORKSseperateApp() throws IOException { 
      FTPClient ftpClient = new FTPClient(); 
      boolean result; 
      try { 
        // Connect to the localhost 
        ftpClient.connect("xxx"); 

        // login to ftp server 
        result = ftpClient.login("xxx", "xxx"); 

        if (result == true) 
        { 
          System.out.println("Logged in Successfully !"); 
        } 
        else 
        { 
          System.out.println("Login Fail !"); 
          return; 
        } 
        // Rename file. 
        result = ftpClient.rename("B100.PDF", "newFile100.pdf"); 
        if (result == true) 
        { 
          System.out.println("File renamed !"); 
        } 
        else 
        { 
          System.out.println("File renaming failed "); 
        } 

      } 
      catch (FTPConnectionClosedException e) 
      { 
        System.err.println(e); 
      } 
      finally 
      { 
        try 
        { 
        ftpClient.disconnect(); 
        } 
        catch (FTPConnectionClosedException e) 
        { 
          System.err.println(e); 
        } 
      } 
    } //eof function 

답변

0

해결 폐쇄 스트림 util.finish

inputStream.close(); 
     util.finish(); 
     bolRenamed = util.renameFileOnServer(uploadFile.getName(), strFileNamePrefix); 

그러나 현재 파일이 변경되는 경우에도 오류가 아래 바꾸기 기능 이동 파일 이름을 변경 캐치 코드로부터 MSGBOX를 보이고 함수 실행의 시작;

try { 
     uploadFile(); 
    } 
    catch (Exception ex) { 
     JOptionPane.showMessageDialog(null, "Error uploading file: " + ex.getMessage(), 
       "Error", JOptionPane.ERROR_MESSAGE); 
     //barUpdate1.setValue(progress); 
    } 
관련 문제