首页 > 解决方案 > 为什么我的日志中没有打印/触发 filenotfound 异常

问题描述

所以我有 2 种方法,我将其代码放在下面。第一个方法调用第二个方法,第二个方法负责打开一个文件读取它的数据并将它发送到一个dataoutputstream请求变量。

我的问题是,如果文件夹中不存在该文件,我将无法打印任何堆栈跟踪,或者如果调用第二种方法,则无法打印有关异常的信息。

但是,如果我取消注释第一种方法中的注释行(它做同样的事情)并且根本不调用第二种方法,我可以在我的日志文件中看到日志、堆栈跟踪和 filenot found 异常。

谁能提出为什么会这样?即使调用第二种方法,我也需要能够使用找不到文件异常:

public void addBodySend(String metadata, File file, String pMode) throws Exception{           
    try{
        request = new DataOutputStream(httpConn.getOutputStream());
        String post_data = crlf+twoHyphens + boundary + crlf+"Content-Disposition: form-data; name=\"properties\""+crlf+crlf+metadata+crlf+twoHyphens+boundary+crlf;
        String fileName = file.getName();
        post_data = post_data + "Content-Disposition: form-data; name=\"file\"; filename=\"" + fileName + "\""+crlf+"Content-Type: application/octet-stream"+crlf+crlf;
        request.writeBytes(post_data);
        if(pMode == "LOCAL_DIR"){
            this.getLocalFile(file);
        }
        /*fileInputStream = new FileInputStream(file);
        bufferedInputStream = new BufferedInputStream(fileInputStream);
        byte[] buffer;
        buffer = new byte[1024];
        int bytesRead=0;
        while((bytesRead = bufferedInputStream.read(buffer)) != -1){
            request.write(buffer,0,bytesRead);
            request.flush();
        }
        request.writeBytes(crlf+twoHyphens + boundary + twoHyphens + crlf);
        request.flush();*/
    }catch(Exception ex){
        this.sendStatus = false;
        ex.printStackTrace();
        log.writeln("Error while sending post request",0);
        for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
            log.writeln(ste.toString(),0);
        }
    }finally{
        if(bufferedInputStream!=null){
            try{
            bufferedInputStream.close();
            }catch(Exception ex){
                log.writeln("Error closing bufferred input stream",0);
            }
        }  
        if(request!=null){
            try{
            request.close();
            }catch(Exception ex){
                log.writeln("Error closing post request",0);
            }
        }    
    }
}

public void getLocalFile(File file) {
    try{
        fileInputStream = new FileInputStream(file);
        bufferedInputStream = new BufferedInputStream(fileInputStream);
        byte[] buffer;
        buffer = new byte[1024];
        int bytesRead=0;
        while((bytesRead = bufferedInputStream.read(buffer)) != -1){
            request.write(buffer,0,bytesRead);
            request.flush();
        }
        request.writeBytes(crlf+twoHyphens + boundary + twoHyphens + crlf);
        request.flush();   
        this.sendStatus = true;        
    }catch(Exception ex){
        this.sendStatus = false;
        ex.printStackTrace();
        log.writeln("Error while sending binary body",0);
        for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
            log.writeln(ste.toString(),0);
        }
    }finally{
        if(bufferedInputStream!=null){
            try{
            bufferedInputStream.close();
            }catch(Exception ex){
                log.writeln("Error closing bufferred input stream",0);
            }
        }
    }
}

标签: java

解决方案


尝试改变你比较这些字符串的方式pMode"LOCAL_DIR". 代替

if(pMode == "LOCAL_DIR"){
     this.getLocalFile(file);
}

if("LOCAL_DIR".equals(pMode)){
      this.getLocalFile(file);
}

希望有帮助


推荐阅读