@PathVariable出现点号"."时导致路径参数截断获取不全的解决办法

@PathVariable出现点号"."时导致路径参数截断获取不全的解决办法

问题实例:

@RequestMapping( value = "/file" )

参数声明:@RequestMapping(value = "{filename}", method = RequestMethod.GET)

传入参数:/file/some_file.32HIK345KHJKH.jpg

获取参数:/file/some_file.32HIK345KHJKH

参考链接:

http://stackoverflow.com/questions/3526523/spring-mvc-pathvariable-getting-truncated

http://stackoverflow.com/questions/7027366/spring-3-0-multiple-pathvariables-problem

http://forum.springsource.org/showthread.php?78085-Problems-with-RequestMapping&p=263563#post263563

http://forum.springsource.org/showthread.php?84738-UrlRewrite-PathVariable-multiple-dots-in-filename

Thread: UrlRewrite, @PathVariable multiple dots in filename

参考:http://forum.springsource.org/showthread.php?84738-UrlRewrite-PathVariable-multiple-dots-in-filename

UrlRewrite, @PathVariable multiple dots in filename

I have a "/file" - controller in a Spring REST web application which provides file streams. So a common scenario for a call is:/file/some_file.32HIK345KHJKH.jpg

I use the urlrewrite.xml from the Spring 3 REST example. For the file controller I added a special rule, with regex instead ofwildcards, like follows:

Code:

...

<rule match-type="regex">

  <from>^/file/([^/]*)$</from>

  <to>/app/file/$1</to>

</rule>

...

The controller handler code for the GET method looks like the following:

Code:

@RequestMapping(value = "{filename}", method = RequestMethod.GET)

public void provideFile(@PathVariable String filename,OutputStream outputStream) throws IOException {

...

// here I do sth. with the filename

...

}

Problem is that the original filename is truncated before last dot. This happens somewhere in between the URL call and the controller handler. So filename only contains: some_file.32HIK345KHJKH instead of some_file.32HIK345KHJKH.jpg

So my question is, what I'm doing wrong and why is the filename truncated at before last dot.

Cheers

Maik

Feb 15th, 2010, 12:14 PM#2

dvestal 

Junior Member

Join Date

Jun 2008

Location

Springfield, MO

Posts

21

The @PathVariable resolution will end at the period when using a String variable. You can specify a regex in your @PathVariable declaration though to be able to match your file patterns. I'd have to look at it more closely to determine the actual regex needed, but it shouldn't be difficult.

@PathVariable("{filename:[a-zA-Z0-9\.]+}") String filename

Feb 15th, 2010, 04:52 PM#3

Mammut 

Junior Member

Join Date

Feb 2010

Posts

2

@dvestal

Thank you dvestal. You pointed me in the right direction. I did further investigations and found this thread here in den forum:http://forum.springsource.org/showth...563#post263563

So the trick with the regualar expression override did the job.

Cheers

Maik


猜你喜欢

转载自jarg.iteye.com/blog/1597612
今日推荐