반응형

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public static void main(String...strings) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.add("X-TOKEN", "eyJhbGciOiJIUzI1NiJ9.eyJzdW4NH0.VqrBBwVnzOsqrkPKA");


MultiValueMap body
= new LinkedMultiValueMap<>();
body.add("file", new FileSystemResource(new File("C:\\Users\\foo\\Downloads\\test.png")));

HttpEntity> requestEntity
= new HttpEntity<>(body, headers);

String serverUrl = "https://localhost/atch/add";

RestTemplate restTemplate = new RestTemplate();
ResponseEntity response = restTemplate
.postForEntity(serverUrl, requestEntity, String.class);

System.out.println(response);
System.out.println(response.getBody());
}

public static Resource getTestFile() throws IOException {
Path testFile = Files.createTempFile("test-file", ".txt");
System.out.println("Creating and Uploading Test File: " + testFile);
Files.write(testFile, "Hello World !!, This is a test file.".getBytes());
return new FileSystemResource(testFile.toFile());
}

}

반응형
LIST

+ Recent posts