所以我有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);
}
}
}
}
答案 0 :(得分:1)
尝试更改比较这些字符串pMode
和"LOCAL_DIR"
的方式。代替
if(pMode == "LOCAL_DIR"){
this.getLocalFile(file);
}
做
if("LOCAL_DIR".equals(pMode)){
this.getLocalFile(file);
}
希望有帮助