Android客户端使用的是HttpClient,高版本的sdk已经不支持,新建文件时需要选择低版本的sdk.
public class UploadFileMainActivity extends Activity {
private static String requestURL = "http://10.0.2.2:8080/服务器端工程名/uploadFile.action";
private Button selectImage, uploadImage;
private ImageView imageView;
private String picPath = null;
Bitmap bmp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_file_main);
init();
selectImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*
* 这个是调用android内置的intent,来过滤图片文件 ,同时也可以过滤其他的
* mnt/sdcard/
*/
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}
});
uploadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (picPath == null) {
Toast.makeText(UploadFileMainActivity.this, "请选择图片!", 1000).show();
} else {
uploadFile(picPath);
}
}
});
}
private void uploadFile(String picPath)
{
new AsyncTask<String,Void,Boolean>(){
@Override
protected Boolean doInBackground(String... params) {
boolean bl=false;
try {
bmp=BitmapFactory.decodeFile(params[0]);
HttpClient hc=new DefaultHttpClient();
HttpPost request=new HttpPost(requestURL);
System.out.println(requestURL);
HttpResponse response;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
InputStreamEntity entity = new InputStreamEntity(is, is.available());
request.setEntity(entity);
response = hc.execute(request);
if(response.getStatusLine().getStatusCode()==200){
bl=true;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bl;
}
protected void onPostExecute(boolean bl){
if(bl==true)
Toast.makeText(getApplicationContext(), "图片上传成功!", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "图片上传不成功在!", Toast.LENGTH_SHORT).show();
}
}.execute(picPath);
}
/* 显示Dialog的method */
private void showDialog(String mess)
{
new AlertDialog.Builder(UploadFileMainActivity.this).setTitle("Message")
.setMessage(mess)
.setNegativeButton("确定",null)
.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
/**
* 当选择的图片不为空的话,在获取到图片的途径
*/
Uri uri = data.getData();
try {
String[] pojo = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, pojo, null, null, null);
if (cursor != null) {
ContentResolver cr = this.getContentResolver();
int colunm_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(colunm_index);
/***
* 这里加这样一个判断主要是为了第三方的软件选择,比如:使用第三方的文件管理器的话,你选择的文件就不一定是图片了,
* 这样的话,我们判断文件的后缀名 如果是图片格式的话,那么才可以
*/
if (path.endsWith("jpg") || path.endsWith("png")) {
picPath = path;
Bitmap bitmap = BitmapFactory.decodeStream(cr
.openInputStream(uri));
imageView.setImageBitmap(bitmap);
} else {
alert();
}
} else {
alert();
}
} catch (Exception e) {
}
}
super.onActivityResult(requestCode, resultCode, data);
}
private void alert() {
Dialog dialog = new AlertDialog.Builder(this).setTitle("提示")
.setMessage("您选择的不是有效的图片")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
picPath = null;
}
}).create();
dialog.show();
}
private void init() {
selectImage=(Button)this.findViewById(R.id.selectImage);
uploadImage=(Button)this.findViewById(R.id.uploadImage);
imageView=(ImageView)this.findViewById(R.id.imageView);
}
}
activity_upload_file_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="选择图片"
android:id="@+id/selectImage" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="上传图片"
android:id="@+id/uploadImage" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"/>
</LinearLayout>