1. 程式人生 > >FTP-org.apache.commons.net.io.CopyStreamException: IOException caught while copying.

FTP-org.apache.commons.net.io.CopyStreamException: IOException caught while copying.

問題(下載ftp檔案):

寫一個ftp下載程式,出現很怪異的錯誤,寫得過程中會出現空指標, 

有時候又出現java.net.SocketTimeoutException: Read timed out,

還會出現org.apache.commons.net.io.CopyStreamException: IOException caught while copying. 




Java程式碼  收藏程式碼
  1. public void run() {  
  2.         ftpClient.enterLocalPassiveMode();  
  3.         OutputStream outputStream = null
    ;  
  4.         try {  
  5.             ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  
  6.             ftpClient.changeWorkingDirectory(path);// 轉移到FTP伺服器目錄  
  7.             FTPFile[] fs = ftpClient.listFiles(); // 得到目錄的相應檔案列表  
  8.             for (int i = 0; i < fs.length; i++) {  
  9.                 FTPFile ff = fs[i];  
  10.                 if (ff.getName().equals(fileName)) {  
  11.                     System.out.println(ff.getName());  
  12.                     String filename = fileName;  
  13.                     // 這個就就是彈出下載對話方塊的關鍵程式碼  
  14.                     // response.setContentType("application/octet-stream");  
  15.                     response.setHeader("Content-disposition"
    ,  
  16.                     "attachment;filename="  
  17.                     + URLEncoder.encode(filename, "utf-8"));  
  18.                     // 將檔案儲存到輸出流outputStream中  
  19.                     InputStream in = ftpClient.retrieveFileStream(new String(ff  
  20.                             .getName().getBytes("GBK"), "ISO-8859-1"));  
  21.                     outputStream = response.getOutputStream();  
  22.                     int len = 0;  
  23.                     long size = 0;  
  24.                     long t = 16364;  
  25.                     byte[] bt = new byte[1024];  
  26.                     while ((len = in.read(bt)) > 0) {  
  27.                         outputStream.write(bt, 0, len); // outputStream.flush();  
  28.                         size = size + len;  
  29.                         System.out.println(this.fileName + "已xiazai :" + size);  
  30.                     }  
  31.                     /* 
  32.                      * // response.s File file = new File("d:/123456.com"); // 
  33.                      * response.reset();response //response. outputStream = 
  34.                      * response.getOutputStream(); // new 
  35.                      * FileOutputStream(file); boolean boo = false; if 
  36.                      * (outputStream != null) { boo = ftpClient 
  37.                      * .retrieveFile(ff.getName(), outputStream); } 
  38.                      */  
  39.                     // System.out.println(boo);  
  40.                     // in.close();  
  41.                     // os.close();  
  42.                     outputStream.flush();  
  43.                 }  
  44.             }  
  45.         } catch (IOException e) {  
  46.             // TODO Auto-generated catch block  
  47.             e.printStackTrace();  
  48.         } finally {  
  49.             try {  
  50.                 outputStream.close();  
  51.             } catch (IOException e) {  
  52.                 // TODO Auto-generated catch block  
  53.                 e.printStackTrace();  
  54.             }  
  55.             FTPUtil.disconnect(ftpClient);  
  56.         }  



org.apache.commons.net.io.CopyStreamException: IOException caught while copying. 
at org.apache.commons.net.io.Util.copyStream(Util.java:127) 
at org.apache.commons.net.ftp.FTPClient.retrieveFile(FTPClient.java:1295) 
at com.yniso.filesystem.DownloadFTPFile.run(DownloadFTPFile.java:92) 






rrrrr.kk 
java.net.SocketTimeoutException: Read timed out 
at java.net.SocketInputStream.socketRead0(Native Method) 
at java.net.SocketInputStream.read(Unknown Source) 
at java.io.BufferedInputStream.read1(Unknown Source) 
at java.io.BufferedInputStream.read(Unknown Source) 
at java.io.BufferedInputStream.fill(Unknown Source) 
at java.io.BufferedInputStream.read(Unknown Source) 
at org.apache.commons.net.telnet.TelnetInputStream.__read(TelnetInputStream.java:114) 
at org.apache.commons.net.telnet.TelnetInputStream.run(TelnetInputStream.java:535) 
at java.lang.Thread.run(Unknown Source) 


Demo.rar 
Demo.rar已xiazai :1024 
Demo.rar已xiazai :2048 
Demo.rar已xiazai :3072 
Demo.rar已xiazai :4096 
Demo.rar已xiazai :5120 
Demo.rar已xiazai :6144 
Demo.rar已xiazai :7168 
Demo.rar已xiazai :8192 
Demo.rar已xiazai :9216 
Demo.rar已xiazai :10240 
Demo.rar已xiazai :11264 
Demo.rar已xiazai :12288 
Demo.rar已xiazai :13312 
Demo.rar已xiazai :14336 
Demo.rar已xiazai :15360 
Demo.rar已xiazai :16384 
Exception in thread "Thread-22" java.lang.NullPointerException 
at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:740) 
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:434) 
at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:349) 
at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:764)
at org.apache.coyote.http11.filters.ChunkedOutputFilter.doWrite(ChunkedOutputFilter.java:124) 
at org.apache.coyote.http11.InternalOutputBuffer.doWrite(InternalOutputBuffer.java:573) 
at org.apache.coyote.Response.doWrite(Response.java:560) 
at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:353) 
at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:434) 
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:309) 
at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:273) 
at org.apache.catalina.connector.CoyoteOutputStream.close(CoyoteOutputStream.java:104) 

at com.yniso.filesystem.DownloadFTPFile.run(DownloadFTPFile.java:120) 

分析原因:經反覆測試這異常好像都是因為網路的問題,因為網路不穩定下載直接被迫中斷了