首页 > 编程语言 > 详细

Spring’s RestTemplate

时间:2019-05-29 10:41:38      阅读:122      评论:0      收藏:0      [点我收藏+]

Spring’s RestTemplate

/**
 * After the word document is generated in memory we can upload it to the server.
 *
 * @param fileContents The byte array we‘re wanting to POST
 * @param filename     The name of the file you‘re uploading. You can make yours up if you want.
 */
private static void uploadWordDocument(byte[] fileContents, final String filename) {
    RestTemplate restTemplate = new RestTemplate();
    String fooResourceUrl = "http://localhost:8080/spring-rest/foos"; // Dummy URL.
    MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();

    map.add("name", filename);
    map.add("filename", filename);

    // Here we 
    ByteArrayResource contentsAsResource = new ByteArrayResource(fileContents) {
        @Override
        public String getFilename() {
            return filename; // Filename has to be returned in order to be able to post.
        }
    };

    map.add("file", contentsAsResource);

    // Now you can send your file along.
    String result = restTemplate.postForObject(fooResourceUrl, map, String.class);
    
    // Proceed as normal with your results.
}

 

Spring’s RestTemplate

原文:https://www.cnblogs.com/stono/p/10942062.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!