springmvc笔记 - 上传图片Bad Request问题【已解决】

开头先抛出问题截图

运行十分正常并未报出问题,接下来看,我们修改数据,以及同时上传图片。

等待点击后

 

错误报错    Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

对,接着就报了这个摸不清的错误报错。让我十分头大,而且控制台一点错误都没有报。

 

好了,我们问题分析起来:

 

看代码(该代码用于接收前端数据的)

@RequestMapping("/updataitems")
    public String UpdataItemByids(MultipartFile itempic, Items items) throws Exception {


        if (itempic != null){
                //图片储存的位置f
                String path="G:\\temp\\";
                //图片上传上来的原始名称
                String originalFilename= itempic.getOriginalFilename();
                //新的图片名称
                String newFilename=UUID.randomUUID()+originalFilename.substring(originalFilename.lastIndexOf("."));
                //新的图片
                File newFile = new File(path+newFilename);
                //将内存的数据写入出来
                itempic.transferTo(newFile);

                //如果上传成功,就将文件地址写入到数据库pic中
                items.setItempic(newFilename);

            }

        itemsServiceImpi.updataItemById(items);

        return "redirect:/items/queryitems";
    }

经过我的几经骚扰度娘,终于知道了问题。

 

参数与pojo 类中的变量类型匹配不对

 

 

 

 前台的name参数与pojo变量名一样,在传参的时候不能与pojo的类型匹配就报Bad Request。

 

 

所以问题出来,就可以解决问题了。

 

 

 简单的改MultipartFile参数名和前台name参数,只要前台的数据不交给pojo里面String类型的变量就不会出错了!

 

看到这里是不是你的代码也出了类似问题呢,哈哈快去思考解Bug吧!

 

最后要感谢博主Hyman_Pi 给的解决的方案

https://blog.csdn.net/Hyman_Pi/article/details/77859047

 

posted @ 2018-07-27 10:14  花裤兜派大星  阅读(1329)  评论(0)    收藏  举报