返回

java-在 tomcat 服务器中,文件是“找不到文件的路径”,而在 psvm 中找到了

发布时间:2022-07-04 23:30:59 243
# php

在基本的 Java 中,我有一个非常奇怪的问题,在同一个 java 类中,我有一个方法和一个包含相同信息的 PVSM:

    public static void main(String[] args) throws IOException, URISyntaxException {
        System.out.println(htmlToString("src/main/Java/archive/trawler/webservices/emailHTMLTemplates/resetMail.html"));
        System.out.println(htmlToString("src\\main\\Java\\archive\\trawler\\webservices\\emailHTMLTemplates\\resetMail.html"));

上面它发现它没有问题,但当我尝试在一个方法中运行它时,它报告它找不到它。底部的错误代码:

    public static boolean sendMailWithToken(String sendTo, String subjectLine, String token) throws FileNotFoundException {
            String htmlMessage = htmlToString("src\\main\\Java\\archive\\trawler\\webservices\\emailHTMLTemplates\\resetMail.html");
java.io.FileNotFoundException: src\main\Java\archive\trawler\webservices\emailHTMLTemplates\resetMail.html (The system cannot find the path specified)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
    at java.base/java.io.FileInputStream.(FileInputStream.java:157)
    at java.base/java.io.FileInputStream.(FileInputStream.java:111)
    at java.base/java.io.FileReader.(FileReader.java:60)
    at archive.trawler.webservices.SendEmail.htmlToString(SendEmail.java:161)
    at archive.trawler.webservices.SendEmail.sendMailWithToken(SendEmail.java:121)
    at archive.trawler.webservices.accountResetResource.resetWachtWoord(accountResetResource.java:28)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
[...]
at setup.HttpsFilter.doFilter(HttpsFilter.java:28)
at javax.servlet.http.HttpFilter.doFilter(HttpFilter.java:57)

为了便于测试,我现在已经对位置进行了硬编码:

    public static String htmlToString(String htmlFileFromRoot) throws IOException {
        StringBuilder contentBuilder = new StringBuilder();
        try {
//            BufferedReader in = new BufferedReader(new FileReader(htmlFileFromRoot));
            BufferedReader in = new BufferedReader(new FileReader("/resetmail.html"));
            String str;
            while ((str = in.readLine()) != null) {
                contentBuilder.append(str);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return contentBuilder.toString();
    }

一般来说,只使用绝对路径似乎是可行的!但在其他方面仍然没有运气.........这是我的目录结构Directory tree

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像