使用Java在Tomcat 9中创建文件时出错
我有一个以前在Tomcat8中运行的Java程序。使用Java在Tomcat 9中创建文件时出错,java,tomcat9,Java,Tomcat9,我有一个以前在Tomcat8中运行的Java程序。 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 = n
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
现在,我已经将我的Tomcat升级到了Tomcat 9,并且我发现程序在getAbsoluteFile
上给了我错误
我怀疑这个错误是因为文件夹的访问权限受到限制
但是,我已经将权限更改为chown root,chmod 777,但它仍然给出相同的错误-
open(FileOutputStream.java:270)Jun 1
16:48:29 T tomcat9[7844]:#011at
FileOutputStream.(FileOutputStream.java:213)6月1日
16:48:29 T tomcat9[7844]:#011at
FileOutputStream.(FileOutputStream.java:162)6月1日
16:48:29 T tomcat9[7844]:#011at
java.io.FileWriter.(FileWriter.java:90)Jun 1 16:48:29 te
tomcat9[7844]:#011at
auap.AdaptiveAuthService.logWriter(AdaptiveAuthService.java:1134)Jun
1 16:48:29 T tomcat9[7844]:#011at
日志编写器(AdaptiveAuthService.java:1151)
同一段代码在Tomcat8中运行良好,但在Tomcat9中却不行
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
我怀疑您会授予777对目录的访问权,但不会授予其子目录的访问权。使用
chmod-R 777
访问目录、子目录和目录中的文件
但是,通常我们不提供777访问权限。我们启动我们的应用程序过程,在您的情况下,它是tomcat启动脚本或服务,具有对所有依赖目录具有适当权限的适当用户
如果您使用“root”用户或任何具有访问从属目录适当权限的用户启动应用程序,您的问题将得到解决。什么是异常消息?文件输出流