Java InputStreams没有关闭或放弃文件

时间:2018-04-16 14:37:49

标签: java inputstream

使用close()时,FileInputStream没有关闭。当循环开始备份时,任何没有SFTPd的文件(由于服务器的超时)都无法访问,因为它仍然保留上一循环中的文件。关闭FileInputStream时我做错了什么?

错误文字: java.nio.file.FileSystemException:[文件路径] - > [文件路径]:进程无法访问该文件,因为它正由另一个进程使用。

Execptions: java.net.ConnectException:连接超时:连接     at java.net.DualStackPlainSocketImpl.connect0(Native Method)     在java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)     在java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)     在java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)     在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)     在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)     在java.net.Socket.connect(Socket.java:589)     在org.apache.commons.net.ftp.FTPClient。 openDataConnection (FTPClient.java:920)     在org.apache.commons.net.ftp.FTPSClient。 openDataConnection (FTPSClient.java:627)     在org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:653)     在org.apache.commons.net.ftp.FTPClient .__ storeFile(FTPClient.java:639)     在org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:2030)     at alliedftp.AlliedFtp.putFiles(AlliedFtp.java:73)     at alliedftp.AlliedFtp.main(AlliedFtp.java:37)

java.nio.file.FileSystemException:[文件路径] - > [文件路径]:进程无法访问该文件,因为它正由另一个进程使用。

at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:387)
at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287)
at java.nio.file.Files.move(Files.java:1395)
at alliedftp.AlliedFtp.putFiles(AlliedFtp.java:77)
at alliedftp.AlliedFtp.main(AlliedFtp.java:37)

private static void putFiles(String filePath) {
        AlliedFtp.setPath(filePath);
        FTPSClient client = new FTPSClient();
        String today = new SimpleDateFormat("MMddyyyy").format(new Date()) + "_";
while(true) {
            try {
               ArrayList<FileInputStream> list = AlliedFtp.setFileInputStreams(new File(path));
               ArrayList<File> listNames = AlliedFtp.setFileNames(new File(path));
               int fileStreamsSize = list.size();
               int fileNamesSize   = listNames.size();                    
               // Connect to remote host
               client.connect(ftpServer);
               // Login
               if(!client.login(ftpUser, ftpPass)) {
                   client.logout();
                   System.out.println("Login Error");
               } 
               // Enter passive mode
               //client.setBufferSize(1024000);
               client.enterLocalPassiveMode();
               client.setFileType(FTP.BINARY_FILE_TYPE);
               // SFTP Files
               for(int i = 0; i < fileNamesSize; i++) {     
                    // SFTP files
                    if(client.storeFile(listNames.get(i).getName(), list.get(i))) {
                        // Close input stream
                        list.get(i).close();
                        // Move Files
                        Files.move(listNames.get(i).toPath(), Paths.get(path + File.separator + "Archive" + File.separator + today + listNames.get(i).getName()), StandardCopyOption.REPLACE_EXISTING);   
                    } else {
                        list.get(i).close();
                    }
                }                                
                // Log Out
                client.logout();
                client.disconnect();   
            } catch(IOException e) {
                System.err.println(e);
            } 
            // Wait
            try {
                TimeUnit.MINUTES.sleep(2);
            } catch(InterruptedException e) {
                System.out.println(e);
            }  
        }  
    }

0 个答案:

没有答案