Android文件上传,PHP端接收

时间:2025-05-15 22:15:02
  • package ;  
  •   
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  •   
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  •   
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  •   
  • import ;  
  • import ;  
  • import ;  
  •   
  • /** 
  •  *  
  •  * ClassName:UploadActivity Function: TODO 测试上传文件,PHP服务器端接收 Reason: TODO ADD 
  •  * REASON 
  •  *  
  •  * @author Jerome Song 
  •  * @version 
  •  * @since Ver 1.1 
  •  * @Date 2013 2013-4-20 上午8:53:44 
  •  *  
  •  * @see 
  •  */  
  • public class UploadActivity extends Activity implements OnClickListener {  
  •     private final String TAG = "UploadActivity";  
  •   
  •     private static final String path = "/mnt/sdcard/";  
  •     private String uploadUrl = "http://192.168.1.102:8080/Android/";  
  •     private Button btnAsync, btnHttpClient, btnCommonPost;  
  •     private AsyncHttpClient client;  
  •   
  •     @Override  
  •     protected void onCreate(Bundle savedInstanceState) {  
  •         super.onCreate(savedInstanceState);  
  •         setContentView(.activity_upload);  
  •         initView();  
  •         client = new AsyncHttpClient();  
  •     }  
  •   
  •     private void initView() {  
  •         btnCommonPost = (Button) findViewById(.button1);  
  •         btnHttpClient = (Button) findViewById(.button2);  
  •         btnAsync = (Button) findViewById(.button3);  
  •         (this);  
  •         (this);  
  •         (this);  
  •     }  
  •   
  •     @Override  
  •     public void onClick(View v) {  
  •         long startTime = ();  
  •         String tag = null;  
  •         try {  
  •             switch (()) {  
  •             case .button1:  
  •                 upLoadByCommonPost();  
  •                 tag = "CommonPost====>";  
  •                 break;  
  •             case .button2:  
  •                 upLoadByHttpClient4();  
  •                 tag = "HttpClient====>";  
  •                 break;  
  •             case .button3:  
  •                 upLoadByAsyncHttpClient();  
  •                 tag = "AsyncHttpClient====>";  
  •                 break;  
  •             default:  
  •                 break;  
  •             }  
  •         } catch (Exception e) {  
  •             ();  
  •   
  •         }  
  •         (TAG, tag + "wasteTime = "  
  •                 + (() - startTime));  
  •     }  
  •   
  •     /** 
  •      * upLoadByAsyncHttpClient:由人造post上传 
  •      *  
  •      * @return void 
  •      * @throws IOException 
  •      * @throws 
  •      * @since CodingExample Ver 1.1 
  •      */  
  •     private void upLoadByCommonPost() throws IOException {  
  •         String end = "\r\n";  
  •         String twoHyphens = "--";  
  •         String boundary = "******";  
  •         URL url = new URL(uploadUrl);  
  •         HttpURLConnection httpURLConnection = (HttpURLConnection) url  
  •                 .openConnection();  
  •         (128 * 1024);// 128K  
  •         // 允许输入输出流  
  •         (true);  
  •         (true);  
  •         (false);  
  •         // 使用POST方法  
  •         ("POST");  
  •         ("Connection""Keep-Alive");  
  •         ("Charset""UTF-8");  
  •         ("Content-Type",  
  •                 "multipart/form-data;boundary=" + boundary);  
  •   
  •         DataOutputStream dos = new DataOutputStream(  
  •                 ());  
  •         (twoHyphens + boundary + end);  
  •         ("Content-Disposition: form-data; name=\"uploadfile\"; filename=\""  
  •                 + (("/") + 1) + "\"" + end);  
  •         (end);  
  •   
  •         FileInputStream fis = new FileInputStream(path);  
  •         byte[] buffer = new byte[8192]; // 8k  
  •         int count = 0;  
  •         // 读取文件  
  •         while ((count = (buffer)) != -1) {  
  •             (buffer, 0, count);  
  •         }  
  •         ();  
  •         (end);  
  •         (twoHyphens + boundary + twoHyphens + end);  
  •         ();  
  •         InputStream is = ();  
  •         InputStreamReader isr = new InputStreamReader(is, "utf-8");  
  •         BufferedReader br = new BufferedReader(isr);  
  •         String result = ();  
  •         (TAG, result);  
  •         ();  
  •         ();  
  •     }  
  •   
  •     /** 
  •      * upLoadByAsyncHttpClient:由HttpClient4上传 
  •      *  
  •      * @return void 
  •      * @throws IOException 
  •      * @throws ClientProtocolException 
  •      * @throws 
  •      * @since CodingExample Ver 1.1 
  •      */  
  •     private void upLoadByHttpClient4() throws ClientProtocolException,  
  •             IOException {  
  •         HttpClient httpclient = new DefaultHttpClient();  
  •         ().setParameter(  
  •                 CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);  
  •         HttpPost httppost = new HttpPost(uploadUrl);  
  •         File file = new File(path);  
  •         MultipartEntity entity = new MultipartEntity();  
  •         FileBody fileBody = new FileBody(file);  
  •         ("uploadfile", fileBody);  
  •         (entity);  
  •         HttpResponse response = (httppost);  
  •         HttpEntity resEntity = ();  
  •         if (resEntity != null) {  
  •             (TAG, (resEntity));  
  •         }  
  •         if (resEntity != null) {  
  •             ();  
  •         }  
  •         ().shutdown();  
  •     }  
  •   
  •     /** 
  •      * upLoadByAsyncHttpClient:由AsyncHttpClient框架上传 
  •      *  
  •      * @return void 
  •      * @throws FileNotFoundException 
  •      * @throws 
  •      * @since CodingExample Ver 1.1 
  •      */  
  •     private void upLoadByAsyncHttpClient() throws FileNotFoundException {  
  •         RequestParams params = new RequestParams();  
  •         ("uploadfile"new File(path));  
  •         (uploadUrl, params, new AsyncHttpResponseHandler() {  
  •             @Override  
  •             public void onSuccess(int arg0, String arg1) {  
  •                 super.onSuccess(arg0, arg1);  
  •                 (TAG, arg1);  
  •             }  
  •         });  
  •     }  
  •   
  • }