安卓端上传图片到服务器php接收

时间:2022-08-28 22:18:48

        之前开发用的,做个记录,但其中还有一些问题没有解决,如:不是每张图片都能上传,在不同的机型上有些格式的图片上传不了。(我怀疑是php端代码的问题)


import java.io.File;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    public static final int SHOW_RESPONSE = 0;

    private Button sendRequest;

    private TextView responseText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        sendRequest = (Button) findViewById(R.id.send_request);
        sendRequest.setOnClickListener(this);  
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.send_request) {
            try {
                uploadFile();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
//=========================================================================================================================================================
//传输图片,需要导入httpmime-4.1.3jar包
    private void uploadFile() throws Exception  
    {  
            String uploadUrl ="http://app.weomg.com/index.php/Home/Company/demo";//接收图片的php代码
            String srcPath ="";//本地图片的路径
            
            //php端代码
//            public function demo(){
//            $target_path  = "./";
//            $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
//            if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
//                echo "The file ".  basename( $_FILES['uploadedfile']['name']).
//                " has been uploaded";
//                } else{
//                    echo "There was an error uploading the file, please try again!";
//                }
//            }
            
            HttpClient httpclient = new DefaultHttpClient();  
            httpclient.getParams().setParameter(  
                    CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);  
            HttpPost httppost = new HttpPost(uploadUrl);  

            MultipartEntity entity = new MultipartEntity();  

            File file = new File(srcPath);  
            FileBody fileBody = new FileBody(file);  
            entity.addPart("uploadedfile", fileBody);  

            httppost.setEntity(entity);  
            HttpResponse response = httpclient.execute(httppost);  

            HttpEntity resEntity = response.getEntity();  
            if (resEntity != null) {              
                Toast.makeText(this, EntityUtils.toString(resEntity), Toast.LENGTH_LONG).show();  
            }  
      
            httpclient.getConnectionManager().shutdown();  
      
    }
}