在get请求中传递名称中带有空格的文件名

问题描述 投票:0回答:1

我有两份文件。一个是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. 或改变 <source src= <%= "'../../videos/" + video + "'" %> type='video/mp4'>
java filenames get-request
1个回答
0
投票

很抱歉,如果我不明白为什么你将这个问题标记为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)可以帮助您完成此任务

© www.soinside.com 2019 - 2024. All rights reserved.