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爲MultipartFilejava
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要去序列化,而後就報錯了。spring