dp9599 2017-09-25 11:03
浏览 114
已采纳

目录在golang http.FileServer()中列出了错误的链接

I'm using http.FileServer in Go to serve some static file into a directory.

This is how I map it using mux as a router:

r.PathPrefix("/download").Handler(http.StripPrefix("/download", http.FileServer(http.Dir(dirPath)))).Methods("GET")

where dirPath is an absolute path of a directory in my file system.

Now it seems to work fine when asking the directory listing with localhost:8080/download, because it returns a page like this

<pre>
<a href="a.xml">a.xml</a>
<a href="b.xml">b.zip</a>
</pre>

Unfortunately the links are broken because I expect them to be mapped, for example to localhost:8080/download/a.xml , while file server maps them to localhost:8080/a.xml.

How can I make my directory listing keep the /download path prefix in links?

  • 写回答

1条回答 默认 最新

  • dongsu1539 2017-09-25 11:21
    关注

    The problem is the pattern you register you handler with: "/download".

    There are 2 problems with it:

    1. The generated URLs are wrong because the handler returned by the http.FileServer() function generates relative URLs to files and subfolders; relative to the root folder passed to http.FileServer(), and if your page is available under the path /download, a relative URL like href="a.xml" will be resolved to /a.xml, and not to /download/a.xml.

    2. Even if the URLs would be good, the files would not be served as the requests would not get routed to your handled (to the file server handler). You must add a trailing slash, as "/download" only matches this single path, and not all paths starting with it. Add a trailing slash: "/download/" and it will match the rooted subtree /download/*.

    So the solution is:

    r.PathPrefix("/download/").Handler(
        http.StripPrefix("/download", http.FileServer(http.Dir(dirPath))),
    ).Methods("GET")
    

    This is documented at http.ServeMux:

    Patterns name fixed, rooted paths, like "/favicon.ico", or rooted subtrees, like "/images/" (note the trailing slash).

    Note that even though we're now using the "/download/" registered path, users are not required to type the trailing slash in the browser, as leaving that out the server will send a redirect to the path that ends with a trailing slash. This will happen automatically. This is also documented at http.ServeMux:

    If a subtree has been registered and a request is received naming the subtree root without its trailing slash, ServeMux redirects that request to the subtree root (adding the trailing slash). This behavior can be overridden with a separate registration for the path without the trailing slash. For example, registering "/images/" causes ServeMux to redirect a request for "/images" to "/images/", unless "/images" has been registered separately.

    Read related question: Go web server is automatically redirecting POST requests

    Here's a simple file server app using only the standard library:

    http.Handle("/dl/",
        http.StripPrefix("/dl", http.FileServer(http.Dir("/home/bob/Downloads"))),
    )
    panic(http.ListenAndServe("localhost:8080", nil))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?