一行日志,让整个文件导出服务导出内容都为空..

输出一行日志,却让整个文件上传服务挂了...

问题

直接上代码,看看有无眼尖的小伙伴发现问题:

// 设置参数
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
FileSystemResource resource = new FileSystemResource(file);
param.set("token", "XXXXXX");
param.set("file", resource);
LOGGER.info("开始文件上传: {}", JSONObject.toJSONString(param));
httpsRestTemplate.postForObject(uploadParam.getUrl(), param, HashMap.class);
LOGGER.info("文件上传结束");

分析

本人企图在上传文件的时候,把请求参数打印出来(为了分析问题),输出方式用 FastJSON 中的方法**JSONObject.toJSONString()**序列化了一下参数 。在实际导出的过程中,发现所有导出导出的文件内容为空,百思不得其姐。

于是进入 toJSONString() 方法,一路Debug进来,到了ListSerializer 这里,如下图:
在这里插入图片描述
itemSerializer这里实际对象是ASMSerializer(关于这玩意,查了一些资料,在FastJSON中使用它来实现反射),而在处理反射的过程中,会调用get方法获取属性值执行序列化。

而在FileSystemResource 中,关于OutputStream 的获取,是会重新new一个FileOutputStream,再往方法里面看,发现了append属性为false,相当于按原来的路径重新写入了一个新文件,而且是不追加的方式。到这里也算明白了为啥我的文件导出来都为空了。

//

public class FileSystemResource extends AbstractResource implements WritableResource {
 //....

public OutputStream getOutputStream() throws IOException {
        return new FileOutputStream(this.file);
    }

}

public FileOutputStream(File file) throws FileNotFoundException {
        this(file, false);
    }

public FileOutputStream(File file, boolean append)
        throws FileNotFoundException
    {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkWrite(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        this.fd = new FileDescriptor();
        fd.attach(this);
        this.append = append;
        this.path = name;

        open(name, append);
    }

小结

  • JSONObject.toJsonString 使用还是得慎重
  • 复杂对象序列化时,特别是一些框架封装的对象还是得多多留意
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

legendaryhaha

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值