HttpClient和MultipartEntity与Jersey Multipart和Android

HttpClient和MultipartEntity与Jersey Multipart和Android,android,httpclient,jersey,multipart,Android,Httpclient,Jersey,Multipart,我有一个rest Web服务,它使用一个包含多部分消息的POST metod: @Path("transferFile") @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_XML) public String multipartTest(com.sun.jersey.multipart.MultiPart data) { try { // get firs

我有一个rest Web服务,它使用一个包含多部分消息的POST metod:

@Path("transferFile")     
 @POST  
@Consumes(MediaType.MULTIPART_FORM_DATA)  
@Produces(MediaType.APPLICATION_XML)  
public String multipartTest(com.sun.jersey.multipart.MultiPart data) {  
try {  
// get first body part (index 0)          
BodyPart bp = multiPart.getBodyParts().get(0);  
etc..  
现在我正试图为此编写一个java客户机。我从一个简单的jersey客户端开始: 是否查看从纯拷贝到剪贴板打印

    MultiPart multiPart = new MultiPart();  
    multiPart.bodyPart( new BodyPart(wavestream,MediaType.APPLICATION_OCTET_STREAM_TYPE));  

    Client c = Client.create();  
    WebResource r = c.resource("http://127.0.0.1:8080/webapp:);
response=r.path("transferFile").type(MediaType.MULTIPART_FORM_DATA).accept(MediaType.APPLICATION_XML).post(String.class, multiPart);  
这很好,一切都很好。然而,我需要这个客户端在Android上工作,我在那个平台上使用jersey时遇到了麻烦。因此,我在android上使用常规方式发送多部分消息:

    HttpClient client = new DefaultHttpClient();
 client.getParams().setParameter("http.socket.timeout", new Integer(90000)); // 90 second 

HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/webapp/transferFile");
 httpPost.setHeader("Content-Type", MediaType.MULTIPART_FORM_DATA );

//tried with and without base64
 byte [] encodedWavestream = Base64.encodeBytesToBytes(wavestream);
 InputStream ins = new ByteArrayInputStream(encodedWavestream);
 InputStreamBody body = new InputStreamBody(ins, "test" );
 int send = ins.available(); 

MultipartEntity requestContent = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE ); 
requestContent.addPart("stream", body);

httpPost.setEntity(requestContent); 
HttpResponse Response = client.execute(httpPost);
这会给服务器带来恼人的响应:

HTTP Status 400 - Bad Request  
The request sent by the client was syntactically incorrect (Bad Request).   

我检查了服务器日志文件,但那里什么都没有。所以我不知道这个错误的根源是什么。我已经写了一个简单的html页面,带有一个post公式和“多部分/表单数据”内容类型,它也可以工作!soapUI自动生成的请求也可以使用!为什么我的客户不工作?有人能帮忙吗?

泽西岛有臭虫。看

此问题仅在少数客户端(iOS、Android)出现

如果将内容类型设置为application/octet stream,则application/octet stream的Jersey MessageWriter将设置内容长度和长度 不作为分块传输方法发送

泽西岛的客户有:

ClientConfig config = new DefaultClientConfig();
config.getProperties().put(ClientConfig.PROPERTY_CHUNKED_ENCODING_SIZE, 32 * 1024);
但它不适用于iOS或Android的客户端。 所以我测试了Apache文件上传。这是另一个bug:“流意外结束”

只有可以为所有客户端上传正确的文件。 这是我的代码:

public Object[] getParametersAndFiles(HttpServletRequest request) throws IOException {
    log.debug("OreillyUpload");
    Properties params = new Properties();
    LinkedHashMap files = new LinkedHashMap();

    File tempDirectory = new File(System.getProperty("java.io.tmpdir")); 

    MultipartParser mp = new MultipartParser(request, 1*1024*1024); // 10MB
    Part part;
    while ((part = mp.readNextPart()) != null) {
        String name = part.getName();
        if (part.isParam()) {
            // it's a parameter part
            ParamPart paramPart = (ParamPart) part;
            String value = paramPart.getStringValue();
            params.put(name, value);

            log.debug("param; name=" + name + ", value=" + value);
        }
        else if (part.isFile()) {
            // it's a file part
            FilePart filePart = (FilePart) part;
            String fileName = filePart.getFileName();
            if (fileName != null) {
                // the part actually contained a file
                File file = new File(tempDirectory,fileName);
                long size = filePart.writeTo(file);
                files.put(name, file);

                log.debug("file; name=" + name + "; filename=" + fileName +
                        ", filePath=" + filePart.getFilePath() +
                        ", content type=" + filePart.getContentType() +
                        ", size=" + size);

            }
            else {
                // the field did not contain a file
                log.debug("file; name=" + name + "; EMPTY");
            }
        }
    }

    return new Object[] {params, files};
}
这是Jersey服务器代码(警告所有Jersey上传地址(如“@FormDataParam”)都应删除):


我假设您在同一台主机上使用android Emulator和应用程序服务器。在这种情况下,您必须使用10.0.2.2而不是localhost或127.0.0.1来访问主机系统!否则,请指向虚拟(仿真)设备本身。
@POST
@Path("uploadMarkup")
@Produces(MediaType.APPLICATION_JSON)
//    @Consumes(MediaType.MULTIPART_FORM_DATA)
////    public void uploadMarkup(
//    public JSONWithPadding uploadMarkup(
//            @FormDataParam("markupFile") InputStream markupFile,
//            @FormDataParam("markupFile") FormDataContentDisposition details,
//            @FormDataParam("slideNum") int slideNum) {
public JSONWithPadding uploadMarkup(@Context HttpServletRequest request) {
    Object[] data = uploadService.getParametersAndFiles(request);
    ...
}