记一个toJSONString异常

异常

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要去序列化,然后就报错了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,080评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,985评论 6 342
  • 对象的创建与销毁 Item 1: 使用static工厂方法,而不是构造函数创建对象:仅仅是创建对象的方法,并非Fa...
    孙小磊阅读 2,040评论 0 3
  • Dies *ist* ein Test
    borderliner阅读 247评论 0 2
  • 阿香是一个小巧玲珑的姑娘,又瘦,要是从后面看,别人会以为她还只是一个十二三岁的孩子。阿香经常抱着她那个个子小小的儿...
    临界紫苏衣阅读 827评论 0 0