我有一个使用Spring Boot的项目,我正在尝试上载包含特殊字符 ie的csv文件: “ΕΛΙΑΚΑΤΑΣΚΕΥΑΣΤΙΚΗΑ.Ε。” “ ???? ??????????????????。?。”
控制器
@RequestMapping(value = "/processfile", method = RequestMethod.POST)
public void processMultiPartFile(HttpServletRequest request, @RequestParam("files") MultipartFile[] files) {
for (MultipartFile file : files) {
System.out.println("File data-->>"+new String(file.getBytes(),"UTF-8"))
}
}
尝试了以下解决方案,但到目前为止没有成功:
解决方案1:在请求中设置字符编码
request.setCharacterEncoding("UTF-8");
解决方案2:使用mutipartfile的inputstreamreader
BufferedReader in = new BufferedReader(new InputStreamReader(file.getInputStream(), "UTF-8"));
String line = null;
while((line = in.readLine()) != null) {
System.out.println("read data----->"+line);
}
解决方案3:尝试使用CommonsMultipartResolver
exclude auto configuration by
@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})
and add custom resolver
@Bean(name="multipartResolver")
public CommonsMultipartResolver multipartResolver(){
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("UTF-8");
return resolver;
}
并在pom.xml中添加以下依赖项
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
应用环境
注意:在本地系统上的eclispse(Luna)IDE中运行应用程序时,会在控制台中获得正确的输出。