android对于上传文件,还是很简单的,和java里面的上传都是一样的,基本上都是熟悉操作输出流和输入流!还有一个特别重要的就是需要一些 content-type这些参数的配置! 如果这些都弄好了,上传就很简单了! 下面是我写的一个上传的工具类:
001 |
package com.spring.sky.image.upload.network; |
003 |
import java.io.DataOutputStream; |
005 |
import java.io.FileInputStream; |
006 |
import java.io.IOException; |
007 |
import java.io.InputStream; |
008 |
import java.net.HttpURLConnection; |
009 |
import java.net.MalformedURLException; |
011 |
import java.util.UUID; |
013 |
import android.util.Log; |
019 |
* Email:vipa1888@163.com |
023 |
public class UploadUtil { |
024 |
private static final String TAG = "uploadFile" ; |
025 |
private static final int TIME_OUT = 10 * 1000 ; |
026 |
private static final String CHARSET = "utf-8" ; |
029 |
* @param file 需要上传的文件 |
030 |
* @param RequestURL 请求的rul |
033 |
public static String uploadFile(File file,String RequestURL) |
035 |
String result = null ; |
036 |
String BOUNDARY = UUID.randomUUID().toString(); |
037 |
String PREFIX = "--" , LINE_END = "\r\n" ; |
038 |
String CONTENT_TYPE = "multipart/form-data" ; |
041 |
URL url = new URL(RequestURL); |
042 |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
043 |
conn.setReadTimeout(TIME_OUT); |
044 |
conn.setConnectTimeout(TIME_OUT); |
045 |
conn.setDoInput( true ); |
046 |
conn.setDoOutput( true ); |
047 |
conn.setUseCaches( false ); |
048 |
conn.setRequestMethod( "POST" ); |
049 |
conn.setRequestProperty( "Charset" , CHARSET); |
050 |
conn.setRequestProperty( "connection" , "keep-alive" ); |
051 |
conn.setRequestProperty( "Content-Type" , CONTENT_TYPE + ";boundary=" + BOUNDARY); |
058 |
DataOutputStream dos = new DataOutputStream( conn.getOutputStream()); |
059 |
StringBuffer sb = new StringBuffer(); |
065 |
* name里面的值为服务器端需要key 只有这个key 才可以得到对应的文件 |
066 |
* filename是文件的名字,包含后缀名的 比如:abc.png |
069 |
sb.append( "Content-Disposition: form-data; name=\"img\"; filename=\"" +file.getName()+ "\"" +LINE_END); |
070 |
sb.append( "Content-Type: application/octet-stream; charset=" +CHARSET+LINE_END); |
072 |
dos.write(sb.toString().getBytes()); |
073 |
InputStream is = new FileInputStream(file); |
074 |
byte [] bytes = new byte [ 1024 ]; |
076 |
while ((len=is.read(bytes))!=- 1 ) |
078 |
dos.write(bytes, 0 , len); |
081 |
dos.write(LINE_END.getBytes()); |
082 |
byte [] end_data = (PREFIX+BOUNDARY+PREFIX+LINE_END).getBytes(); |
089 |
int res = conn.getResponseCode(); |
090 |
Log.e(TAG, "response code:" +res); |
093 |
Log.e(TAG, "request success" ); |
094 |
InputStream input = conn.getInputStream(); |
095 |
StringBuffer sb1= new StringBuffer(); |
097 |
while ((ss=input.read())!=- 1 ) |
099 |
sb1.append(( char )ss); |
101 |
result = sb1.toString(); |
102 |
Log.e(TAG, "result : " + result); |
108 |
} catch (MalformedURLException e) { |
110 |
} catch (IOException e) { |
参数就一个File文件和一个请求上传的URL,不过需要注意的是 ,因为需要用到了网络请求,所以大家可不要忘记在上传的时候,给android客户端加一个访问王珞丹呃权限哦!
还有一点就是需要大家注意一下:本人是做服务器端javaEE的,我发现在上传的过程中,如果文件的标识name是java关键字之类的,上传过程中,会存在很多位置的问题的!所以大家经可能的不要使用关键字哦!
下面是Activity的代码:
001 |
package com.spring.sky.image.upload; |
006 |
import com.spring.sky.image.upload.network.UploadUtil; |
008 |
import android.app.Activity; |
009 |
import android.app.AlertDialog; |
010 |
import android.app.Dialog; |
011 |
import android.content.ContentResolver; |
012 |
import android.content.DialogInterface; |
013 |
import android.content.Intent; |
014 |
import android.database.Cursor; |
015 |
import android.graphics.Bitmap; |
016 |
import android.graphics.BitmapFactory; |
017 |
import android.net.Uri; |
018 |
import android.os.Bundle; |
019 |
import android.provider.MediaStore; |
020 |
import android.util.Log; |
021 |
import android.view.View; |
022 |
import android.view.View.OnClickListener; |
023 |
import android.widget.Button; |
024 |
import android.widget.ImageView; |
028 |
* Email:vipa1888@163.com |
033 |
public class MainActivity extends Activity implements OnClickListener{ |
034 |
private static final String TAG = "uploadImage" ; |
035 |
private static String requestURL = "http://192.168.1.14:8080/SetBlobData/img!up" ; |
036 |
private Button selectImage,uploadImage; |
037 |
private ImageView imageView; |
039 |
private String picPath = null ; |
041 |
/** Called when the activity is first created. */ |
043 |
public void onCreate(Bundle savedInstanceState) { |
044 |
super .onCreate(savedInstanceState); |
045 |
setContentView(R.layout.main); |
047 |
selectImage = (Button) this .findViewById(R.id.selectImage); |
048 |
uploadImage = (Button) this .findViewById(R.id.uploadImage); |
049 |
selectImage.setOnClickListener( this ); |
050 |
uploadImage.setOnClickListener( this ); |
052 |
imageView = (ImageView) this .findViewById(R.id.imageView); |
058 |
public void onClick(View v) { |
060 |
case R.id.selectImage: |
062 |
* 这个是调用android内置的intent,来过滤图片文件 ,同时也可以过滤其他的 |
064 |
Intent intent = new Intent(); |
065 |
intent.setType( "image/*" ); |
066 |
intent.setAction(Intent.ACTION_GET_CONTENT); |
067 |
startActivityForResult(intent, 1 ); |
069 |
case R.id.uploadImage: |
070 |
File file = new File(picPath); |
073 |
String request = UploadUtil.uploadFile( file, requestURL); |
074 |
uploadImage.setText(request); |
083 |
protected void onActivityResult( int requestCode, int resultCode, Intent data) { |
084 |
if (resultCode==Activity.RESULT_OK) |
087 |
* 当选择的图片不为空的话,在获取到图片的途径 |
089 |
Uri uri = data.getData(); |
090 |
Log.e(TAG, "uri = " + uri); |
092 |
String[] pojo = {MediaStore.Images.Media.DATA}; |
094 |
Cursor cursor = managedQuery(uri, pojo, null , null , null ); |
097 |
ContentResolver cr = this .getContentResolver(); |
098 |
int colunm_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); |
099 |
cursor.moveToFirst(); |
100 |
String path = cursor.getString(colunm_index); |
102 |
* 这里加这样一个判断主要是为了第三方的软件选择,比如:使用第三方的文件管理器的话,你选择的文件就不一定是图片了,这样的话,我们判断文件的后缀名 |
105 |
if (path.endsWith( "jpg" )||path.endsWith( "png" )) |
108 |
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri)); |
109 |
imageView.setImageBitmap(bitmap); |
113 |
} catch (Exception e) { |
117 |
super .onActivityResult(requestCode, resultCode, data); |
122 |
Dialog dialog = new AlertDialog.Builder( this ) |
124 |
.setMessage( "您选择的不是有效的图片" ) |
125 |
.setPositiveButton( "确定" , |
126 |
new DialogInterface.OnClickListener() { |
127 |
public void onClick(DialogInterface dialog, |
layout代码:
01 |
<? xml version = "1.0" encoding = "utf-8" ?> |
02 |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 |
android:orientation = "vertical" |
04 |
android:layout_width = "fill_parent" |
05 |
android:layout_height = "fill_parent" |
08 |
android:layout_width = "fill_parent" |
09 |
android:layout_height = "wrap_content" |
11 |
android:id = "@+id/selectImage" |
14 |
android:layout_width = "fill_parent" |
15 |
android:layout_height = "wrap_content" |
17 |
android:id = "@+id/uploadImage" |
20 |
android:layout_width = "wrap_content" |
21 |
android:layout_height = "wrap_content" |
22 |
android:id = "@+id/imageView" |
以上就是android 上传图片的全部代码,如果想上传其他文件的话,可以修改过滤条件就可以了,同时文件的类型一定要和服务器端的文件类型保持一致,否则上传就失败了!