Permission Denied Error when Creating file in Java/Springboot – Tomcat

huangapple go评论115阅读模式
英文:

Permission Denied Error when Creating file in Java/Springboot - Tomcat

问题

我正在尝试在Java/SpringBoot中创建一个文件,并使用FileWriter方法写入它。以下是我的代码。

String filename = "file.csv";
LOGGER.info("Creating File CSV");
File file = new File(filename);
Boolean isExists = file.exists();
boolean isExistsAbs = file.getAbsoluteFile().exists();
LOGGER.info("CSV File Exists? : " + isExists);
LOGGER.info("CSV File ABSExists? : " + isExistsAbs);
boolean setWritable = file.setWritable(true);
LOGGER.info("SetWritable CSV Output : " + Boolean.toString(setWritable));
boolean setExecutable = file.setExecutable(true);
LOGGER.info("SetExecutable CSV Output : " + Boolean.toString(setExecutable));
LOGGER.info("before file Writer CSV");
FileWriter fileWriter = new FileWriter(filename);
LOGGER.info("after file Writer CSV");
LOGGER.info("Writing File CSV");
String content = "Some CSV Content";
fileWriter.write(content);
LOGGER.info("Written to File CSV");
fileWriter.close();
LOGGER.info("Closing File CSV");

当我在本地系统中执行此代码时,它可以正常工作,并且以下是代码的日志:

2020-07-28 00:56:17.204  INFO 15956 --- Controller       : Creating File CSV
2020-07-28 00:56:17.205  INFO 15956 --- Controller       : CSV File Exists? : true
2020-07-28 00:56:17.206  INFO 15956 --- Controller       : CSV File ABSExists? : true
2020-07-28 00:56:17.206  INFO 15956 --- Controller       : SetWritable CSV Output : true
2020-07-28 00:56:17.207  INFO 15956 --- Controller       : SetExecutable CSV Output : true
2020-07-28 00:56:17.207  INFO 15956 --- Controller       : before file Writer CSV
2020-07-28 00:56:17.208  INFO 15956 --- Controller       : after file Writer CSV
2020-07-28 00:56:17.209  INFO 15956 --- Controller       : Writing File CSV
2020-07-28 00:56:17.209  INFO 15956 --- Controller       : Written to File CSV
2020-07-28 00:56:17.210  INFO 15956 --- Controller       : Closing File CSV

但是,当我在部署到Tomcat服务器后执行相同的代码时,我收到以下日志/错误:

2020-07-27 14:34:15.465  INFO 17902 --- Controller       : Creating File CSV
2020-07-27 14:34:15.465  INFO 17902 --- Controller       : CSV File Exists? : false
2020-07-27 14:34:15.465  INFO 17902 --- Controller       : CSV File ABSExists? : false
2020-07-27 14:34:15.466  INFO 17902 --- Controller       : SetWritable CSV Output : false
2020-07-27 14:34:15.466  INFO 17902 --- Controller       : SetExecutable CSV Output : false
2020-07-27 14:34:15.466  INFO 17902 --- Controller       : before file Writer CSV
2020-07-27 14:34:15.488 ERROR 17902 --- [http-nio-8080-exec-31] o.s.b.w.servlet.support.ErrorPageFilter  : Forwarding to error page from request [/api/getFlighPlanCSV] due to exception [file.csv (Permission denied)]

java.io.FileNotFoundException: file.csv (Permission denied)
	at java.base/java.io.FileOutputStream.open0(Native Method) ~[na:na]
	at java.base/java.io.FileOutputStream.open(FileOutputStream.java:291) ~[na:na]
	at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:234) ~[na:na]
	at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:123) ~[na:na]
	at java.base/java.io.FileWriter.<init>(FileWriter.java:66) ~[na:na]
	at com.aiknights.com.rapidviewmain.controller.SiteInspectionController.DownloadFlightPlan(SiteInspectionController.java:175) ~[classes/:0.0.1-SNAPSHOT]
	...

看起来在写入文件时出现错误,指出找不到文件,似乎是由于权限问题导致应用程序/.war无法在当前工作目录中创建文件。

我的应用程序名称是 - applicationmain.war,位于 - /opt/tomcat/webapps/applicationmain.war,每当我部署war文件时都会创建一个文件夹 - /opt/tomcat/webapps/applicationmain。我已尝试将所有权限分配给war文件本身和部署war文件时创建的文件夹,但没有成功。

如果需要更多关于设置和代码的详细信息,我会回答评论。

任何形式的帮助将不胜感激,因为我已经花了太多时间处理这个问题。

英文:

