相关文章推荐
有胆有识的灌汤包  ·  jest axios ...·  1 年前    · 
没有腹肌的雪糕  ·  ATL classes and ...·  1 年前    · 
安静的小刀  ·  matlab numpy arange-掘金·  1 年前    · 

有时候需要用Post请求上传文件,如果是常见的表单/文件,在header中使用常见的Content-Type就可以达成,我们可以使用方便的 RestTemplate ,里面封装了常见的 application/json application/x-www-urlencoded multipart/form-data 等等,还有图片啊二进制文件之类的,几乎涵盖了大部分文件种类,发送请求的方法网上也很多。
但我尝试上传dicom文件到orthanc server的时候,Content-Type的要求是 application/dicom ,然后加上一个 dcm 文件一起发送Post请求。
Upload API
尝试用了 RestTemplate ,也查了网上发送Post请求上传文件的资料,但是发现会各种报错。最后用了原始的 HttpURLConnection 并上传成功,这也许也是使用Java发送Post请求上传文件的通式。

	public void postDcm(String filePath, String orthancUrl, String token) throws IOException {
        URL url = new URL(orthancUrl);
        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
        httpCon.setDoOutput(true);
        httpCon.setRequestMethod("POST");
        // Optional: 如果需要,可以加上token等其它header信息
        httpCon.setRequestProperty ("Authorization", "Bearer " + token);
        OutputStream os = httpCon.getOutputStream();
        byte[] bytes = Files.readAllBytes(Paths.get(filePath));
        os.write(bytes);
        os.flush();
        os.close();
        httpCon.connect();
        // 打印出response
        String result;
        BufferedInputStream bis = new BufferedInputStream(httpCon.getInputStream());
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        int result2 = bis.read();
        while(result2 != -1) {
            buf.write((byte) result2);
            result2 = bis.read();
        result = buf.toString();
        System.out.println(result);
                    有时候需要用Post请求上传文件,如果是常见的表单/文件,在header中使用常见的Content-Type就可以达成,我们可以使用方便的RestTemplate,里面封装了常见的application/json、application/x-www-urlencoded、multipart/form-data等等,还有图片啊二进制文件之类的,几乎涵盖了大部分文件种类,发送请求的方法网上也很多。但我尝试上传dicom文件到orthanc server的时候,Content-Type的要求是applicati