httpclient Implement file Upload Relay

Source: Internet
Author: User

Development Features:
Web front-end submission upload file, a server receive, forwarding to B server for file processing
The following is a simple implementation of the code, the specifics of the optimization of the fundamental requirements of their own changes.

PublicString Handleresponse (httpservletrequest request, httpservletresponse response) throws Unsupportedencodingexcep tion, IOException {String method = Request.getmethod ();String URL ="B-Server API URL";if (Method.equals ("POST")) {string contentType =  "Application/json; Charset=utf-8 "; if (Request.getcontenttype ()! = null) ContentType = Request.getcontenttype (); //gets the null pointer map<string,  string[]> tmp = new HashMap (Request.getparametermap ()); if (Contenttype.tolowercase (). StartsWith ( "multipart/")) { Multiparthttpservletrequest multipartrequest = webutils.getnativerequest (Request, Multiparthttpservletrequest.class); Multipartfile file = multipartrequest.getfile ( "file"); return httpclientupload (URL, file, tmp);} } return null;}        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
Public StringHttpclientupload (String URL, multipartfile file, map<string, string[]>params) throws Clientprotocolexception, IOException {HttpClient HttpClient =New Defaulthttpclient ();Request Processing page HttpPost HttpPost =New HttpPost (URL);Create the file to be processed String fileName = File.getoriginalfilename (); Contentbody files =New Bytearraybody (File.getbytes (), fileName);Populate the requested form field multipartentity reqentity =New Multipartentity (); Reqentity.addpart ("File", files);if (Params! =NULL) {Here's a cursory treatment of values[]For (String key:Params.keyset ()) {string[] values =Params.Get (key);for (int i =0; i < values.length; i++) {StringValue = Values[i];try {value = Urlencoder.encode ( Value,  "UTF-8"); Reqentity.addpart (Key, new stringbody (value));} catch (unsupportedencodingexception e) {//TODO auto-generated Catch block E.printstacktrace (); }}}} //set request Httppost.setentity (reqentity); //execute httpresponse response = Httpclient.execute (HttpPost); if (Httpstatus.sc_ok = Response.getstatusline (). Getstatuscode ()) {HttpEntity entity = Response.getentity (); return entityutils.tostring (Entity, Charset.forname ( "UTF-8" )); } return null;}        
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • +
    • ,
    • ,
    • ,
    • , PNS
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244545
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244541
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244538
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244527
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244528
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244529
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244530

httpclient Implement file Upload Relay

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.