0

我知道以前有人问过这个问题。然而,尽管按照建议做了所有事情,我仍然面临一个问题。

如果我的路径变量有 .jpg 扩展名,它会被截断。这是我的设置

 <mvc:annotation-driven
        content-negotiation-manager="contentNegotiationManager">
        <mvc:message-converters>
            <bean
                class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>image/jpeg</value>
                        <value>image/jpg</value>
                        <value>image/png</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>

    </mvc:annotation-driven>

    <bean id="contentNegotiationManager"
        class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="favorPathExtension" value="false" />
    </bean>

<bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <util:list>
                <ref bean="jsonMessageConverter" />
            </util:list>
        </property>
    </bean>

    <bean name="exceptionHandlerExceptionResolver"
        class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver">
        <property name="order" value="0" />
        <property name="contentNegotiationManager" ref="contentNegotiationManager" />
    </bean>

    <bean name="handlerMapping"
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
        <property name="contentNegotiationManager" ref="contentNegotiationManager" />
        <property name="useSuffixPatternMatch" value="false" />
        <property name="useTrailingSlashMatch" value="true"></property>
    </bean>

控制器

@RequestMapping(value = "image/{fileName}", method = RequestMethod.GET, produces = { MediaType.IMAGE_JPEG_VALUE,
            MediaType.IMAGE_GIF_VALUE, MediaType.IMAGE_PNG_VALUE })
    public @ResponseBody
    byte[] getImage(@PathVariable("fileName") final String fileName);

我需要做更多吗?

4

1 回答 1

0

试试这个(注意更新value@RequestMapping

@RequestMapping(value = "image/{fileName:[a-z]\\.[a-z]}}", method = RequestMethod.GET, produces = { MediaType.IMAGE_JPEG_VALUE,
        MediaType.IMAGE_GIF_VALUE, MediaType.IMAGE_PNG_VALUE })
public @ResponseBody
byte[] getImage(@PathVariable("fileName") final String fileName);

请参阅此处的参考。

于 2014-11-26T11:23:45.660 回答