Java analog HTTP request upload file, based on Apache HttpClient

Source: Internet
Author: User

1. Reliance

 The request to impersonate the HTTP port relies on Apache HttpClient, requires third-party JSON support, and the project adds

<dependency>            <groupId>org.apache</groupId>            <artifactid>httpclient</ artifactid>            <version>4.1.2</version>        </dependency>


<dependency>   <groupId>org.apache</groupId>   <artifactid>httpclient</ Artifactid>   <version>4.1.2</version></dependency>

2. Source code

ImportOrg.apache.commons.httpclient.HttpStatus;Importorg.apache.http.HttpEntity;ImportOrg.apache.http.HttpResponse;Importorg.apache.http.client.HttpClient;ImportOrg.apache.http.client.methods.HttpPost;Importorg.apache.http.entity.mime.MultipartEntity;ImportOrg.apache.http.entity.mime.content.InputStreamBody;ImportOrg.apache.http.entity.mime.content.StringBody;Importorg.apache.http.impl.client.DefaultHttpClient;Importorg.apache.http.util.EntityUtils;ImportOrg.json.JSONObject;ImportJava.io.*;Importjava.net.HttpURLConnection;ImportJava.net.URL;/*** Created by Administrator on 2016/1/19.*/ Public classFileuploadtest { Public Staticstring Upload (string url,string filePath) {string Fdfspath= ""; Try{HttpClient HttpClient=Newdefaulthttpclient ();//HttpPost httppost = new HttpPost ("http://127.0.0.1: 8082/fileupload.action ");//Request FormatHttpPost HttpPost =Newhttppost (URL); File File=NewFile (FilePath); String name=File.getname (); InputStream in=Newfileinputstream (file); Multipartentity reqentity=Newmultipartentity (); Inputstreambody Inputstreambody=Newinputstreambody (in,name); Stringbody Filenam=Newstringbody (name); Reqentity.addpart ("UploadFile", Inputstreambody); Reqentity.addpart ("Uploadfilename", Filenam);            Httppost.setentity (reqentity); HttpResponse Response=Httpclient.execute (HttpPost); intStatusCode =response.getstatusline (). Getstatuscode (); if(StatusCode = =HTTPSTATUS.SC_OK) {//System.out.println ("Server normal response .....");Httpentity resentity =response.getentity (); Jsonobject JSON=NewJsonobject (entityutils.tostring (resentity). toString ()); System.out.println (Json.getstring ("Returnresult"));//System.out.println (entityutils.tostring (resentity));//HttpClient comes with the tool class to read the returned data//System.out.println (Resentity.getcontent ());Entityutils.consume (resentity); }        } Catch(Exception e) {e.printstacktrace (); }        return""; }  }

3. Parsing

Upload parameters: url--Upload server-side interface request, FilePath---Local file or picture storage path.

   

Java analog HTTP request upload file, based on Apache HttpClient

Related Article

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.