Android学习之客户端上传图片到服务器

根据项目需求,需要上传相册和拍照的图片,参考和借鉴了别人的资料和代码,简单地功能实现了。
一、 HTTP   multipart/form-data—— 上传报文格式
我们来了解下Android客户端如何通过HTTP协议来将图片上传到服务器。我们先来了解下 HTTP   multipart/form-data, 上传报文格式
假设接收文件的网页程序是http://172.31.8.6:8080/fileUpload/file_upload。假设我们要发送一个图片文件,文件名为“iamge.jpg”。
首先,客户端成功链接172.31.8.6后,会发送如下的HTTP请求:

      POST/http://172.31.8.6:8080/fileUpload/file_upload HTTP/1.1 

  Accept: text/plain, */* 
  Accept-Language: zh-cn 

  Host: 172.31.8.6
  Content-Type:multipart/form-data;boundary=-----------------------------7db372eb000e2
  User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
  Content-Length: 3693
  Connection: Keep-Alive

  -------------------------------7db372eb000e2

  Content-Disposition: form-data; name="img"; filename="image.jpg"

  Content-Type: image/jpeg

  (此处省略jpeg文件二进制数据...)

  -------------------------------7db372eb000e2--

此内容必须一字不差,包括最后的回车,红色字体部分就是协议的头。给服务器上传数据时,并非协议头每个字段都得说明,其中,content-type是必须的,它包括一个类似标志性质的名为boundary的标志,它可以是随便输入的字符串。对后面的具体内容也是必须的。它用来分辨一段内容的开始。Content-Length: 3693 ,这里的3693是要上传文件的总长度。绿色字体部分就是需要上传的数据,可以是文本,也可以是图片等。数据内容前面需要有Content-Disposition, Content-Type以及Content-Transfer-Encoding等说明字段。最后的紫色部分就是协议的结尾了。
注意这一行:
   Content-Type: multipart/form-data; boundary=---------------------------7db372eb000e2  
  根据 rfc1867, multipart/form-data是必须的. 
  ---------------------------7db372eb000e2 是分隔符,分隔多个文件、表单项。其中b372eb000e2 是即时生成的一个数字,用以确保整个分隔符不会在文件或表单项的内容中出现。Form每个部分用分隔符分割,分隔符之前必须加上"--"着两个字符(即--{boundary})才能被http协议认为是Form的分隔符,表示结束的话用在正确的分隔符后面添加"--"表示结束。
  前面的 ---------------------------7d 是 IE 特有的标志,Mozila 为---------------------------71. 
  每个分隔的数据的都可以用Content-Type来表示下面数据的类型,可以参考rfc1341( http://www.ietf.org/rfc/rfc1341.txt)
  例如 :Contect-Type:image/jpeg 表示下面的数据是jpeg文件数据。
二、客户端源码
MainActivity.java

package com.fg.activity; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.fg.util.UploadUtil; import com.fg.util.UploadUtil.OnUploadProcessListener; /** * @author ICQwlj<br> * Email :wlj250237@126.com<br> * 说明:主要用于选择文件和上传文件操作 */ public class MainActivity extends Activity implements OnClickListener,OnUploadProcessListener{ private static final String TAG = "uploadImage"; /** * 去上传文件 */ protected static final int TO_UPLOAD_FILE = 1; /** * 上传文件响应 */ protected static final int UPLOAD_FILE_DONE = 2; // /** * 选择文件 */ public static final int TO_SELECT_PHOTO = 3; /** * 上传初始化 */ private static final int UPLOAD_INIT_PROCESS = 4; /** * 上传中 */ private static final int UPLOAD_IN_PROCESS = 5; /*** * 这里的这个URL是我服务器的javaEE环境URL */ private static String requestURL = "http://172.31.8.6:8080/fileUpload/file_upload"; private Button selectButton,uploadButton; private ImageView imageView; private TextView uploadImageResult; private String picPath = null; private ProgressDialog progressDialog; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); } /** * 初始化数据 */ private void initView() { selectButton = (Button) this.findViewById(R.id.selectImage); uploadButton = (Button) this.findViewById(R.id.uploadImage); selectButton.setOnClickListener(this); uploadButton.setOnClickListener(this); imageView = (ImageView) this.findViewById(R.id.imageView); uploadImageResult = (TextView) findViewById(R.id.uploadImageResult); progressDialog = new ProgressDialog(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.selectImage: Intent intent = new Intent(this,SelectPicActivity.class); startActivityForResult(intent, TO_SELECT_PHOTO); break; case R.id.uploadImage: if(picPath!=null) { handler.sendEmptyMessage(TO_UPLOAD_FILE); }else{ Toast.makeText(this, "上传的文件路径出错", Toast.LENGTH_LONG).show(); } break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode==Activity.RESULT_OK && requestCode == TO_SELECT_PHOTO) { picPath = data.getStringExtra(SelectPicActivity.KEY_PHOTO_PATH); Log.i(TAG, "最终选择的图片="+picPath); Bitmap bm = BitmapFactory.decodeFile(picPath); imageView.setImageBitmap(bm);

//更新图库

Uri localUri = Uri.fromFile(new File(picPath)); Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri); sendBroadcast(localIntent); } super.onActivityResult(requestCode, resultCode, data); } /** * 上传服务器响应回调 */ @Override public void onUploadDone(int responseCode, String message) { progressDialog.dismiss(); Message msg = Message.obtain(); msg.what = UPLOAD_FILE_DONE; msg.arg1 = responseCode; msg.obj = message; handler.sendMessage(msg); } private void toUploadFile() { uploadImageResult.setText("正在上传中..."); progressDialog.setMessage("正在上传文件..."); progressDialog.show(); String fileKey = "img"; UploadUtil uploadUtil = UploadUtil.getInstance(); uploadUtil.setOnUploadProcessListener(this); //设置监听器监听上传状态 Map<String, String> params = new HashMap<String, String>(); params.put("orderId", "11111"); uploadUtil.uploadFile( picPath,fileKey, requestURL,params); } @Override public void onUploadProcess(int uploadSize) { Message msg = Message.obtain(); msg.what = UPLOAD_IN_PROCESS; msg.arg1 = uploadSize; handler.sendMessage(msg); } @Override public void initUpload(int fileSize) { Message msg = Message.obtain(); msg.what = UPLOAD_INIT_PROCESS; msg.arg1 = fileSize; handler.sendMessage(msg ); } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case TO_UPLOAD_FILE: toUploadFile(); break; case UPLOAD_INIT_PROCESS: break; case UPLOAD_IN_PROCESS: break; case UPLOAD_FILE_DONE: String result = "响应码:"+msg.arg1+"\n响应信息:"+msg.obj+"\n耗时:"+UploadUtil.getRequestTime()+"秒"; uploadImageResult.setText(result); break;

default: break; } super.handleMessage(msg); } }; }

