【发布时间】:2019-10-18 15:05:25
【问题描述】:
我有一个以前在 Tomcat 8 中运行的 Java 程序。
现在,我已经将我的 Tomcat 升级到 Tomcat 9,我发现程序在 getAbsoluteFile 处给了我错误。
我怀疑这个错误是由于文件夹访问权限的限制。
但是,我已经将权限更改为 chown root,chmod 777,但它仍然给出相同的错误-
java.io.FileOutputStream.open(FileOutputStream.java:270) 6 月 1 日 16:48:29 te tomcat9 [7844]: #011at java.io.FileOutputStream.(FileOutputStream.java:213) 6 月 1 日 16:48:29 te tomcat9 [7844]: #011at java.io.FileOutputStream.(FileOutputStream.java:162) 6 月 1 日 16:48:29 te tomcat9 [7844]: #011at java.io.FileWriter.(FileWriter.java:90) Jun 1 16:48:29 te tomcat9[7844]: #011at auap.AdaptiveAuthService.logWriter(AdaptiveAuthService.java:1134) 六月 1 16:48:29 te tomcat9 [7844]:#011at auap.AdaptiveAuthService.logWriter(AdaptiveAuthService.java:1151)
同一段代码在 Tomcat 8 中运行良好,但在 Tomcat 9 中却不行。
Tomcat 9 的安全增强是否有任何限制导致我的代码无法运行?
上面是我在 Tomcat8 和 Tomcat9 中创建文件的代码。
private static int logWriter(String data) {
Calendar now = Calendar.getInstance();
int year_file = now.get(Calendar.YEAR);
int month_file= (now.get(Calendar.MONTH) + 1);
int date_file = now.get(Calendar.DATE);
String filename = "LogAdaptive"+year_file+month_file+date_file;
File logFile = new File("/var/logs/tomcat9/"+filename+".txt");
boolean blnExists = logFile.exists();
FileWriter fw = null;
FileWriter fstream = null;
BufferedWriter bw = null;
BufferedWriter fbw = null;
try {
if (!blnExists) // if not exist, create file
{
fw = new FileWriter(logFile.getAbsoluteFile());
bw = new BufferedWriter(fw);
bw.write(data);
String newLine = System.getProperty("line.separator");
bw.write(newLine);
bw.close();
fw.close();
} else // if exist, amend file
{
fstream = new FileWriter(logFile, true);
fbw = new BufferedWriter(fstream);
fbw.write(data);
fbw.newLine();
fbw.close();
fstream.close();
}
} catch (Exception e) {
logWriter(EXCEPTION_STR0 + e);
} finally {
try {
if (fw != null){
fw.close();
}
} catch (IOException e) {
logWriter(EXCEPTION_IO + e);
}
try {
if (fstream != null){
fstream.close();
}
} catch (IOException e) {
logWriter(EXCEPTION_IO + e);
}
try {
if (bw != null){
bw.close();
}
} catch (IOException e) {
logWriter(EXCEPTION_IO + e);
}
try {
if (fbw != null){
fbw.close();
}
} catch (IOException e) {
logWriter(EXCEPTION_IO + e);
}
}
return 1;
} // end logWriter function
【问题讨论】:
-
异常信息是什么?
-
文件输出流