Android 上传文件
上传文件其实就通过文件中数据,通过tcp/ip协议,把内容传到服务器中。
可以单独的利用java中的socket或者是urlconnection进行io输入输出就可以了。
但是做过web的都知道,可以通过form表单进行上传文件。
<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html>
这样可以利用androidapi自带的httpclient进行模仿web中的form表单进行提交。
package com.example.testandroidupload; import java.io.File; import java.io.FileInputStream; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.HttpVersion; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.InputStreamBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.params.CoreProtocolPNames; import org.apache.http.params.HttpParams; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread() { public void run() { try { doUPload(); } catch (Exception e) { e.printStackTrace(); } }; }.start(); } public HttpClient createClient() { HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, HTTP.DEFAULT_CONTENT_CHARSET); params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true); params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30 * 1000); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 30 * 1000); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg); return new DefaultHttpClient(conMgr, params); } void doUPload() throws Exception { // 需要上传的文件 File file = new File("/mnt/sdcard/test.png"); FileInputStream fis = new FileInputStream(file); HttpClient httpclient = createClient(); String url = ""; MultipartEntity me = new MultipartEntity(); me.addPart("file", new InputStreamBody(fis, file.getName())); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(me); HttpResponse httpResponse = httpclient.execute(httpPost); int code = httpResponse.getStatusLine().getStatusCode(); log("http status code:" + code); if (code == HttpStatus.SC_OK) { String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8); // 上传的结果,可以使json,或者是返回上传后文件的的url log("result = " + result); log("上传成功"); } } static void log(String msg) { Log.i("test", msg); } }
上面的代码中需要MultipartEntity,这个是第三方jar文件(httpmime-4.1.1.jar),需要添加到libs文件夹中。