FTP-org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
阿新 • • 發佈:2019-01-25
問題(下載ftp檔案):
寫一個ftp下載程式,出現很怪異的錯誤,寫得過程中會出現空指標,
有時候又出現java.net.SocketTimeoutException: Read timed out,
還會出現org.apache.commons.net.io.CopyStreamException: IOException caught while copying.
Java程式碼
- public void run() {
- ftpClient.enterLocalPassiveMode();
-
OutputStream outputStream = null
- try {
- ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
- ftpClient.changeWorkingDirectory(path);// 轉移到FTP伺服器目錄
- FTPFile[] fs = ftpClient.listFiles(); // 得到目錄的相應檔案列表
- for (int i = 0; i < fs.length; i++) {
-
FTPFile ff = fs[i];
- if (ff.getName().equals(fileName)) {
- System.out.println(ff.getName());
- String filename = fileName;
- // 這個就就是彈出下載對話方塊的關鍵程式碼
- // response.setContentType("application/octet-stream");
-
response.setHeader("Content-disposition"
- "attachment;filename="
- + URLEncoder.encode(filename, "utf-8"));
- // 將檔案儲存到輸出流outputStream中
- InputStream in = ftpClient.retrieveFileStream(new String(ff
- .getName().getBytes("GBK"), "ISO-8859-1"));
- outputStream = response.getOutputStream();
- int len = 0;
- long size = 0;
- long t = 16364;
- byte[] bt = new byte[1024];
- while ((len = in.read(bt)) > 0) {
- outputStream.write(bt, 0, len); // outputStream.flush();
- size = size + len;
- System.out.println(this.fileName + "已xiazai :" + size);
- }
- /*
- * // response.s File file = new File("d:/123456.com"); //
- * response.reset();response //response. outputStream =
- * response.getOutputStream(); // new
- * FileOutputStream(file); boolean boo = false; if
- * (outputStream != null) { boo = ftpClient
- * .retrieveFile(ff.getName(), outputStream); }
- */
- // System.out.println(boo);
- // in.close();
- // os.close();
- outputStream.flush();
- }
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- outputStream.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- FTPUtil.disconnect(ftpClient);
- }
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)
分析原因:經反覆測試這異常好像都是因為網路的問題,因為網路不穩定下載直接被迫中斷了