使⽤restTemplate的post请求传输⽂件与⽂件数组
restTemplate传递⽂件数组
传递MultipartFile[]
被请求接⼝
@ApiImplicitParams({
@ApiImplicitParam(name ="fid", value ="当前表ifd", required =true, dataType ="string")
})
@ApiOperation(value ="图⽚保存", notes ="图⽚保存")
@PostMapping(value ="/savePicture")
public ResponseModel savePicture(@RequestParam("multipartFile")MultipartFile[] multipartFile, String fid)throws IOException {
ResponseModel responseModel =new ResponseModel();
if(multipartFile.length<=0){
responseModel.ResMessage ="⽂件为空上传失败";
}else{
String save = tcartransferService.save(multipartFile,fid);
responseModel.ResMessage =save;
responseModel.ResCode=-1;
responseModel.ResMessage ="图⽚保存成功!";
}
return responseModel;
}
请求接⼝
@ApiImplicitParams({
@ApiImplicitParam(name ="fid", value ="当前表ifd", required =true, dataType ="string")
})
@ApiOperation(value ="图⽚保存", notes ="图⽚保存")
@PostMapping(value ="/savePicture")
public String savePicture(@RequestParam("multipartFile")MultipartFile[] multipartFile, String fid,HttpServletRequest httpServletRequest)throws IOExcep tion {
MultiValueMap<String,Object> map =new LinkedMultiValueMap<>();
//设置请求头
HttpHeaders headers =new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);
headers.add("token",Header("token"));
List<Object> fileList =new ArrayList<>();
for(MultipartFile multipartFiles : multipartFile){
ByteArrayResource byteArrayResource =new Bytes()){
@Override
public String getFilename()throws IllegalStateException {
OriginalFilename();
}
};
fileList.add(byteArrayResource);
}
map.put("multipartFile", fileList);
map.add("fid", fid);
HttpEntity<MultiValueMap<String, Object>> request =
new HttpEntity<MultiValueMap<String, Object>>(map, headers);
String data = restTemplate.postForObject(URL +"/tcartransfer/savePicture", request, String.class);
return data;
}
传递单个⽂件
被调⽤接⼝
@RequestMapping(value ="/save", method ={RequestMethod.POST})
public ResponseModel save(@RequestParam("multipartFile") MultipartFile multipartFile, Tmanagement tmanagement, String A, String B,String C,String D,String E
)throws IOException, ParseException {
SimpleDateFormat date=new SimpleDateFormat("yyyy-MM-dd");
ResponseModel responseModel =new ResponseModel();
if(StringUtils.isNotBlank(A)){
tmanagement.setFDeliveryTimeT(A);
}getsavefilename
if(StringUtils.isNotBlank(B)){
tmanagement.setFDeliveryTimeTh(date.parse(B));
}
if(StringUtils.isNotBlank(C)){
tmanagement.setFDeliveryTimeFour(date.parse(C));
}
if(StringUtils.isNotBlank(D)){
tmanagement.setFDeliveryTimeFive(date.parse(D));
}
if(StringUtils.isNotBlank(E)){
tmanagement.setFDeliveryTimeSix(date.parse(E));
}
String preserve = tmodelreceive.qualityDepartment(multipartFile, tmanagement);
if("修改成功".equals(preserve)){
responseModel.ResCode=-1;
responseModel.ResMessage = preserve;
}else{
responseModel.ResMessage = preserve;
}
return responseModel;
}
restTemplate调⽤接⼝传递单个⽂件
@RequestMapping(value ="/save", method ={RequestMethod.POST})
public String save(@RequestParam("multipartFile") MultipartFile multipartFile, Tmanagement tmanagement,HttpServletRequest httpServletRequest )throws IOException {
MultiValueMap<String,Object> map =new LinkedMultiValueMap<>();
//设置请求头
HttpHeaders headers =new HttpHeaders();
MediaType type = MediaType.parseMediaType("multipart/form-data");
headers.setContentType(type);
headers.add("token",Header("token"));
List<Object> fileList =new ArrayList<>();
SimpleDateFormat ss =new SimpleDateFormat("yyyy-MM-dd");
if(!multipartFile.isEmpty()){
ByteArrayResource byteArrayResource =new Bytes()){
@Override
public String getFilename()throws IllegalStateException {
OriginalFilename();
}
};
fileList.add(byteArrayResource);
}
FStatus().equals("zlbpd")){
//质量部判定质量⼯单中
map.add("FId",FId());
map.add("A",FDeliveryTimeT());
}else FStatus().equals("zzbwx")){
//制造部维修中
map.add("FId",FId());
map.add("B",ss.FDeliveryTimeTh()));
}else FStatus().equals("zlbjy")){
//待质量部检验
map.add("FId",FId());
map.add("C",ss.FDeliveryTimeFour()));
}else FStatus().equals("tzxubm")){
//通知要求部门-报废
map.add("FId",FId());
map.add("D",ss.FDeliveryTimeFive()));
}else FStatus().equals("ATOczqr")){
//ATO处置确认中
map.add("FId",FId());
map.add("E",ss.FDeliveryTimeSix()));
}
map.add("FStatus",FStatus());
map.put("multipartFile", fileList);
HttpEntity<MultiValueMap<String, Object>> request =
new HttpEntity<MultiValueMap<String, Object>>(map, headers);
String data = restTemplate.postForObject(URL +"/tmanagement/save", request, String.class);
return data;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论