2012-04-05 1 views
0

누구든지이 코드의 문제점을 볼 수 있습니까? 진행률 막대를 표시하지 않지만 모든 파일을 업로드합니다. 나는 체크 아웃 썬 튜토리얼과 스윙 워커도 만들었지 만 아직 해결하지 못했습니다.ProgressMonitorInputStream을 ftp 업로드에 추가하는 방법은 무엇입니까?

private static boolean putFile(String m_sLocalFile, FtpClient m_client) { 
    boolean success = false; 
    int BUFFER_SIZE = 10240; 
    if (m_sLocalFile.length() == 0) { 
     System.out.println("Please enter file name"); 
    } 
    byte[] buffer = new byte[BUFFER_SIZE]; 
    try { 
     File f = new File(m_sLocalFile); 
     int size = (int) f.length(); 
     System.out.println("File " + m_sLocalFile + ": " + size + " bytes"); 
     System.out.println(size); 
     FileInputStream in = new FileInputStream(m_sLocalFile); 
     //test 
     InputStream inputStream = new BufferedInputStream(
         new ProgressMonitorInputStream(null,"Uploading " + f.getName(),in)); 

     //test 
     OutputStream out = m_client.put(f.getName()); 

     int counter = 0; 
     while (true) { 
      int bytes = inputStream.read(buffer); //in 
      if (bytes < 0) 
       break; 
      out.write(buffer, 0, bytes); 
      counter += bytes; 
      System.out.println(counter); 
     } 

     out.close(); 
     in.close(); 
     inputStream.close(); 
     success =true; 
    } catch (Exception ex) { 
     System.out.println("Error: " + ex.toString()); 
    } 
    return true; 
} 

답변

0

나는 코드가 괜찮다고 생각합니다.
작업이 진행 막대가 필요할만큼 오래 걸리지 않았습니까?

다음은 로컬 파일에서 읽고 다른 로컬 파일에 쓰는 코드의 수정 된 버전입니다.
지연 시간을 추가하여 진행률 표시 줄에 시간을 제공합니다. 샘플 12MB PDF 파일로 내 시스템에서 정상적으로 작동하며 진행률 막대가 표시됩니다.
파일 크기가 작 으면 5 밀리 초에서 100 또는 그 이상으로 수면을 늘리십시오. 실험 해보십시오.

그리고 ProgressMonitorInputStream 클래스가 존재한다는 것을 알지 못해서 나 자신을 배웠습니다.].

/** 
* main 
*/ 
public static void main(String[] args) { 
    try { 
     System.out.println("start"); 

     final String inf = "d:/testfile.pdf"; 
     final String outf = "d:/testfile.tmp.pdf"; 
     final FileOutputStream out = new FileOutputStream(outf) { 
      @Override 
      public void write(byte[] b, int off, int len) throws IOException { 
       super.write(b, off, len); 
       try { 
        // We delay the write by a few millis to give the progress bar time to kick in 
        Thread.sleep(5); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     }; 

     putFile(inf, out); 

     System.out.println("end"); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 

private static boolean putFile(String m_sLocalFile, OutputStream out /*FtpClient m_client*/) { 
    boolean success = false; 
    int BUFFER_SIZE = 10240; 
    if (m_sLocalFile.length() == 0) { 
     System.out.println("Please enter file name"); 
    } 
    byte[] buffer = new byte[BUFFER_SIZE]; 
    try { 
     File f = new File(m_sLocalFile); 
     int size = (int) f.length(); 
     System.out.println("File " + m_sLocalFile + ": " + size + " bytes"); 
     System.out.println(size); 
     FileInputStream in = new FileInputStream(m_sLocalFile); 
     //test 
     InputStream inputStream = new BufferedInputStream(
         new ProgressMonitorInputStream(null,"Uploading " + f.getName(),in)); 

     //test 
     //OutputStream out = m_client.put(f.getName()); 

     int counter = 0; 
     while (true) { 
      int bytes = inputStream.read(buffer); //in 
      if (bytes < 0) 
       break; 
      out.write(buffer, 0, bytes); 
      counter += bytes; 
      System.out.println(counter); 
     } 

     out.close(); 
     in.close(); 
     inputStream.close(); 
     success =true; 
    } catch (Exception ex) { 
     System.out.println("Error: " + ex.toString()); 
    } 
    return true; 
} 
+0

당신이 옳습니다. 작업이 진행 막대가 필요하기에 충분하지 않습니다. – itro

관련 문제