【问题标题】:How to write file to ubuntu linux server ? Java 11如何将文件写入 ubuntu linux 服务器?爪哇 11
【发布时间】:2021-10-08 00:39:56
【问题描述】:

我确实通过 Tomcat apache 将使用 spring 编写的 java Web 应用程序部署到了 ubuntu linux 服务器。 我创建了文件夹来将所有下载存储在 linux 服务器文件夹内的 tomcat 路径中: 路径:“opt/tomcat/apache-tomcat-9.0.46/work/downloads/”

当我尝试从外部 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)

【问题讨论】:

  • 您正在尝试输出到以 IP 地址开头的路径...?
  • UNC 路径仅适用于 Windows,但实际上不需要通过网络传输文件,因为目标文件夹是本地文件夹...
  • 目的地不是本地IP地址是外部机器(ubuntu服务器)
  • 目的地不是本地的,IP 地址是外部机器(ubuntu 服务器)—— 路径暗示了一个运行在目的地上的 Samba 服务器。有吗?
  • 这是最后一次尝试的堆栈跟踪: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.(FileOutputStream.java: 237) 在 java.base/java.io.FileOutputStream.(FileOutputStream.java:126) 在 com.hotels.river.controllers.RateHawkController.downloadDataDump(RateHawkController.java:1068)

标签: java linux file tomcat write


【解决方案1】:

您有权限问题:

  • 找出运行 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) 权限。

【讨论】:

  • 那么如何打开对这些目录的写入权限?
  • 检查tutorial on UNIX permissions。由于您手动安装了 Tomcat 而不是使用 Ubuntu 的软件包,因此很难猜测 Tomcat 使用的是什么凭据。如果它使用用户tomcat,组tomcat 一个chown tomcat:tomcat -R /opt/tomcat/apache-tomcat-9.0.46/workfind /opt/tomcat/apache-tomcat-9.0.46/work -type d chown 750 {} + 就可以了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 2020-08-06
  • 2016-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
相关资源
最近更新 更多