java - 为什么我的日志中没有打印/触发 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);
}
}
}
}
解决方案
尝试改变你比较这些字符串的方式pMode
和"LOCAL_DIR"
. 代替
if(pMode == "LOCAL_DIR"){
this.getLocalFile(file);
}
做
if("LOCAL_DIR".equals(pMode)){
this.getLocalFile(file);
}
希望有帮助
推荐阅读
- python - 向所有客户端发送消息
- java - 如何从属性文件中指定的 XML 文件加载 Spring bean 定义?
- java - 如何创建不返回响应的改造 api 方法调用
- javascript - 谷歌图书 API
- matlab - 数据区域的Matlab polyfit
- python - Python - 从子目录中捕获 txt 文件,无需手动编写
- php - 周数唯一可排序整数 id
- python - 使用 Doc2Vec 测量两个文档之间的相似性
- android - 根据文本值使用 kotlin 将数据从 firebase 检索到 android studio
- django - 如何在 Python Django 中托管 Angular 6 应用程序?