HttpClient模拟表单提交文件

POM文件:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.2</version>
</dependency>

Test Case文件:

/**
 * send attachment file Mail
 *
 * @author jadenq
 */
public class UploadTest {
 
    @Test
    public void sendMailTest() {
        String requestIdValue = "-999";
        String toAddressValue = "[email protected];[email protected]";
        String subjectValue = "send mail by httpClient";
        String contentValue = "The test is <font color = 'green'>successful</font>";
        String filePathValue = "C:/Users/Public/Pictures/Sample Pictures/Tulips.jpg";
        try {
            sendMail(requestIdValue, toAddressValue, subjectValue, contentValue, filePathValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private void sendMail(String requestIdValue, String toAddressValue, String subjectValue, String contentValue, String filePath) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            //Server address
            String uri = "http://127.0.0.1:7080/alert-service/mails/attachment";
            HttpPost httppost = new HttpPost(uri);
 
            String path = filePath;
            File file = new File(path);
            FileBody attachFilea = new FileBody(file);
            StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
            StringBody requestId = new StringBody(requestIdValue, ContentType.TEXT_PLAIN);
            StringBody toAddress = new StringBody(toAddressValue, ContentType.TEXT_PLAIN);
            StringBody subject = new StringBody(subjectValue, ContentType.TEXT_PLAIN);
            StringBody content = new StringBody(contentValue, ContentType.TEXT_PLAIN);
 
            HttpEntity reqEntity = MultipartEntityBuilder.create()
                                                         .addPart("attachFilea", attachFilea)
                                                         .addPart("comment", comment)
                                                         .addPart("requestId", requestId)
                                                         .addPart("toAddress", toAddress)
                                                         .addPart("subject", subject)
                                                         .addPart("content", content)
                                                         .build();
            httppost.setEntity(reqEntity);
            System.out.println("executing request " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    System.out.println("Response content length: " + resEntity.getContentLength());
                }
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
 
}


猜你喜欢

转载自blog.csdn.net/weixin_37962025/article/details/80661114