InvalidPathException: Malformed input or input contains unmappable characters 中文名文件 linux存储失败
作者:mmseoamin日期:2023-12-20

InvalidPathException: Malformed input or input contains unmappable characters 中文名文件 linux存储失败

使用java.nio.file.Path规范并解析路径,生成文件到本地,部分文件名带中文。提供对外下载文件接口,用于下载这些文件,下载后的文件要显示中文名。本地(windows)开发调试都OK。通过docker容器部署到开发环境(linux集群)后,调用文件生成接口,后台报错,报错信息如下:

java.nio.file.InvalidPathException: Malformed input or input contains unmappable characters: ????98?5??????????.doc
 	at sun.nio.fs.UnixPath.encode(UnixPath.java:147)
 	at sun.nio.fs.UnixPath.(UnixPath.java:71)
 	at sun.nio.fs.UnixFileSystem.getPath(UnixFileSystem.java:281)
 	at java.io.File.toPath(File.java:2273)
 	at org.springframework.core.io.FileSystemResource.(FileSystemResource.java:103)
 	at com.sx.system.controller.FileController.uploadFile$original$nBURC4QV(FileController.java:80)
 	at com.sx.system.controller.FileController.uploadFile$original$nBURC4QV$accessor$6dFBGKAE(FileController.java)
 	at com.sx.system.controller.FileController$auxiliary$hKLUE52q.call(Unknown Source)
 	at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:86)
 	at com.sx.system.controller.FileController.uploadFile(FileController.java)
 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 	at java.lang.reflect.Method.invoke(Method.java:498)
 	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)

解决办法 通过设置java环境参数来解决,若是docker部署的,有KubeSphere,则编辑yaml文件 添加即可

JAVA_TOOL_OPTIONS=‘-Dfile.encoding=“UTF-8” -Dsun.jnu.encoding=“UTF-8”’