【问题标题】:passing a filename with a space in the name in a get request在获取请求中传递名称中带有空格的文件名
【发布时间】:2019-08-12 16:30:50
【问题描述】:

我有一个文件的两个副本。一个是 CatBoarding.mp4,另一个是 Cat Boarding.mp4

当我在 chrome 中执行时:http://localhost:8080/VT/Pages/jspDynamic/dynamic.jsp?video=CatBoarding.mp4 工作正常。

但是当我尝试时:

  1. http://localhost:8080/VT/Pages/jspDynamic/dynamic.jsp?video="Cat Boarding.mp4"
  2. http://localhost:8080/VT/Pages/jspDynamic/dynamic.jsp?video='Cat Boarding.mp4'
  3. http://localhost:8080/VT/Pages/jspDynamic/dynamic.jsp?video=Cat Boarding.mp4
  4. http://localhost:8080/VT/Pages/jspDynamic/dynamic.jsp?video=Cat%20Boarding.mp4

找不到文件。我确实注意到浏览器在每种情况下都会为空格插入 %20

@the_storyteller 发现问题不在于我如何将参数传递给 jsp,而在于我如何在 jsp 中使用参数。原来我们在jsp文件里面有:

<source src= <%= "../../videos/" + video %> type='video/mp4'>

这最终看起来像:

<source src= videos/Cat Boarding.mp4 type='video/mp4'>

有两种简单的修复方法:

  1. 在jsp文件中添加video = video.replace(" ","%20");
  2. 或改变

    &lt;source src= &lt;%= "'../../videos/" + video + "'" %&gt; type='video/mp4'&gt;

【问题讨论】:

  • URL 不能有空格,所以它们被替换为 %20,这是该字符的字符代码。
  • 太棒了。但该空间作为获取请求的一部分位于参数中。你是说文件名中不能有空格?
  • 他们可以有空格 - %20 是一个编码的空格。 url 将与空格一起发送,服务器(在本例中为笔记本电脑中的网络浏览器)将解码参数并将%20 转换回空格。请参阅本文档解释编码。 w3schools.com/tags/ref_urlencode.asp
  • 谢谢,但我对您的回复感到困惑。我可以传递一个包含空格的文件名吗?如果是,怎么做?
  • @the_storyteller,你的最终见解是正确的!谢谢你。当我添加 video = video.replace(" ","%20");在 type='video/mp4'> 之前,它就像一个魅力。

标签: java filenames get-request


【解决方案1】:

对不起,如果我不明白您为什么将此问题标记为 java 问题,希望这会有所帮助:

请记住,URL/URN 中的“空格”被视为不安全字符。 RFC1630 说:

  There is a conflict between the need to be able to represent many
  characters including spaces within a URI directly, and the need to
  be able to use a URI in environments which have limited character
  sets or in which certain characters are prone to corruption.  This
  conflict has been resolved by use of an hexadecimal escaping
  method which may be applied to any characters forbidden in a given
  context.  When URLs are moved between contexts, the set of
  characters escaped may be enlarged or reduced unambiguously.

  The use of white space characters is risky in URIs to be printed
  or sent by electronic mail, and the use of multiple white space
  characters is very risky.  This is because of the frequent
  introduction of extraneous white space when lines are wrapped by
  systems such as mail, or sheer necessity of narrow column width,
  and because of the inter-conversion of various forms of white
  space which occurs during character code conversion and the
  transfer of text between applications.  This is why the canonical
  form for URIs has all white spaces encoded.

所以我想如果您需要在代码中使用“有风险”或“不明确”的 URI,只需将有风险的字符替换为它们的百分比编码代码(%20 代表空格,%3C 代表“

解决此问题的另一种方法是,在您的情况下,使用更安全的名称“slug”保存您的视频文件,用户和系统(如搜索引擎或 http 客户端)更容易阅读。

例如,法语标题使用可能导致相同问题的字符(é、è、î、à、ç 等)。使用 slug,您可以在将文件保存到服务器之前更改文件,所有“空格”都会出现您选择的字符(例如“-”或“_”)。

java中的Slugify(com.github.slugify)可以帮你做到这一点

【讨论】:

    猜你喜欢
    • 2020-03-27
    • 2019-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多