Apache HttpClient making multipart form post

后端 未结 3 859
暖寄归人
暖寄归人 2020-11-27 11:39

I\'m pretty green to HttpClient and I\'m finding the lack of (and or blatantly incorrect) documentation extremely frustrating. I\'m trying to implement the following post (

3条回答
  •  攒了一身酷
    2020-11-27 12:14

    if use org.apache.commons.httpclient.HttpClient package, maybe that can help you!

        HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
        //here should set HttpConnectionManagerParams but not important for you
        HttpClient httpClient = new HttpClient(httpConnectionManager);
    
        PostMethod postMethod = new PostMethod("http://localhost/media");
    
        FilePart filePart = new FilePart("file", new File(filepath));
        StringPart typePart = new StringPart("type", fileContent.getType(), "utf-8");
        StringPart fileNamePart = new StringPart("fileName", fileContent.getFileName(), "utf-8");
        StringPart timestampPart = new StringPart("timestamp", ""+fileContent.getTimestamp(),"utf-8");
        Part[] parts = { typePart, fileNamePart, timestampPart, filePart };
    
        MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, postMethod.getParams());
        postMethod.setRequestEntity(multipartRequestEntity);
        httpClient.executeMethod(postMethod);
        String responseStr = postMethod.getResponseBodyAsString();
    

提交回复
热议问题