tomcat 如何写文件到ubuntu linux服务器?Java 11

t9aqgxwy  于 2022-11-13  发布在  Linux
关注(0)|答案(1)|浏览(134)

我确实通过Tomcat apache将用spring编写的java web应用程序部署到ubuntu linux服务器上。我创建了一个文件夹来存储linux服务器文件夹中tomcat路径下的所有下载:文件名:“opt/tomcat/apache-tomcat-9.0.46/work/downloads/“(文件名:opt/tomcat/apache-tomcat-9.0.46/work/downloads/apache-tomcat-9.0.46),文件名:”文件名:“文件名:”
当我尝试从外部API下载文件到ubuntu目录(以上路径)时,我收到错误500外部,这是由于服务器找不到路径造成的。
方法代码:( java )

@RequestMapping(value = "/downloadDataDump", method = RequestMethod.GET)
public String downloadDataDump(@RequestParam(value = "fileName") String fileName) throws IOException {

    String fileUrl = null;
    URL url = null;
    HttpURLConnection con = null;
    try {
        fileUrl = getHotelDataDumpUrl("all", "en").getBody().getData().getURL();
        url = new URL(fileUrl);
        con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty("Accept-Encoding", "zstd");
    } catch (IOException e) {
        e.printStackTrace();
        } catch (ParseException e) {
        e.printStackTrace();
    }
    FileOutputStream fout = null;
    ZstdInputStream reader = null;
    try {
        reader = new ZstdInputStream(con.getInputStream());
        fout = new FileOutputStream("//212.102.105.18/opt/tomcat/apache-tomcat-9.0.46/work/downloads" + fileName);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

堆栈追踪(来自注解):

java.io.FileNotFoundException: /opt/tomcat/apache-tomcat-9.0.46/work/downloads/hotelNew (Permission denied)
   at java.base/java.io.FileOutputStream.open0(Native Method)
   at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
   at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
   at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:126)
   at com.hotels.river.controllers.RateHawkController.downloadDataDump(RateHawkController.java:1068)
vwkv1x7d

vwkv1x7d1#

您有权限问题:

  • 找出Tomcat以哪个用户身份运行:它是java进程的用户
  • 以Tomcat用户身份打开一个shell(假设它是tomcat):
sudo -u tomcat /bin/bash
  • 尝试创建文件:
touch /opt/tomcat/apache-tomcat-9.0.46/work/downloads/hotelNew

要创建文件,Tomcat需要对所有目录的遍历(x)权限和对downloads文件夹的写入(w)权限。

相关问题