英文:
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("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");
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.<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]
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论