Java org.apache.commons.net.ftp.FTPClient无法在连接失败后重新启动ftp上载

Java org.apache.commons.net.ftp.FTPClient无法在连接失败后重新启动ftp上载,java,jakarta-ee,ftp,javafx,java-io,Java,Jakarta Ee,Ftp,Javafx,Java Io,我试图实现ftp上传的回退机制,以防通信错误/超时等。实际上,我试图实现的是,如果文件正在上传(使用调度程序)我有一个连接失败,那么这个文件在调度器下一次尝试上传时应该能够重新启动文件上传 因此,我的计划程序每X分钟调用一个方法,该方法轮询一个目录以检索要发送的文件: public void sendZippFiles() throws IOException, Exception { ... if (directoryListing2 != null) { fo

我试图实现ftp上传的回退机制,以防通信错误/超时等。实际上,我试图实现的是,如果文件正在上传(使用调度程序)我有一个连接失败,那么这个文件在调度器下一次尝试上传时应该能够重新启动文件上传

因此,我的计划程序每X分钟调用一个方法,该方法轮询一个目录以检索要发送的文件:

public void sendZippFiles() throws IOException, Exception {
    ...
    if (directoryListing2 != null) {
        for (File child : directoryListing2) {
            ...
            StudyDetailsDAO studyDetailsDAO = new StudyDetailsDAOImpl();
            TransferFileFtp ftpTransfer = new TransferFileFtp();

            // check if the file is in progress of uploading
            Boolean isInProgress = studyDetailsDAO.checkStudyDetailsIsInProgress(tmpStr);
            // check if the file is registered in local db - only registered files should be send
            Boolean studyExist = studyDetailsDAO.checkStudyDetailsExists(tmpStr);

            if(!isInProgress && child1.exists() && !child1.isDirectory() && studyExist) {
                 // set flag true to mark that the file is in progress of uploading
                 studyDetailsDAO.updateStudyIsInProgress(tmpStr, true);
                 // try and upload the file by calling a method to upload the file
                 Boolean toFtpTransfer = ftpTransfer.uploadFileFtp3(child1);
                 if (toFtpTransfer==true) {
                    //if success then a) rename remote file and delete file from local directory
                    ftpTransfer.renameFileFtp3(child1);
                    boolean delete = child1.delete();
                    //if success then update required flags to db
                    if(child1.toPath().toString().endsWith(".zip")) {
                    try {
                        studyDetailsDAO.updateStudyIsUploaded(tmpStr, true);
                        studyDetailsDAO.updateStudyIsInProgress(tmpStr, false);
                        studyDetailsDAO.updateIsReadyToProccess(tmpStr, false);
                    } catch(Exception e){
                        // if any error occurs
                        System.out.println("Error sending the file!");
                        e.printStackTrace();
                        toFtpTransfer = false;
                    }                    
                    } else {
                        System.out.println("Error in file type!");
                        child1.delete();
                    }
                } else {
                    System.out.println("An error occured during file upload...");
                    // maybe here we should try and set IsInProgress to FALSE
                    studyDetailsDAO.updateStudyIsInProgress(tmpStr, false);
                }
                ....
下面是上传文件的方法:ftpTransfer.uploadFileFtp3(文件)

因此,问题如下:如果文件上载在计划程序下次尝试重新启动其上载时中断(通信错误),我将收到NullPointerException:

Start uploading file
Start uploading file with size: 23783382
Ιαν 22, 2015 2:15:58 ΜΜ com.npap.dicomrouter2.FXMLDocumentController RunScheduledTasks
SEVERE: null
java.lang.NullPointerException
at com.npap.network.TransferFileFtp.uploadFileFtp3(TransferFileFtp.java:240)
at com.npap.utils.ProcessDicomFiles.sendZippFiles(ProcessDicomFiles.java:165)
at com.npap.dicomrouter2.FXMLDocumentController.RunScheduledTasks(FXMLDocumentController.java:290)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8216)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3724)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/128893786.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
我收到错误的地方是我试图在upload file方法中写入outputStream的地方:

while ((read = inputStream.read(bytesIn)) != -1) {
            outputStream.write(bytesIn, 0, read);
            System.out.println("Uploaded: " + read*(cc+1) + " of " + fileSize + " of file: " + sendFile.getName());
            cc++;
        }

我错过了什么?我是否应该以某种方式重新创建一个新的OutputStream对象?这就是问题所在吗?

您的
输出流是否为空?尝试通过
if(outputStream==null)
检查它,那么if(outputStream==null)应该重新启动整个过程吗?构建所有必需的对象等?您必须在某个地方构建对象
OutputStream
,这样它就不会为空。具体在哪里?我尝试在try{}块外初始化outputStream,然后将其设置为ftpClient.storeFileStream(secondRemoteFile)。无论我尝试了什么,我都会收到相同的NullPointerException。。。你能详细说明一下吗?我建议你做的(有趣的是,你在文件上传失败后再次调用该方法)是创建一个新类专门用于文件上传(我不确定你是否在做同样的事情)。如果文件上载失败,请再次调用
new MyClass().uploadFileFtp3(文件)
while ((read = inputStream.read(bytesIn)) != -1) {
            outputStream.write(bytesIn, 0, read);
            System.out.println("Uploaded: " + read*(cc+1) + " of " + fileSize + " of file: " + sendFile.getName());
            cc++;
        }