在tomcat服务器中,文件是“未找到文件的途径”,而在psvm中可以找到。

发布于 2025-02-12 05:31:25 字数 3214 浏览 4 评论 0原文

在基本的爪哇我有一个非常奇怪的问题, 在同一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.<init>(FileInputStream.java:157)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
    at java.base/java.io.FileReader.<init>(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();
    }

仅使用绝对路径才能使用。 但是仍然没有其他任何东西。这是我的目录结构

In basic Java I have a very odd issue,
In the same java class I have a method and a PVSM containing the same info:

    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"));

Above it finds it without issue, but when I try and run it within a method it reports it cannot find it. Errorcode at the bottom:

    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.<init>(FileInputStream.java:157)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
    at java.base/java.io.FileReader.<init>(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)

I have hardcoded the location in now for easier testing:

    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();
    }

Litterally only using absolute path seems to work.. !
But still no luck in anything else.. here is my directory structure
Directory tree

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早茶月光 2025-02-19 05:31:25

您必须确保SRC是sendmailWithToken的项目的存储库根。

You have to ensure that src is the repository root for the project in which sendMailWithToken is called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文