dromara / hutool

🍬A set of tools that keep Java sweet.
https://hutool.cn
Other
28.91k stars 7.48k forks source link

简易服务器搭建无效 #3635

Closed yuf19-2022 closed 2 months ago

yuf19-2022 commented 2 months ago

版本情况 5.8.28

JDK版本: oracle-openjdk-17.0.11 hutool版本: 5.8.28(请确保最新尝试是否还有问题)

问题描述(包括截图)

image

  1. 复现代码
HttpUtil.createServer(8888)
    // 设置默认根目录
    .setRoot("D:\\workspace\\site\\hutool-site")
    .start();
  1. 堆栈信息

  2. 测试涉及到的文件(注意脱密)

比如报错的Excel文件,有问题的图片等。

looly commented 2 months ago

检查:

  1. 必须在main方法中运行,不能使用单元测试运行
  2. 看是否有报错,端口是否被占用。

有问题补充再打开此issue。

yuf19-2022 commented 2 months ago

检查:

  1. 必须在main方法中运行,不能使用单元测试运行
  2. 看是否有报错,端口是否被占用。

有问题补充再打开此issue。

就是在main方法中调用的,浏览器访问不了,用idea新建的maven项目在Main类中的main方法调用的

yuf19-2022 commented 2 months ago

检查:

  1. 必须在main方法中运行,不能使用单元测试运行
  2. 看是否有报错,端口是否被占用。

有问题补充再打开此issue。

刚开始访问还出现标题 image 后面就什么都没有了

yuf19-2022 commented 2 months ago

检查:

  1. 必须在main方法中运行,不能使用单元测试运行
  2. 看是否有报错,端口是否被占用。

有问题补充再打开此issue。

用nginx试,老是把http变成https,其他的静态网站没有啊

looly commented 2 months ago
/**
 * http://localhost:8888/?name=hutool
 */
public class Issue3343Test {
    public static void main(final String[] args) {
        final SimpleServer server = HttpUtil.createServer(8888)
        server.addAction("/", Issue3343Test::index);
        server.start();
    }

    private static void index(HttpServerRequest request, HttpServerResponse response) {
        // 具体逻辑中再次获取请求参数
        ListValueMap<String, String> params = request.getParams();
        Console.log("index params: " + params);
        response.getWriter().write("GOT: " + params);
    }
}

试下不使用root,单独使用action看可否。

yuf19-2022 commented 2 months ago
/**
 * http://localhost:8888/?name=hutool
 */
public class Issue3343Test {
  public static void main(final String[] args) {
      final SimpleServer server = HttpUtil.createServer(8888)
      server.addAction("/", Issue3343Test::index);
      server.start();
  }

  private static void index(HttpServerRequest request, HttpServerResponse response) {
      // 具体逻辑中再次获取请求参数
      ListValueMap<String, String> params = request.getParams();
      Console.log("index params: " + params);
      response.getWriter().write("GOT: " + params);
  }
}

试下不使用root,单独使用action看可否。

我的问题,路径写的不对,我直接用的hutool-site,应该是它的子文件夹docs,只是用nginx在edge浏览器老是给我把http变成https导致访问不了,在别的浏览器OK了,不知道之前路径写错用edge浏览器有缓存的问题

Mr-LiuDC commented 1 month ago

同问,我试了下面两种方式设置root目录,IDEA里面启动程序能正常访问到static目录下的资源,打包成可执行 jar 就不行了

// server.setRoot("src/main/resources/static");
URL url = ResourceUtil.getResource("static");
server.setRoot(url.getPath());

应该是没法正确识别 jar 包的路径

2024-07-12 15:27:00.279 ERROR [pool-2-thread-1] org.example.filter.TraceIdFilter        : Exception occurred when processing request from 0:0:0:0:0:0:0:1 to /favicon.ico
java.nio.file.InvalidPathException: Illegal char <:> at index 4: file:\D:\developer\code\codeup\aipark\study-only\java-study\hutool-usage-example\target\hutool-usage-example-1.0.0-SNAPSHOT-jar-with-dependencies.jar!\static
        at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:204) ~[?:?]
        at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:175) ~[?:?]
        at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) ~[?:?]
        at sun.nio.fs.WindowsPath.parse(WindowsPath.java:92) ~[?:?]
        at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:231) ~[?:?]
        at java.io.File.toPath(File.java:2401) ~[?:?]
        at cn.hutool.core.io.FileUtil.isSub(FileUtil.java:3592) ~[hutool-usage-example-1.0.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at cn.hutool.core.io.FileUtil.checkSlip(FileUtil.java:3527) ~[hutool-usage-example-1.0.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at cn.hutool.core.io.FileUtil.file(FileUtil.java:340) ~[hutool-usage-example-1.0.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at cn.hutool.http.server.action.RootAction.doAction(RootAction.java:68) ~[hutool-usage-example-1.0.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at cn.hutool.http.server.handler.ActionHandler.handle(ActionHandler.java:44) ~[hutool-usage-example-1.0.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:98) ~[jdk.httpserver:?]
        at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:82) ~[jdk.httpserver:?]
        at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:101) ~[jdk.httpserver:?]
        at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:871) ~[jdk.httpserver:?]
        at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:98) ~[jdk.httpserver:?]
        at org.example.filter.TraceIdFilter.doFilter(TraceIdFilter.java:35) ~[hutool-usage-example-1.0.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at cn.hutool.http.server.SimpleServer$1.doFilter(SimpleServer.java:130) ~[hutool-usage-example-1.0.0-SNAPSHOT-jar-with-dependencies.jar:?]
        at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:101) ~[jdk.httpserver:?]
        at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:847) ~[jdk.httpserver:?]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572) ~[?:?]
        at java.util.concurrent.FutureTask.run(FutureTask.java:317) ~[?:?]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) ~[?:?]
        at java.lang.Thread.run(Thread.java:1583) [?:?]

image

looly commented 1 month ago

@Mr-LiuDC 不要使用相对路径,否则会去jar包里找路径的,使用绝对路径吧。

Mr-LiuDC commented 1 month ago

@looly 感谢你的回复, 我知道使用绝对路径可以,但也仅限于本地调试,我想要的效果是可以从jar包里面找。这对于可执行jar非常有用,特别是静态HTML需要和Java代码频繁更新的时候。