HttpClient (Fri)--Upload file for simulation form

Source: Internet
Author: User

1.maven Dependency

      <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>

2. Code implementation

ImportJava.io.File;Importjava.io.IOException;Importorg.apache.http.HttpEntity;ImportOrg.apache.http.HttpResponse;Importorg.apache.http.client.ClientProtocolException;Importorg.apache.http.client.HttpClient;ImportOrg.apache.http.client.config.RequestConfig;ImportOrg.apache.http.client.methods.HttpPost;ImportOrg.apache.http.entity.mime.MultipartEntityBuilder;Importorg.apache.http.impl.client.HttpClients;Importorg.apache.http.util.EntityUtils; Public classHttpclientupload { Public Static voidMain (string[] args) {//upload the fileUrl to the URL, and after the upload is successful, returnString url = "Http://127.0.0.1:8080/Joe/upload"; String FILEURL= "C:/abc.txt"; Try{System.out.println (post (URL,"File",NewFile (FILEURL)); } Catch(Exception e) {e.printstacktrace (); }    }    /*** Post: (upload). *      * @authorJoe date:2017 September 11 afternoon 5:37:46 *@paramServerURL *@paramFileparamname *@paramfile *@return     * @throwsclientprotocolexception *@throwsIOException*/     Public Staticstring Post (String ServerURL, string fileparamname, file file)throwsclientprotocolexception, IOException {httppost httppost=NewHttpPost (ServerURL); Multipartentitybuilder Builder=multipartentitybuilder.create (); //uploaded Filesbuilder.addbinarybody (fileparamname, file); Httpentity httpentity=Builder.build ();        Httppost.setentity (httpentity); //request to get data timeout time, set from Connect//Manager gets connection timeout (because the current version is available to share the connection pool), set the connection time-out periodRequestconfig Requestconfig =Requestconfig.custom (). SetSocketTimeout (10000). Setconnectionrequesttimeout (3000). Setconnecttimeout (10000). build ();        Httppost.setconfig (Requestconfig); HttpClient HttpClient=Httpclients.createdefault (); HttpResponse Response=Httpclient.execute (HttpPost); returnentityutils.tostring (Response.getentity ()); }}

HttpClient (Fri)--Upload file for simulation form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.