import java.io.IOException; |
import org.springframework.core.io.FileSystemResource; |
import org.springframework.http.HttpEntity; |
import org.springframework.http.HttpHeaders; |
import org.springframework.http.HttpMethod; |
import org.springframework.http.MediaType; |
import org.springframework.util.LinkedMultiValueMap; |
import org.springframework.web.client.RestTemplate; |
public class Application { |
public static void main(String[] args) throws IOException { |
LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); |
FileSystemResource value = new FileSystemResource(new File("D://test.png")); |
HttpHeaders headers = new HttpHeaders(); |
headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers); |
RestTemplate restTemplate = new RestTemplate(); |
restTemplate.exchange("http://localhost/api/v1/users/avatar", HttpMethod.POST, requestEntity, String.class); |
}
ref: https://gist.github.com/ansidev/5816b8b3108c30b5279f5fec67506798