异常
java.io.FileNotFoundException: Byte array resource [resource loaded from byte array] cannot be resolved to absolute file path
at org.springframework.core.io.AbstractResource.getFile(AbstractResource.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.alibaba.fastjson.util.FieldInfo.get(FieldInfo.java:453)
at com.alibaba.fastjson.serializer.FieldSerializer.getPropertyValueDirect(FieldSerializer.java:110)
at com.alibaba.fastjson.serializer.JavaBeanSerializer.write(JavaBeanSerializer.java:196)
at com.alibaba.fastjson.serializer.JSONSerializer.writeWithFieldName(JSONSerializer.java:304)
at com.alibaba.fastjson.serializer.ASMSerializer_1_HttpEntity.write
at com.alibaba.fastjson.serializer.ListSerializer.write(ListSerializer.java:126)
at com.alibaba.fastjson.serializer.MapSerializer.write(MapSerializer.java:243)
at com.alibaba.fastjson.serializer.JSONSerializer.write(JSONSerializer.java:275)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:648)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:590)
at com.alibaba.fastjson.JSON.toJSONString(JSON.java:555)
上下文
其中file为MultipartFile
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
final String filename = file.getName();
Path path = file.toPath();
map.add("name", filename);
map.add("filename", filename);
HttpHeaders fileHeaders = new HttpHeaders();
fileHeaders.setContentDispositionFormData("file",filename);
ByteArrayResource contentsAsResource = new ByteArrayResource(Files.readAllBytes(path)){
@Override
public String getFilename(){
return filename;
}
};
map.add("file", new HttpEntity<>(contentsAsResource, fileHeaders));
报错的地方
JSON.toJSONString(map)
toJSONString也不是所有类型的都可以使用,要慎重一点,这里遇到file要去序列化,然后就报错了。