简体   繁体   English

使用客户端 Java 代码上传 Spring Boot Multipart 文件

[英]Spring boot Multipart file upload using Client Side Java Code

I have written a restful web service in spring boot which receives the file.我在 spring boot 中编写了一个宁静的 web 服务,它接收文件。

@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public void uploadFile(@RequestParam("file") MultipartFile uploadfile) {
    System.out.println("filename: " + uploadfile.getName());
}

How can we upload the file from client side java code to web service .我们如何将文件从客户端 java 代码上传到 web 服务 Instead of AJAX call or HTML page form multipart request?而不是 AJAX 调用或 HTML 页面表单多部分请求?

The code below call the web service with JSON object.下面的代码使用 JSON 对象调用 Web 服务。 Like this I want to receive the file in above written web service.像这样我想在上面编写的网络服务中接收文件。

void clientRequest(String server_url, JSONObject fileObj){

  try {

    URL url = new URL(server_url);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");

    OutputStream os = conn.getOutputStream();
    os.write(fileObj.toString().getBytes());
    os.flush();

    BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        logger.info("output :: " + output);
    }

    conn.disconnect();

  } catch (Exception e) {
    e.printStackTrace();
  }
}

You can use Spring's HttpEntity along with ByteArrayResource to upload the file, here is an example:您可以使用 Spring 的HttpEntityByteArrayResource来上传文件,这是一个示例:

MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
ByteArrayResource resource = new ByteArrayResource(file.getBytes()) {
    @Override
    public String getFilename() {
        return file.getName();
    }
};
data.add("file", resource);

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(data, requestHeaders);

final ResponseEntity<<SomeClass>> responseEntity = restTemplate.exchange(<url>, 
        HttpMethod.POST, requestEntity, new ParameterizedTypeReference<SomeClass>(){});

SomeClass result = responseEntity.getBody();

If you want to use a MultipartFile , you must use the multipart/form-data mimetype when requesting.如果要使用MultipartFile ,则必须在请求时使用multipart/form-data mimetype。 Instead of sending the JSON as request entity, you should construct a specific multipart-entity with a single field file in it.您应该构建一个特定的多部分实体,其中包含一个字段file ,而不是将 JSON 作为请求实体发送。

This is how it's done: How can I make a multipart/form-data POST request using Java?这是如何完成的: 如何使用 Java 发出多部分/表单数据 POST 请求?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM