目录

postman测试接口出现404

目录

postman测试接口出现404

@RestController
public class UpAndDownController {

    @RequestMapping("uploadFile")
    public void upload(HttpServletRequest request) throws IOException {
        UploadService uploadService = new UploadService();
        uploadService.uploadBinary(request);
    }
}
public class UploadService {
    public String uploadBinary(HttpServletRequest request) throws IOException {
        ServletInputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            inputStream = request.getInputStream();

//            fileOutputStream = new FileOutputStream(new File("E:\\wordExport\\UpAndDown\\word_demo.docx"));
            fileOutputStream = new FileOutputStream(new File("E:\\wordExport\\UpAndDown\\testA001.jpg"));

            int len;
            byte[] bytes = new byte[1024];
            while((len = inputStream.read(bytes))!=-1){
                fileOutputStream.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败";
        }
        finally {
            if(fileOutputStream!=null){
                fileOutputStream.close();
            }
            if(inputStream!=null){
                inputStream.close();
            }
        }
        return "上传成功";
    }
}

上传方法原代码地址 博主不懂一休的《 》

出现问题:用postman测试报404

https://i-blog.csdnimg.cn/blog_migrate/06315803a45d7aeaae58566724a73d44.png

https://i-blog.csdnimg.cn/blog_migrate/ca3287d5af5e75e22c51eaf978353ab9.png

查了很多资料,大多数情况都说问题是:

404找不到资源,无非就是接口路径,提交方式、参数类型、返回结果类型有问题。

而我这是返回类型的问题。接口写的是@Controller,方法上没有添加@ResponseBody注解,这会去跳转返回结果名对应的页面(即return的结果),没有找到这样的页面,所以就404了。而我们接口返回结果要json格式的数据,所以添加@ResponseBody即可。

但这些对我这种情况都没有用

然后查到一篇文章 《 》

说可能问题出在项目Application的目录层级上面

我原本的Application放在word文件夹下,提到com下,404报错就解决了

https://i-blog.csdnimg.cn/blog_migrate/a2c5b578b3ef628323e86971f96a7c4e.png https://i-blog.csdnimg.cn/blog_migrate/15293d973c84e610834c2129d1c50020.png