使用RestTemplate如何上传文档

数据流使用此种方式
 final String FileName = "test.xlsx";
		ByteArrayResource byteArrayResource = new ByteArrayResource(fileBytesRespE.getBody()){   
            @Override  
            public String getFilename() throws IllegalStateException {   
                return FileName;  
            }  
              
        };
	
        form.add("uploadfile", byteArrayResource);

 文件上传使用此种方式
                        HttpHeaders headers = new HttpHeaders();
		       
		         headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		
		          headers.setContentType( MediaType.parseMediaType("multipart/form-data; charset=UTF-8"));
		        ing, Object>();
		         MultiValueMap form = new  LinkedMultiValueMap ();
		         FileUtils.writeByteArrayToFile(new File(excelMultipartFile.getOriginalFilename()),excelMultipartFile.getBytes() );
		         FileSystemResource fileSystemResource = new FileSystemResource(new File(excelMultipartFile.getOriginalFilename()));
		         form.add("file", fileSystemResource);
		        
		        HttpEntity< MultiValueMap > formEntity = new HttpEntity< MultiValueMap >(form, headers);
		        
		      
		       Map uriVariables = new HashMap();
		       uriVariables.put("namespace", "test");
			   ResponseEntity result = restTemplate.postForEntity("https://xx432432fefewqreqx.com",formEntity , String.class);


你可能感兴趣的:(Java)