谨慎使用java的try catch
发布时间:2020-12-13 20:04:57 所属栏目:PHP教程 来源:网络整理
导读:看看下面代码有何问题: public static String writeFileToRemote(File file) throws Exception {PostMethod filePost = new PostMethod(Common.getInstance().getProperty(project/remoteFileServer)+ /attach?action=upload);try { Part[] parts = new Par
看看下面代码有何问题: public static String writeFileToRemote(File file) throws Exception {
PostMethod filePost = new PostMethod(Common.getInstance().getProperty(
"project/remoteFileServer")
+ "/attach?action=upload");
try {
Part[] parts = new Part[4];
parts[0] = new StringPart("FILE_KIND","2");
parts[1] = new StringPart("RECORD","true");
parts[2] = new StringPart("FILE_KIND","2");
parts[3] = new FilePart("FILE_PATH",file.getName(),file);
filePost.setRequestEntity(new MultipartRequestEntity(parts,filePost.getParams()));
HttpClient client = new HttpClient();
// 由于要上传的文件可能比较大,因此在此设置最大的连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(
5000);
int status = client.executeMethod(filePost);
String file_path = "";
if (status == HttpStatus.SC_OK) {
// 从服务器响应的串中获得fileId
String res = filePost.getResponseBodyAsString();
String retFileStr = "retobj.fileId = "";
res = res.substring(res.indexOf(retFileStr)
+ retFileStr.length());
file_path = res.substring(0,res.indexOf('"'))
.replace('|','/');
}
file.delete();
return file_path;
} catch (Exception e) {
e.printStackTrace();
throw new Exception("写远程文件时出现异常!");
}
} 报错:
Caused by: java.lang.Exception: 写远程文件时出现异常!
com.age.sale.view.tool.ImportTool.writeFileToRemote(ImportTool.java:112) 此时根本没法定位毛病!
因此,应当把try catch去掉,如果要catch,必须把报错信息记录下来!并显示到前台!由于查询日志是很麻烦的1件事情。 本文出自:ouyida3的csdn blog 2015.3.5 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |