1

这是基于弹簧的api:

@RequestMapping(value = ControllerConstants.FILE_UPLOAD_URI, method = RequestMethod.POST)
    public @ResponseBody
    boolean processUpload(@RequestParam("file") MultipartFile file, @RequestParam("accessionId") String accessionId, @RequestParam("protocol") String protocol, HttpServletRequest request) throws IOException {        
        logger.info("upload file: {} with accessionId: {} and protocol:{}",file.getOriginalFilename(),accessionId,protocol);         
        return dataManagerService.writeFile(file, protocol, accessionId);
    }

这是我的最大努力:

FormDataMultiPart part = new FormDataMultiPart()
                .field("accessionId", returnedAccessionId)
                    .field("protocol", protocol)
                    .field("name", file.getName())
                    .field("file", stream, MediaType.APPLICATION_OCTET_STREAM_TYPE);    

            MultivaluedMap<String, String> params = new MultivaluedMapImpl();
            params.add("file",  file.getAbsolutePath());
            params.add("accessionId", returnedAccessionId);
            params.add("protocol", protocol);       

            WebResource resource2 = client.resource(agent.getServerEndpointUri() + "/novax/service/dataManager/upload");
            ClientResponse response4 = resource2
                    .queryParams(params)
                    .type(MediaType.MULTIPART_FORM_DATA_TYPE)
                    .post(ClientResponse.class, part);

响应为 404。
2 个问题: - 是否正确定义了 spring API?- 如何使用 Jersey 客户端正确调用 api?

4

1 回答 1

0

好的,我得到它的工作,使用以下代码:

        FormDataMultiPart part = new FormDataMultiPart()
                .field("accessionId", returnedAccessionId)
                .field("protocol", protocol)
                .field("name", file.getName());
        part.bodyPart (new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE)); 

        WebResource resource2 = client.resource(pathOrUrl);
        ClientResponse response4 = resource2
                .type(MediaType.MULTIPART_FORM_DATA)
                .post(ClientResponse.class, part);

我需要使用“bodyPart”方法,该方法似乎不适用于“构建器”模式。

于 2012-12-11T03:01:12.297 回答