温馨提示:本文翻译自stackoverflow.com,查看原文请点击:spring boot - I get "(Read-only file system)" when try to write a file on HD
file io spring-boot ubuntu tomcat9

spring boot - 尝试在HD上写入文件时出现“(只读文件系统)”

发布于 2020-03-27 11:51:53

我想使用在Tomcat9上运行的springboot servlet在此路径中写入文件

/mnt/data-new/data/USERPROFILE/607/file.txt 

当我尝试写入该文件时,出现以下错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Jul 03 14:11:39 UTC 2019
There was an unexpected error (type=Internal Server Error, status=500).
/mnt/data-new/data/USERPROFILE/607/file.txt (Read-only file system)

我已经使用777在该路径上设置了权限,但没有任何更改。

Servlet代码非常简单:

@GetMapping("/")
public String index() throws IOException {
     FileWriter fw = new FileWriter("/mnt/data-new/data/USERPROFILE/607/file.txt");
     fw.write("File wrote");
     fw.close();
     return "OK";
}

我创建了一个文件名为“ file2.txt”的文件,该示例代码可以读取该文件:

@ResponseBody
@GetMapping("/read")
public String read() throws IOException {
    BufferedReader br = new BufferedReader(new FileReader("/mnt/data-new/data/USERPROFILE/607/file2.txt"));

    String st;
    while ((st = br.readLine()) != null) return st;
    return null;
}

我认为这是一项卡塔利娜政策,但似乎已被禁用:

root@devel-spring:/etc/tomcat9# echo $CATALINA_OPTS
-Djava.security.debug=all

而且,即使使用此选项,我也不会在“ / var / log / syslog”中获得安全管理器日志

我应该怎么做才能在该绝对路径中写入文件?

编辑:文件系统已挂载RW,我可以使用bash创建该文件。

查看更多

查看更多

提问者
Polletto
被浏览
199
Polletto 2019-07-04 00:10

感谢deman bugs.debian.org 链接中的 Emmanuel Bourg,我发现Tomcat9

“被沙箱记录,只能写入其自己的目录。您必须调整systemd配置,以允许Tomcat写入...”。

所以我纳米/etc/systemd/system/multi-user.target.wants/tomcat9.service添加

ReadWritePaths=/mnt/data-new/data/

然后

systemctl daemon-reload
service tomcat9 restart

现在,

使用上面的代码,我成功地编写了我的测试文件。