在我的 Spring 启动应用程序中,我需要将String值与MultipartFile一起传递为Requestparam
。
控制器在下面,我将 MultipartFile 转换为java.io.File
,然后在restTemplate
的帮助下将其传递给DAO控制器。
来自Angular的请求将首先命中上载控制器,然后 UploadController 是客户端(Java),它将使用基本URL调用服务器 svs-ba-dao控制器。csvUpload
包含基本URL:http:// localhost:8082 / svs-ba-dao / csvUpload?parentPkId =&file = multipartFile
@CrossOrigin
@RestController
public class UploadController {
private static final Logger log = LogManager.getLogger(UploadController.class);
@Value("${salama.dao.csvUpload.rest.url}")
private String csvUpload;
@Autowired
UploadHelperService uploadHelperService;
@PostMapping(value = "/csvUpload")
public String csvUpload(@QueryParam("parentPkId") String parentPkId, @RequestParam("file") MultipartFile file ) throws IllegalStateException, IOException{
log.info("Calling csvUpload :" + csvUpload);
final HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
File cnvFile = uploadHelperService.multipartToFile(file,file.getOriginalFilename());
System.out.println("Converted File Name is -->"+cnvFile.getName());
final MultiValueMap<String, Object> data = new LinkedMultiValueMap<>();
data.add("parentPkId", parentPkId);
data.add("file", new FileSystemResource(cnvFile));
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(data, requestHeaders);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> obj =restTemplate.exchange(csvUpload, HttpMethod.POST,
requestEntity, String.class);
return obj.getBody();
}
}
在svs-ba-dao控制器中捕获它如下
@RequestMapping(value="/csvUpload", method=RequestMethod.POST)
public String csvUpload(@RequestParam String parentPkId, @RequestParam MultipartFile file) throws IOException {
log.info("Entered method csvUpload() of svs-ba-dao.class");
return uploadService.csvUpload(parentPkId,file);
}
我已经将这些属性包含到应用程序的application.properties文件中:spring.servlet.multipart.maxFileSize=1MB spring.servlet.multipart.maxRequestSize=1MB
因此,我启动了我的应用程序,并调用了生成POST请求的/csvUpload
。我遇到错误。
Converted File Name is -->testInput_TxnMpMotSlv.csv
2019-01-24T15:12:41,217 ERROR [[localhost].[/svs-ba-dao].[dispatcherServlet]] [http-nio-8085-exec-2] Servlet.service() for servlet [dispatcherServlet] in context with path [/svs-ba-dao] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpServerErrorException: 500 null] with root cause
org.springframework.web.client.HttpServerErrorException: 500 null
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:97) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:766) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:724) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:600) ~[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at com.svs-ba-dao.controller.UploadController.csvUpload(UploadController.java:59) ~[classes/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_192]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_192]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_192]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_192]
resttemplate中存在错误,因为它采用null,但是从语法上来说,调用RestTemplate的方法是正确的,因为我没有收到任何错误。
通过传递
requestparam
值,我是否可以正确调用基本URL ?即parentPkId =?&file = multipartFile
http:// localhost:8082 / svs-ba-dao / csvUpload?parentPkId =&file = multipartFile
老实说,我以前没有做过。
如果我做错了,欢迎任何线索或更正。提前致谢
最佳答案
在这里我发现我错了
csvUpload将保存应在rest模板中传递的URL
@Value("${salama.dao.csvUpload.rest.url}")
private String csvUpload;
我发现该基本网址:即csvUpload
http://localhost:8082/svs-ba-dao/csvUpload?parentPkId=&file=multipartFile
从 UploadController 发送错误。无需在URL中为
multipartFile
指定类似mediaType
的名称。它会自动捡起。但是对于某些URL,如果发送JSON对象,则需要指定
?mediaType=json
。这是我更新的基本 URL :
http://localhost:8082/svs-ba-dao/csvUpload?parentPkId=&file=