SelectPicActivity.java

package com.fg.activity; import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; /** * @author ICQwlj<br> * Email :wlj250237@126.com<br> * 说明:主要用于选择文件操作 */ public class SelectPicActivity extends Activity implements OnClickListener{ /*** * 使用照相机拍照获取图片 */ public static final int SELECT_PIC_BY_TACK_PHOTO = 1; /*** * 使用相册中的图片 */ public static final int SELECT_PIC_BY_PICK_PHOTO = 2; /*** * 从Intent获取图片路径的KEY */ public static final String KEY_PHOTO_PATH = "photo_path"; private static final String TAG = "SelectPicActivity"; private LinearLayout dialogLayout; private Button takePhotoBtn,pickPhotoBtn,cancelBtn; /**获取到的图片路径*/ private String picPath; private Intent lastIntent ; private Uri photoUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.select_pic_layout); initView(); } /** * 初始化加载View */ private void initView() { dialogLayout = (LinearLayout) findViewById(R.id.dialog_layout); dialogLayout.setOnClickListener(this); takePhotoBtn = (Button) findViewById(R.id.btn_take_photo); takePhotoBtn.setOnClickListener(this); pickPhotoBtn = (Button) findViewById(R.id.btn_pick_photo); pickPhotoBtn.setOnClickListener(this); cancelBtn = (Button) findViewById(R.id.btn_cancel); cancelBtn.setOnClickListener(this); lastIntent = getIntent(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.dialog_layout: finish(); break; case R.id.btn_take_photo: takePhoto(); break; case R.id.btn_pick_photo: pickPhoto(); break; default: finish(); break; } } /** * 拍照获取图片 */ private void takePhoto() { //执行拍照前,应该先判断SD卡是否存在 String SDState = Environment.getExternalStorageState(); if(SDState.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE" /*** * 需要说明一下,以下操作使用照相机拍照,拍照后的图片会存放在相册中的 * 这里使用的这种方式有一个好处就是获取的图片是拍照后的原图 * 如果不实用ContentValues存放照片路径的话,拍照后获取的图片为缩略图不清晰 */ ContentValues values = new ContentValues(); photoUri = this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri); /**-----------------*/ startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO); }else{ Toast.makeText(this,"内存卡不存在", Toast.LENGTH_LONG).show(); } } /*** * 从相册中取图片 */ private void pickPhoto() { Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO); } @Override public boolean onTouchEvent(MotionEvent event) { finish(); return super.onTouchEvent(event); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == Activity.RESULT_OK) { doPhoto(requestCode,data); } super.onActivityResult(requestCode, resultCode, data); } /** * 选择图片后,获取图片的路径 * @param requestCode * @param data */ private void doPhoto(int requestCode,Intent data) { if(requestCode == SELECT_PIC_BY_PICK_PHOTO ) //从相册取图片,有些手机有异常情况,请注意 { if(data == null) { Toast.makeText(this, "选择图片文件出错", Toast.LENGTH_LONG).show(); return; } photoUri = data.getData(); if(photoUri == null ) { Toast.makeText(this, "选择图片文件出错", Toast.LENGTH_LONG).show(); return; } } String[] pojo = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(photoUri, pojo, null, null,null); if(cursor != null ) { int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]); cursor.moveToFirst(); picPath = cursor.getString(columnIndex); cursor.close(); } Log.i(TAG, "imagePath = "+picPath); if(picPath != null && ( picPath.endsWith(".png") || picPath.endsWith(".PNG") ||picPath.endsWith(".jpg") ||picPath.endsWith(".JPG") )) { lastIntent.putExtra(KEY_PHOTO_PATH, picPath); setResult(Activity.RESULT_OK, lastIntent); finish(); }else{ Toast.makeText(this, "选择图片文件不正确", Toast.LENGTH_LONG).show(); } } }

UploadUtil.java

package com.fg.util; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import java.util.Map; import java.util.UUID; import android.util.Log; /** * * 上传工具类 * @author ICQwlj<br> * Email :wlj250237@126.com<br> * 支持上传文件和参数 */ public class UploadUtil { private static UploadUtil uploadUtil; private static final String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成 private static final String PREFIX = "--"; private static final String LINE_END = "\r\n"; private static final String CONTENT_TYPE = "multipart/form-data"; // 内容类型 private UploadUtil() { } /** * 单例模式获取上传工具类 * @return */ public static UploadUtil getInstance() { if (null == uploadUtil) { uploadUtil = new UploadUtil(); } return uploadUtil; } private static final String TAG = "UploadUtil"; private int readTimeOut = 10 * 1000; // 读取超时 private int connectTimeout = 10 * 1000; // 超时时间 /*** * 请求使用多长时间 */ private static int requestTime = 0; private static final String CHARSET = "utf-8"; // 设置编码 /*** * 上传成功 */ public static final int UPLOAD_SUCCESS_CODE = 1; /** * 文件不存在 */ public static final int UPLOAD_FILE_NOT_EXISTS_CODE = 2; /** * 服务器出错 */ public static final int UPLOAD_SERVER_ERROR_CODE = 3; protected static final int WHAT_TO_UPLOAD = 1; protected static final int WHAT_UPLOAD_DONE = 2; /** * android上传文件到服务器 * * @param filePath * 需要上传的文件的路径 * @param fileKey * 在网页上<input type=file name=xxx/> xxx就是这里的fileKey * @param RequestURL * 请求的URL */ public void uploadFile(String filePath, String fileKey, String RequestURL, Map<String, String> param) { if (filePath == null) { sendMessage(UPLOAD_FILE_NOT_EXISTS_CODE,"文件不存在"); return; } try { File file = new File(filePath); uploadFile(file, fileKey, RequestURL, param); } catch (Exception e) { sendMessage(UPLOAD_FILE_NOT_EXISTS_CODE,"文件不存在"); e.printStackTrace(); return; } } /** * android上传文件到服务器 * * @param file * 需要上传的文件 * @param fileKey * 在网页上<input type=file name=xxx/> xxx就是这里的fileKey * @param RequestURL * 请求的URL */ public void uploadFile(final File file, final String fileKey, final String RequestURL, final Map<String, String> param) { if (file == null || (!file.exists())) { sendMessage(UPLOAD_FILE_NOT_EXISTS_CODE,"文件不存在"); return; } Log.i(TAG, "请求的URL=" + RequestURL); Log.i(TAG, "请求的fileName=" + file.getName()); Log.i(TAG, "请求的fileKey=" + fileKey); new Thread(new Runnable() { //开启线程上传文件 @Override public void run() { toUploadFile(file, fileKey, RequestURL, param); } }).start(); } private void toUploadFile(File file, String fileKey, String RequestURL, Map<String, String> param) { String result = null; requestTime= 0; long requestTime = System.currentTimeMillis(); long responseTime = 0; try { URL url = new URL(RequestURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(readTimeOut); conn.setConnectTimeout(connectTimeout); conn.setDoInput(true); // 允许输入流 conn.setDoOutput(true); // 允许输出流 conn.setUseCaches(false); // 不允许使用缓存 conn.setRequestMethod("POST"); // 请求方式 conn.setRequestProperty("Charset", CHARSET); // 设置编码 conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY); /** * 当文件不为空,把文件包装并且上传 */ DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); StringBuffer sb = null; String params = ""; /*** * 以下是用于上传参数 */ if (param != null && param.size() > 0) { Iterator<String> it = param.keySet().iterator(); while (it.hasNext()) { sb = null; sb = new StringBuffer(); String key = it.next(); String value = param.get(key); sb.append(PREFIX).append(BOUNDARY).append(LINE_END); sb.append("Content-Disposition: form-data; name=\"").append(key).append("\"").append(LINE_END).append(LINE_END); sb.append(value).append(LINE_END); params = sb.toString(); Log.i(TAG, key+"="+params+"##"); dos.write(params.getBytes()); } } sb = null; params = null; sb = new StringBuffer(); /** * 这里重点注意: name里面的值为服务器端需要key 只有这个key 才可以得到对应的文件 * filename是文件的名字,包含后缀名的 比如:abc.png */ sb.append(PREFIX).append(BOUNDARY).append(LINE_END); sb.append("Content-Disposition:form-data; name=\"" + fileKey + "\"; filename=\"" + file.getName() + "\"" + LINE_END); sb.append("Content-Type:image/pjpeg" + LINE_END); // 这里配置的Content-type很重要的 ,用于服务器端辨别文件的类型的 sb.append(LINE_END); params = sb.toString(); sb = null; Log.i(TAG, file.getName()+"=" + params+"##"); dos.write(params.getBytes()); /**上传文件*/ InputStream is = new FileInputStream(file); onUploadProcessListener.initUpload((int)file.length()); byte[] bytes = new byte[1024]; int len = 0; int curLen = 0; while ((len = is.read(bytes)) != -1) { curLen += len; dos.write(bytes, 0, len); onUploadProcessListener.onUploadProcess(curLen); } is.close(); dos.write(LINE_END.getBytes()); byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END).getBytes(); dos.write(end_data); dos.flush();

/** * 获取响应码 200=成功 当响应成功,获取响应的流 */ int res = conn.getResponseCode(); responseTime = System.currentTimeMillis(); this.requestTime = (int) ((responseTime-requestTime)/1000); Log.e(TAG, "response code:" + res); if (res == 200) { Log.e(TAG, "request success"); InputStream input = conn.getInputStream(); StringBuffer sb1 = new StringBuffer(); int ss; while ((ss = input.read()) != -1) { sb1.append((char) ss); } result = sb1.toString(); Log.e(TAG, "result : " + result); sendMessage(UPLOAD_SUCCESS_CODE, "上传结果:" + result); return; } else { Log.e(TAG, "request error"); sendMessage(UPLOAD_SERVER_ERROR_CODE,"上传失败:code=" + res); return; } } catch (MalformedURLException e) { sendMessage(UPLOAD_SERVER_ERROR_CODE,"上传失败:error=" + e.getMessage()); e.printStackTrace(); return; } catch (IOException e) { sendMessage(UPLOAD_SERVER_ERROR_CODE,"上传失败:error=" + e.getMessage()); e.printStackTrace(); return; } } /** * 发送上传结果 * @param responseCode * @param responseMessage */ private void sendMessage(int responseCode,String responseMessage) { onUploadProcessListener.onUploadDone(responseCode, responseMessage); } /** * 下面是一个自定义的回调函数,用到回调上传文件是否完成 * * @author shimingzheng * */ public static interface OnUploadProcessListener { /** * 上传响应 * @param responseCode * @param message */ void onUploadDone(int responseCode, String message); /** * 上传中 * @param uploadSize */ void onUploadProcess(int uploadSize); /** * 准备上传 * @param fileSize */ void initUpload(int fileSize); } private OnUploadProcessListener onUploadProcessListener; public void setOnUploadProcessListener( OnUploadProcessListener onUploadProcessListener) { this.onUploadProcessListener = onUploadProcessListener; } public int getReadTimeOut() { return readTimeOut; } public void setReadTimeOut(int readTimeOut) { this.readTimeOut = readTimeOut; } public int getConnectTimeout() { return connectTimeout; } public void setConnectTimeout(int connectTimeout) { this.connectTimeout = connectTimeout; } /** * 获取上传使用的时间 * @return */ public static int getRequestTime() { return requestTime; } public static interface uploadProcessListener{ }

}

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fg.activity" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <!-- 权限赋予 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.READ_LOGS" /> <application android:icon="@drawable/logo" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SelectPicActivity" android:theme="@style/DialogStyleBottom"/> </application> </manifest>

