您现在的位置是:首页 > 正文

RestTemplate文件上传模拟form表单提交

2024-02-01 00:57:30阅读 2

使用RestTemplate调用接口起码四次了

但不同的是这次是上传文件,注意,这个跟一般的调用 content-type不一样哦

介绍一下我的项目背景:

前端是 fileinput插件 上传

后端是 springmvc接收 ,调用接口就用到了RestTemplate

此处时通过RestTemplate 模拟form表单提交

restTemplate 我采用了网上说的方法,配置成了bean

并进一步优化,放在了util里

先附上controller层代码


	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> upload(@RequestParam("file_data") CommonsMultipartFile upfile) {
		Map<String, Object> result = new HashMap<>();
		long fileSize = upfile.getSize();
		//图片大小处理 自己写代码
	try {
			String returnUrl = RestTemplateUtils.uploadFile(upfile);
			
		} catch (Exception e) {
		
			result.put("error","出错了");
			return result ;
		}
		return result;
	}

 RestTemplateUtils 工具类,这里因为是静态方法,所以注入restTemplate采用了set方式注入,类加上@Component,set方法加上@Autowired

此处方法来自:https://blog.csdn.net/RogueFist/article/details/79575665


import java.io.File;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
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;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class RestTemplateUtils {

	
	private static RestTemplate restTemplate;
	
	//用这种方法注入restTemplate
	@Autowired
	public void setRestTemplate(RestTemplate restTemplate) {
		RestTemplateUtils.restTemplate=restTemplate;
	}
		
	public static String uploadFile(CommonsMultipartFile upfile) throws Exception {
	
		MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
	//	param.add("params", "params");
		
		FileUtils.writeByteArrayToFile(new File(upfile.getOriginalFilename()), upfile.getBytes());
		FileSystemResource fileResource = new FileSystemResource(new File(upfile.getOriginalFilename()));
		param.add("image", fileResource);

		HttpHeaders headers = new HttpHeaders();
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		headers.setContentType(MediaType.parseMediaType("multipart/form-data; charset=UTF-8"));

		HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(param,
				headers);
		ResponseEntity<Map> result = restTemplate.postForEntity(url, requestEntity, Map.class);
		Map<String, Object> body = result.getBody();
		Object isSuccess = body.get("success");
		String isSuccessString = String.valueOf(isSuccess);
		String returnUrlString = "";
		if ("true".equals(isSuccessString)) {
			returnUrlString = (String) body.get("url");
		} else {
			returnUrlString=null;
		}
		return returnUrlString;
	}

}

 RestTemplate可以注入的方法

此处参考:https://blog.csdn.net/qq_35981283/article/details/82056285

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;


@Configuration
public class RestTemplateConfig {
//	@Bean
//	public RestTemplate restTemplate() {
//		RestTemplate restTemplate = new RestTemplate();
//		return restTemplate;
//	}
	@Bean
    public RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory());
        return restTemplate;
    }
}

 嗯,代码基本上就是这样了,欢迎提问,互相学习

 

网站文章