此方法适用,从一个服务器向另一个服务器发送文件,及从A服务器调用此方法,文件不会存到A服务器,而是通过对于的URL地址提交到到B服务器
实战代码:
@Login @PostMapping(value = "upload",consumes = "multipart/form-data") @ApiOperation(value = "上传图片接口",consumes = "multipart/form-data") public R multiUploadFileModel(@ModelAttribute CollectionInfoImgModel model)throws IOException { MultipartFile file=model.getFile(); File toFile = null; if(file.equals("")||file.getSize()<=0){ file = null; }else { InputStream ins = null; ins = file.getInputStream(); toFile = new File(file.getOriginalFilename()); inputStreamToFile(ins, toFile); ins.close(); } //url地址等于要把文件传到B服务器的接口地址 String url = "http://****/api/upload"; RestTemplate rest = new RestTemplate(); FileSystemResource resource = new FileSystemResource(toFile); MultiValueMap<String, Object> param = new LinkedMultiValueMap<>(); param.add("jarFile", resource); param.add("type", model.getType()); param.add("key", model.getKey()); param.add("mode", model.getMode()); HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String,Object>>(param); ResponseEntity<String> retUrl = rest.exchange(url, HttpMethod.POST, httpEntity, String.class); return R.ok("ok").put("path",retUrl.getBody().toString()); } /** B服务器接收A服务器传输文件的地址 **/ @ResponseBody @RequestMapping(value = "upload",method = RequestMethod.POST) public String uploads(String type, String key, String mode,MultipartFile jarFile) throws IOException{ logger.info(key+"进入文件接收方法"); CollectionInfoImgModel model=new CollectionInfoImgModel(); model.setFile(jarFile); model.setType(type); model.setKey(key); model.setMode(mode); String retUrl = FileUtils.saveUploadedFiles(model); return retUrl; }
文章来源:
不凡
版权声明:本站所发布的全部内容源于互联网搬运,仅供用于学习和交流,如果有侵权之处请第一时间联系我们删除。敬请谅解! E-mail:xujinxiao@126.com
还木有评论哦,快来抢沙发吧~