关于java:文件上传

38次阅读

共计 910 个字符,预计需要花费 3 分钟才能阅读完成。

@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<String> 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

正文完
 0