I am trying to create a File in Java/SpringBoot and writing to it using FileWriter Method.
Below is my Code.

                String filename = file.csv;
				LOGGER.info(&quot;Creating File CSV&quot;);
				File file = new File(filename);
				Boolean Isexists =file.exists();
				boolean isexistsabs = file.getAbsoluteFile().exists();
				LOGGER.info(&quot;CSV File Exists? : &quot;+Isexists);
				LOGGER.info(&quot;CSV File ABSExists? : &quot;+isexistsabs);
				boolean setwritable = file.setWritable(true);
				LOGGER.info(&quot;SetWritable CSV Output : &quot;+Boolean.toString(setwritable));
				boolean setExecutable = file.setExecutable(true);
				LOGGER.info(&quot;SetExecutable CSV Output : &quot;+Boolean.toString(setExecutable));
				LOGGER.info(&quot;before file Writer CSV&quot;);
				FileWriter filewriter = new FileWriter(filename);
				LOGGER.info(&quot;after file Writer CSV&quot;);
				LOGGER.info(&quot;Writing File CSV&quot;);
				String content =&quot;Some CSV Content&quot;;
				filewriter.write(content);
				LOGGER.info(&quot;Written to File CSV&quot;);
				filewriter.close();
				LOGGER.info(&quot;Closing File CSV&quot;);

When I execute the code in my local system it is working and below is the log of the code

2020-07-28 00:56:17.204  INFO 15956 --- Controller       : Creating File CSV
2020-07-28 00:56:17.205  INFO 15956 --- Controller       : CSV File Exists? : true
2020-07-28 00:56:17.206  INFO 15956 --- Controller       : CSV File ABSExists? : true
2020-07-28 00:56:17.206  INFO 15956 --- Controller       : SetWritable CSV Output : true
2020-07-28 00:56:17.207  INFO 15956 --- Controller       : SetExecutable CSV Output : true
2020-07-28 00:56:17.207  INFO 15956 --- Controller       : before file Writer CSV
2020-07-28 00:56:17.208  INFO 15956 --- Controller       : after file Writer CSV
2020-07-28 00:56:17.209  INFO 15956 --- Controller       : Writing File CSV
2020-07-28 00:56:17.209  INFO 15956 --- Controller       : Written to File CSV
2020-07-28 00:56:17.210  INFO 15956 --- Controller       : Closing File CSV

But when I execute the same after deploying to a tomcat server below is the log/error I am recieveing

2020-07-27 14:34:15.465  INFO 17902 --- Controller       : Creating File CSV
2020-07-27 14:34:15.465  INFO 17902 --- Controller       : CSV File Exists? : false
2020-07-27 14:34:15.465  INFO 17902 --- Controller       : CSV File ABSExists? : false
2020-07-27 14:34:15.466  INFO 17902 --- Controller       : SetWritable CSV Output : false
2020-07-27 14:34:15.466  INFO 17902 --- Controller       : SetExecutable CSV Output : false
2020-07-27 14:34:15.466  INFO 17902 --- Controller       : before file Writer CSV
2020-07-27 14:34:15.488 ERROR 17902 --- [http-nio-8080-exec-31] o.s.b.w.servlet.support.ErrorPageFilter  : Forwarding to error page from request [/api/getFlighPlanCSV] due to exception [file.csv (Permission denied)]

java.io.FileNotFoundException: file.csv (Permission denied)
	at java.base/java.io.FileOutputStream.open0(Native Method) ~[na:na]
	at java.base/java.io.FileOutputStream.open(FileOutputStream.java:291) ~[na:na]
	at java.base/java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:234) ~[na:na]
	at java.base/java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:123) ~[na:na]
	at java.base/java.io.FileWriter.&lt;init&gt;(FileWriter.java:66) ~[na:na]
	at com.aiknights.com.rapidviewmain.controller.SiteInspectionController.DownloadFlightPlan(SiteInspectionController.java:175) ~[classes/:0.0.1-SNAPSHOT]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.1.RELEASE.jar:5.2.1.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.1.RELEASE.jar:5.2.1.RELEASE]

It seems to be erroring out when writing into the file saying File not found, seems like the application/.war is not able to create a file in the PWD because of the permission issue.

My application name - applicationmain.war
resides at - /opt/tomcat/webapps/applicationmain.war
there is also a folder created whenever I deploy the war file - /opt/tomcat/webapps/applicationmain
I tried assiging all permissions to both the war file itself and the folder that is created when deploying the war file with no luck

I will answer to any comments asking for more details about the setup and code.

Any kind of help would be really appreciated as I already spent too many hours on dealing with this.

答案1

得分: 2

确保您对所有父目录具有读取和执行权限。

示例:

chmod o+x /opt
英文:

Ensure that you have read and execute access to all parent directories as well.

Example:

chmod o+x /opt

huangapple
  • 本文由 发表于 2020年7月28日 03:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63122520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定