postman

把json传回后台的时候,一直报错Bad Request400,以为传输json格式的时候有误,其实是postman的使用方式错了

后台代码

@RequestMapping(value = "/upload", method = {RequestMethod.GET,RequestMethod.POST})
    public String addPicture(HttpServletRequest request, @RequestBody String file){
        Responemsg responemsg=new Responemsg();
        FileInfo fileInfo = new FileInfo();

        if (file.isEmpty()){
            return new Gson().toJson(responemsg);
        }

        try {

            JSONObject jSONObject = JSONObject.fromObject(file);

            // 拆分原文件名
            String base64 = jSONObject.getString("base64");
            int length = jSONObject.getInt("length");

            // 判断文件是否一致
            if (base64.length() != length) {
                return new Gson().toJson(new Responemsg());
            }

            String name = jSONObject.getString("name");
            int i = name.indexOf(".");
            fileInfo.setFname(name.substring(0, i));
            fileInfo.setFext(name.substring(i));

            // 系统生成文件名
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String format = formatter.format(new Date().getTime());
            int num = (int)(Math.random() * 1000);
            fileInfo.setSysname(format+num);

            File file1 = new File(reportup);
            if (file1.isDirectory()){

            }else{
                file1.getParentFile().mkdirs();
            }

            if (fileInfo != null){
                responemsg.setCode("200");
                responemsg.setMsg("上传成功");
            }

        }catch (Exception ee){

        }

        return new Gson().toJson(responemsg);
    }
posted @ 2021-04-01 17:10  张三丰学Java  阅读(60)  评论(0编辑  收藏  举报