使用Spring RestTemplate上传zip文件和表单数据

时间:2017-11-06 13:42:58

标签: java spring resttemplate

我正在尝试使用RestTemplate上传zip文件和表单数据。 请在下面找到代码。

@Controller
@RequestMapping("/test")
public class EdicomGatewayClient {

    @RequestMapping(value ="/publisEdicomDocument", method = RequestMethod.POST)
    public void publisEdicomDocument() {        

        List<ApiError> errors = null;

        try {
            RestTemplate restTemplate = new RestTemplate();

            String restURL = "";
            File file = new File("");

            DiskFileItem fileItem = new DiskFileItem("file", "text/plain", false, file.getName(), (int) file.length(), file.getParentFile());
            fileItem.getOutputStream();
            MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

            List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
            acceptableMediaTypes.add(MediaType.MULTIPART_FORM_DATA);

            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(acceptableMediaTypes);

            MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<String, Object>();

            multipartRequest.add("user","OPENTEXT_AR_TST");
            multipartRequest.add("password","4t4qqdbddp");
            multipartRequest.add("domain","OPENTEXT_AR_TST");
            multipartRequest.add("group","ASPEDI41");
            multipartRequest.add("publishType","2");
            multipartRequest.add("process","MAPA_AFIP_WSMTXCA_EDICOM_OPENTEXT");
            multipartRequest.add("saveOriginalImage",false);
            multipartRequest.add("sendDocument",true);
            multipartRequest.add("returnData",false);
            multipartRequest.add("returnDataType","2");
            multipartRequest.add("message", new ByteArrayResource(multipartFile.getBytes()));
            multipartRequest.add("filename", multipartFile.getOriginalFilename());

            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(multipartRequest, headers);

            ResponseEntity response = restTemplate.exchange(restURL,HttpMethod.POST, requestEntity, String.class);

            System.out.println("Response ******************************************:"+response);

        } catch (Exception e) {

        }   
    }

    public static void main(String[] args) {
        EdicomGatewayClient edicom = new EdicomGatewayClient();
        edicom.publisEdicomDocument();
    }       
}

这给了我以下错误:

org.springframework.web.client.HttpClientErrorException: 400 bad Request.

0 个答案:

没有答案