android通過http上傳文件到web服務(wù)器端
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
本文章介紹了一個(gè)關(guān)于android通過http來實(shí)現(xiàn)文件上傳功能,在服務(wù)器端我們是用php來實(shí)現(xiàn)的,有需要的朋友可以參考一下 代碼如下 <?php ///如果有上傳文件則接收 if($_FILES){ $target_path = $target_path . basename( $_FILES['file1']['name']); try{ if(move_uploaded_file($_FILES['file1']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['file1']['name']). " has been uploaded"; } } catch( Exception $e ) { echo $e->getMessage(); } } ?> Android 代碼: package com.nbcio.baishicha.test; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; public class test extends Activity { /* * 變量聲明 newName:上傳后在服務(wù)器上的文件名稱 * * uploadFile:要上傳的文件路徑 actionUrl:服務(wù)器對(duì)應(yīng)的程序路徑 */ private String newName = ""; private String uploadFile = ""; private String actionUrl = "http://www.hzhuti.com/index.php";//這里定義你的上傳路徑 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); newName = bundle.getString("fileName"); uploadFile = newName; try { String ok = post(actionUrl, newName); Toast.makeText(this, "OK!", Toast.LENGTH_LONG).show(); finish(); } catch (IOException e) { // // TODO Auto-generated catch block e.printStackTrace(); } } /* 上傳文件到Server的方法 */ /** * * @param actionUrl * @param params * @param files * @return * @throws IOException */ public static String post(String actionUrl, String FileName) throws IOException { String BOUNDARY = java.util.UUID.randomUUID().toString(); String PREFIX = "--", LINEND = "rn"; String MULTIPART_FROM_DATA = "multipart/form-data"; String CHARSET = "UTF-8"; URL uri = new URL(actionUrl); HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); conn.setReadTimeout(5 * 1000); // 緩存的最長(zhǎng)時(shí)間 conn.setDoInput(true);// 允許輸入 conn.setDoOutput(true);// 允許輸出 conn.setUseCaches(false); // 不允許使用緩存 conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY); DataOutputStream outStream = new DataOutputStream( conn.getOutputStream()); // 發(fā)送文件數(shù)據(jù) if (FileName != "") { StringBuilder sb1 = new StringBuilder(); sb1.append(PREFIX); sb1.append(BOUNDARY); sb1.append(LINEND); sb1.append("Content-Disposition: form-data; name="file1"; filename="" + FileName + """ + LINEND); sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND); sb1.append(LINEND); outStream.write(sb1.toString().getBytes()); InputStream is = new FileInputStream(FileName); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { outStream.write(buffer, 0, len); } is.close(); outStream.write(LINEND.getBytes()); } // 請(qǐng)求結(jié)束標(biāo)志 byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes(); outStream.write(end_data); outStream.flush(); // 得到響應(yīng)碼 int res = conn.getResponseCode(); InputStream in = null; if (res == 200) { in = conn.getInputStream(); int ch; StringBuilder sb2 = new StringBuilder(); while ((ch = in.read()) != -1) { sb2.append((char) ch); } } return in == null ? null : in.toString(); } }
該文章在 2013/2/25 12:48:32 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |