[Multifile upload 3] Using urlencodedformentity forms

Source: Internet
Author: User
Import Java. io. bytearrayoutputstream; import Java. io. file; import Java. lang. ref. weakreference; import Java. util. arraylist; import Java. util. list; import Org. apache. HTTP. httpentity; import Org. apache. HTTP. httpresponse; import Org. apache. HTTP. namevaluepair; import Org. apache. HTTP. client. httpclient; import Org. apache. HTTP. client. entity. urlencodedformentity; import Org. apache. HTTP. client. methods. httppost; import Org. apache. HTTP. impl. client. defaulthttpclient; import Org. apache. HTTP. message. basicnamevaluepair; import Org. apache. HTTP. util. entityutils; import COM. lxb. uploadwithprogress. tool. base64coder; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. media. thumbnailutils; import android. util. log;/*** urlencodedformentity upload */public class httpurlencodedformentitypost {/*** upload * @ Param pathlist */public static void upload (list <string> pathlist) {string files = "fileupload:"; for (INT I = 0; I <pathlist. size (); I ++) {log. I ("httpurlencodedformentitypost", "filepath:" + pathlist. get (I); // generate A Bitmap bitmap tbitmap = converttobitmap (pathlist. get (I), 400,400); // write bitmap into the stream bytearrayoutputstream stream = new bytearrayoutputstream (); tbitmap. compress (bitmap. compressformat. jpe G, 100, stream); // converts the stream into an array byte [] B = stream. tobytearray (); // stores the image stream as a string. String file = new string (base64coder. encodelines (B); // set a split line file + = "--------------------------- 7da4247580612"; // accumulate the string data files converted from each file + = file ;} httpclient client = new defaulthttpclient (); // set the upload parameter list <namevaluepair> formparams = new arraylist <namevaluepair> (); formparams. add (New basicnamevaluepair ("Files", files); httppo St post = new httppost ("http: // 10.147.142.78: 8080/fileuploadservlet/fileupload? Tag = uefe "); urlencodedformentity entity; try {entity = new urlencodedformentity (formparams," UTF-8 "); Post. addheader ("accept", "text/JavaScript, text/html, application/XML, text/XML"); Post. addheader ("Accept-charset", "GBK, UTF-8; q = 0.7, *; q = 0.3"); Post. addheader ("Accept-encoding", "gzip, deflate, SDCh"); Post. addheader ("connection", "keep-alive"); Post. addheader ("cache-control", "No-Cache"); Post. addheader ("Content-Type", "application/X-WWW-form-urlencoded"); Post. setentity (entity); httpresponse response = client.exe cute (post); log. I ("httpurlencodedformentitypost", "statuscode:" + response. getstatusline (). getstatuscode (); httpentity E = response. getentity (); log. I ("httpurlencodedformentitypost", "response:" + entityutils. tostring (e); If (200 = response. getstatusline (). getstatuscode () {toast. maketast (activity, "uploaded", 1000 ). show;} else {toast. maketast (activity, "Upload Failed", 1000 ). show ();} client. getconnectionmanager (). shutdown ();} catch (exception e) {e. printstacktrace ();}} /*** generate a bitmap * @ Param path * @ Param W specified width * @ Param h specified height * @ return */public static bitmap converttobitmap (string path, int W, int h) {bitmapfactory. options opts = new bitmapfactory. options (); // set to true to retrieve only the image size opts. injustdecodebounds = true; // The returned bitmapfactory is null. decodefile (path, opts); int width = opts. outwidth; int Height = opts. outheight; int insamplesize = 1; if (height> w | width> H) {If (width> height) {insamplesize = math. round (float) height/(float) H);} else {insamplesize = math. round (float) width/(float) W) ;}} opts. insamplesize = insamplesize; opts. injustdecodebounds = false; return bitmapfactory. decodefile (path, opts );}}

A base64coder. Java is required.

Usage

List<String> filePathList = new ArrayList<String>();filePathList.add(Environment.getExternalStorageDirectory()+ "/baidu/cache/image/" + "12778991134.png");filePathList.add(Environment.getExternalStorageDirectory()+ "/baidu/cache/image/" + "123457894146.png");new Thread(new Runnable() {    public void run() {        HttpUrlEncodedFormEntityPost.upload(filePathList);    }}).start();


[Multifile upload 3] Using urlencodedformentity forms

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.