有个功能需要调用一个传输文件的api,使用FileSystemResource
进行传输,在调用api之前使用JSONObject.toJSONString()
输出一下log,结果发现每次输出log之后本地文件就会变成0KB。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72</version>
</dependency>
问题复现
- 随便创建了一个txt文件写点内容
- demo代码如下
HashMap<String, Object> hashMap = new HashMap<>(); FileSystemResource fileSystemResource = new FileSystemResource("E:\\my-code\\test\\test-fastjson.txt"); System.out.println("file size before Serialize:" + fileSystemResource.getFile().length()); hashMap.put("id",1); hashMap.put("file",fileSystemResource); System.out.println(JSONObject.toJSONString(hashMap)); System.out.println("file size after Serialize:" + fileSystemResource.getFile().length());
- 运行结果:
file size before Serialize:25 {"file":{"description":"file [E:\\my-code\\test\\test-fastjson.txt]","file":"E:\\my-code\\test\\test-fastjson.txt","filename":"test-fastjson.txt","open":false,"outputStream":{},"path":"E:/my-code/test/test-fastjson.txt","readable":true,"uRI":"file:/E:/my-code/test/test-fastjson.txt","uRL":"file:/E:/my-code/test/test-fastjson.txt","writable":true},"id":1} file size after Serialize:0