Java Spring-使用UTF-8从文件加载

Java Spring-使用UTF-8从文件加载,java,spring,maven,spring-mvc,utf-8,Java,Spring,Maven,Spring Mvc,Utf 8,我在使用Spring加载UTF-8文件时遇到问题 这就是我的工作: 我有一个属性文件,与此内容一起保存为UTF-8 global.variable.try=This is product variable cache.location.filename.regions=regions hacky.carky=éíáščýéíšž hehe haha hoho +íšářá 在我的控制器中,我通过两种方式访问它 @Controller @RequestMapping(value = "/aser

我在使用Spring加载UTF-8文件时遇到问题

这就是我的工作:

我有一个属性文件,与此内容一起保存为UTF-8

global.variable.try=This is product variable
cache.location.filename.regions=regions
hacky.carky=éíáščýéíšž hehe haha hoho +íšářá
在我的控制器中,我通过两种方式访问它

@Controller
@RequestMapping(value = "/aserver")
public class AServerController {

@Value("${hacky.carky}")
    private String hackyCarky;  

    @RequestMapping(value = "/hackycarky", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Object hackycarky(ServletRequest servletRequest, ServletResponse response) throws MalformedURLException, IOException{
        return hackyCarky;
    }   

    @RequestMapping(value = "/regions", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Object regions(ServletRequest servletRequest, ServletResponse response) throws MalformedURLException, IOException{
        String filePath = "c:\\prace\\eclipse workspace\\czechtraditions\\server\\src\\main\\resources\\server-general.properties";
        return new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);     
    }   
}
如果我访问/aserver/hackycarky,它将提供所需的输出:

éíáščýéíšž hehe haha hoho +íšářá
但是,如果我访问/aserver/regions,则输出如下:

global.variable.try=This is product variable
cache.location.filename.regions=regions
hacky.carky=���??���?? hehe haha hoho +�?�?�
PS:我不需要访问属性文件,这只是一个测试用例,可以肯定的是,该文件的格式是正确的-因此使用@Value${hacky.carky}可以正常工作

响应头在这两种情况下都是相同的,具有此属性

Content-Type:application/json;charset=UTF-8
我在web.xml中设置了utf-8的过滤器映射:

<filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/rest/*</url-pattern>
</filter-mapping>
我在pom.xml中为maven设置了utf-8:

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
完整地址的示例是http://localhost:8080/czechtraditions-服务器/rest/A服务器/区域已解决

我不会将文件转换为字符串,而是发送字节数组,让客户机根据头文件中的内容类型来决定如何处理它,这样就可以了

@RequestMapping(value = "/regions", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object regions(ServletRequest servletRequest, ServletResponse response) throws MalformedURLException, IOException{
    String filePath = "c:\\prace\\eclipse workspace\\czechtraditions\\server\\src\\main\\resources\\server-general.properties";
    return Files.readAllBytes(Paths.get(filePath));     
}