三、服务器源码
FileUpload.java

package com.zhangke.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.PrintWriter; import java.text.DecimalFormat; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /*** * 文件上传例子 resource code encoding is utf-8 * <br>主要为了android客户端实现功能 代码写的乱 请大家见谅 * @author ICQwlj<br> * Email :wlj250237@126.com<br> * */ public class FileUpload extends ActionSupport { private String savePath; /**这里的名字和html的名字必须对称*/ private File img; /**要上传的文件类型*/ private String imgContentType; /**文件的名称*/ private String imgFileName; private String orderId;

public String getSavePath() { return ServletActionContext.getServletContext().getRealPath(savePath); } public File getImg() { return img; } public String getImgFileName() { return imgFileName; } public void setSavePath(String value) { this.savePath = value; } public void setImgFileName(String imgFileName) { this.imgFileName = imgFileName; } public void setImg(File img) { this.img = img; } public String getImgContentType() { return imgContentType; } public void setImgContentType(String imgContentType) { this.imgContentType = imgContentType; } public String getOrderId() { return orderId; } public void setOrderId(String orderId) { this.orderId = orderId; }

/** * 指定的上传类型zip和图片格式的文件 */ private static final String[] types = { "application/x-zip-compressed", "ZIP", "image/pjpeg","image/x-png","image/jpeg","image/png" }; //"application/octet-stream; charset=utf-8", /*** * 判断文件的类型是否为指定的文件类型 * @return */ public boolean filterType() { boolean isFileType = false; String fileType = getImgContentType(); System.out.println(fileType); for (String type : types) { if (type.equals(fileType)) { isFileType = true; break; } } return isFileType; }

/** * 取得文件夹大小 * * @param f * @return * @throws Exception */ public long getFileSize(File f) throws Exception { return f.length(); } public String FormetFileSize(long fileS) {// 转换文件大小 DecimalFormat df = new DecimalFormat("#.00"); String fileSizeString = ""; if (fileS < 1024) { fileSizeString = df.format((double) fileS) + "B"; } else if (fileS < 1048576) { fileSizeString = df.format((double) fileS / 1024) + "K"; } else if (fileS < 1073741824) { fileSizeString = df.format((double) fileS / 1048576) + "M"; } else { fileSizeString = df.format((double) fileS / 1073741824) + "G"; } return fileSizeString; } /** * 上传文件操作 * * @return * @throws Exception */ public String upload() throws Exception { String ct = ServletActionContext.getRequest().getHeader("Content-Type"); System.out.println("Content-Type="+ct); String result = "unknow error"; System.out.println("orderId="+getOrderId()); PrintWriter out = ServletActionContext.getResponse().getWriter(); if (!filterType()) { System.out.println("文件类型不正确"); ServletActionContext.getRequest().setAttribute("typeError", "您要上传的文件类型不正确"); result = "error:" + getImgContentType() + " type not upload file type"; } else { System.out.println("当前文件大小为:" + FormetFileSize(getFileSize(getImg()))); FileOutputStream fos = null; FileInputStream fis = null; try { // 保存文件那一个路径 fos = new FileOutputStream(getSavePath() + "\\" + getImgFileName()); fis = new FileInputStream(getImg()); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } result = "upload file success !"; } catch (Exception e) { result = "upload file failed ! "; e.printStackTrace(); } finally { fos.close(); fis.close(); } } out.print(result); return null; } }

struts.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.custom.i18n.resources" value="globalMessages"/> <constant name="struts.i18n.encoding" value="utf-8"/> <!-- 服务器指定一个临时的路径,这里使用了绝对路径,大家根据自己的Tomcat路径自行设置,否则会上传失败 --> <constant name="struts.multipart.saveDir" value="D:\apache-tomcat-6.0.35/temp"/> <constant name="struts.multipart.maxSize" value="30000000"></constant> <package name="lee" extends="struts-default"> <action name="file_*" class="com.zhangke.action.FileUpload" method="{1}"> <interceptor-ref name="fileUpload"> <!--<param name="allowedTypes">application/zip,application/octet-stream</param>--> <param name="maximumSize">524288000</param> </interceptor-ref> <interceptor-ref name="defaultStack"/> <!-- 保存路径为当前项目WebRoot/upload --> <param name="savePath">/upload</param> <result name="success">index.jsp</result> <result name="input">index.jsp</result> </action> </package> </struts>

四、效果图
Android学习之客户端上传图片到服务器 - ICQwlj - ICQwlj Android学习之客户端上传图片到服务器 - ICQwlj - ICQwlj
  五、完整的代码: http://download.csdn.net/detail/wlj142/7594879。其中,代码参考借鉴了蛮多人,在此就不一一例举了。
PS:如果是在本地服务器上调试代码,个人有以下几点建议:
1、最好拿真机来跑Android程序,因为有些时候,模拟器上报错是因为模拟器的某些硬件不支持,毕竟最后要拿到真机上运行。而且,用真机运行程序还有一个好处,就是运行速度快,加载模拟器的时间实在是不敢恭维;
2、如果自身的服务器没有对外IP,则需要通过将本地服务器与真机同时连接到一个无线路由里面,由路由器统一分配IP,然后再在客户端里面的访问HTTP的URL中,将IP改为服务器所获得到的IP地址即可。当然,也可以使用10.0.2.2来访问本地服务器,不过得通过USB线连接真机。
以上是个人看法,仅供参考。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页