文件上传

@PostMapping("/upload")
    public String upload(@RequestParam("abc") MultipartFile file, HttpServletRequest request) {
        System.out.println("==============" + request.getContentType());
        String uploadPath = "/Users/test";
        String originalFileName = file.getOriginalFilename();
        File local = new File(uploadPath + "/" + originalFileName);
        try {
            file.transferTo(local);
        } catch (IllegalStateException e) {
        } catch (IOException e) {
        }

        return originalFileName;
    }

使用curl上传:

curl -i  -F "abc=@TLS.md" -X POST  http://127.0.0.1:8080/upload

非form形式:

@PostMapping("/binary")
    public void binary(HttpServletRequest request) throws IOException {
        File destination = new File("/Users/test/abc");
        ServletInputStream inputStream = request.getInputStream();

        Enumeration ens = request.getHeaderNames();
        ens.asIterator().forEachRemaining(t -> System.out.println(t + ":" + request.getHeader(t)));

        FileUtils.copyInputStreamToFile(inputStream, destination);
    }

使用curl上传

curl --data-binary  @TLS.md  http://127.0.0.1:8080/binary

你可能感兴趣的