Android本地图片上传(拍照+相册)
包含内容: 源码,全套工具
作者QQ549710689
运行效果图:
Android端代码
MainActivity:
public class MainActivity extends Activity implements OnClickListener, OnUploadProcessListener { private static final String TAG = "uploadImage"; private File file = null; /** * 去上传文件 */ 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; /** * 上传完毕 */ private static final int UPLOAD_END_PROCESS = 6; /*** * 这里的这个URL是我服务器的javaEE环境URL */ private static String requestURL = WelcomeActivity.ServerUrl + "ajax/admin/ProPicUpload.ashx"; private Button selectButton, uploadButton; private ImageView imageView; private TextView uploadImageResult; private ProgressBar progressBar; 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); progressBar = (ProgressBar) findViewById(R.id.progressBar1); } @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) { uploadImageResult.setText("正在上传中..."); //progressDialog.setMessage("正在上传文件..."); //progressDialog.show(); 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) // TO_SELECT_PHOTO) { picPath = data.getStringExtra(SelectPicActivity.KEY_PHOTO_PATH); Log.i(TAG, "最终选择的图片1=" + picPath); Bitmap bm = BitmapFactory.decodeFile(picPath); imageView.setImageBitmap(bm); // ContentResolver resolver = getContentResolver(); // Bitmap bm = MediaStore.Images.Media.getBitmap(resolver, // data.getData()); } 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 class UpdateImageThread extends Thread { @Override public void run() { toUploadFile(); } }; private void toUploadFile() { if (picPath != null) { String fileKey = "Filedata"; UploadUtil uploadUtil = UploadUtil.getInstance(); uploadUtil.setOnUploadProcessListener(this); // 设置监听器监听上传状态 Map<String, String> params = new HashMap<String, String>(); params.put("type", "android"); //uploadUtil.uploadFile(picPath, fileKey, requestURL, params); //uploadUtil.uploadFile(file, fileKey, requestURL, params); try { @SuppressWarnings("static-access") String s = uploadUtil.uploadSubmit(requestURL, params, new File(picPath), fileKey); Log.e("s=", s); //progressDialog.hide(); Message msg = Message.obtain(); msg.what = UPLOAD_END_PROCESS; msg.obj=s; handler.sendMessage(msg); //UPLOAD_END_PROCESS } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); //uploadImageResult.setText("上传失败"); } } } @SuppressLint("HandlerLeak") private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case TO_UPLOAD_FILE: // toUploadFile(); new UpdateImageThread().start(); break; case UPLOAD_INIT_PROCESS: progressBar.setMax(msg.arg1); break; case UPLOAD_IN_PROCESS: Log.e("setProgress", msg.arg1 + "%"); progressBar.setProgress(msg.arg1); uploadImageResult.setText(msg.arg1 + "%"); break; case UPLOAD_FILE_DONE: String result = "响应码:" + msg.arg1 + "\n响应信息:" + msg.obj + "\n耗时:" + UploadUtil.getRequestTime() + "秒"; uploadImageResult.setText(result); break; case UPLOAD_END_PROCESS: uploadImageResult.setText("上传成功"); String imageUrl = msg.obj.toString(); Intent lastIntent = getIntent(); lastIntent.putExtra("url", imageUrl); setResult(Activity.RESULT_OK, lastIntent); finish(); break; default: break; } super.handleMessage(msg); } }; @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); } }
/** * * 上传工具类 * * @author spring sky<br> * Email :vipa1888@163.com<br> * QQ: 840950105<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); // conn.setRequestProperty("Content-Type", // "application/x-www-form-urlencoded"); /** * 当文件不为空,把文件包装并且上传 */ 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()); // dos.flush(); } } 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(); // // dos.write(tempOutputStream.toByteArray()); /** * 获取响应码 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 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 static 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 { } private static long totalSize = 0; /** * 提交参数里有文件的数据 * * @param url * 服务器地址 * @param param * 参数 * @return 服务器返回结果 * @throws Exception */ public String uploadSubmit(String url, Map<String, String> param, File file, String fileKey) throws Exception { HttpPost post = new HttpPost(url); // MultipartEntity entity = new MultipartEntity(); CustomMultipartEntity entity = new CustomMultipartEntity( new ProgressListener() { @Override public void transferred(long num) { onUploadProcessListener .onUploadProcess((int) ((num / (float)totalSize) * 100)); } }); if (param != null && !param.isEmpty()) { for (Map.Entry<String, String> entry : param.entrySet()) { if (entry.getValue() != null && entry.getValue().trim().length() > 0) { entity.addPart(entry.getKey(), new StringBody(entry.getValue())); } } } // 添加文件参数 if (file != null && file.exists()) { entity.addPart(fileKey, new FileBody(file)); } totalSize = entity.getContentLength(); post.setEntity(entity); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(post); int stateCode = response.getStatusLine().getStatusCode(); StringBuffer sb = new StringBuffer(); if (stateCode == HttpStatus.SC_OK) { HttpEntity result = response.getEntity(); if (result != null) { InputStream is = result.getContent(); BufferedReader br = new BufferedReader( new InputStreamReader(is)); String tempLine; while ((tempLine = br.readLine()) != null) { sb.append(tempLine); } } } post.abort(); return sb.toString(); } }
C#代码:
public class ProPicUpload : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Charset = "utf-8"; HttpPostedFile file = context.Request.Files["Filedata"]; if (context.Request["type"] == "android") { if (file != null) { //上传图片方法就不贴了 //通过file就可以保存到服务器 context.Response.Write("/upload/" "," "文件名.jpg"); } } } public bool IsReusable { get { return false; } } }