Recently in the Android file upload, requires the form form to submit, the project uses the Afinal framework has the file upload function, but always can not be written with PHP server docking, unable to upload success. Read source discovery: Afinal used a great god to write the Multipartentity.java generated form form content, but the resulting content format is not standard, and there are many problems, such as: first read all the files into memory, and then generate a byte stream written to the socket. So here's the question: what if it's a hundreds of MB file?
Several searches, by this article (has been reproduced by me, but the example code has expired ) inspired , I found the Apache source Httpcomponents-client-4.3.6-src.zip, in an example, found an important component multipartentitybuilder, you can generate form form httpentity, with the Httpentity, no matter what HTTP frame you are, you should be able to use it.
don't know how to use it? Like this:
Httppost httppost = new httppost (URL); httppost.setentity (makemultipartentity (params, files)); Httpresponse response = gethttpclient (). Execute (httppost);...private static Httpclient mclient;private static httpclient gethttpclient () { if (mclient == null) { //if ( BUILD.VERSION.SDK_INT >= 9); //will not take this class of case, based on httpurlconnection if (build.version.sdk_int >= 8) { mclient = androidhttpclient.newinstance (getUserAgent ()); }else { mclient = new defaulthttpclient (); } &nbSp; } return mclient;}
Multipartentitybuilder Usage tidy up as follows:
Need to use Httpmime-4.3.6.jar and Httpcore-4.3.3.jar in Httpcomponents-client-4.3.6-bin.zip.
Public static httpentity makemultipartentity (list<namevaluepair> params, final map<string, file> files) { MultipartEntityBuilder Builder = multipartentitybuilder.create (); builder.setmode ( httpmultipartmode.browser_compatible)//If there are sockettimeoutexception and so on, you can modify this enumeration // Builder.setcharset (Charset.forname ("UTF-8"));//Do not use this will cause the server to receive parameters if (params != null && params.size () > 0) { for (Namevaluepair p : params) { builder.addtextbody (P.getname (), p.getvalue (), ContentType.TEXT_ Plain.withcharset ("UTF-8")); } } if (files != null&Nbsp;&& files.size () > 0) { set <entry<string, file>> entries = files.entryset (); for (entry<string, file> entry : entries) { builder.addpart (Entry.getKey (), new filebody (Entry.getvalue ())); } } return builder.build ();}
An example of Apache is also attached , which can be found in Httpcomponents-client-4.3.6-bin.zip .
/* * ==================================================================== * licensed to the apache software foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * Distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the apache license, version 2.0 (the * "License"); you may not use this file except in compliance * with the license. you may obtain a copy of the license at * * http://www.apache.org/licenses/license-2.0 * * unless required by applicable law or agreed to in writing, * software distributed under the license is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either Express or implied. see the license for the * specific language governing permissions and limitations * under the license. * ==================================================================== * * this software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the apache software foundation, please see *
Android uses Multipartentitybuilder to implement file uploads like form form submission