论坛首页 移动开发技术论坛

Android二进制文件转码到Base64并通过Post进行提交

浏览 11866 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-02-08  

类似于Ruby,在上面调试成功后,将功能迁移到了Android应用上面。

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.util.Base64;
import android.util.Log;

	/**
	 * TODO FIX Check the file length, it's long not int.This may lead the
	 * problem.
	 * 
	 * @param fileName
	 * @param url
	 * @return
	 */
	public static boolean httpPostBase64(String fileName, String url) {
		try {
			File file = new File(fileName);
			FileInputStream in = new FileInputStream(file);
			byte[] buffer = new byte[(int) file.length() + 100];
			int length = in.read(buffer);
			String data = Base64.encodeToString(buffer, 0, length,
					Base64.DEFAULT);

			HttpPost httpRequest = new HttpPost(url);
			/*
			 * NameValuePair实现请求参数的封装
			 */
			List<NameValuePair> params = new LinkedList<NameValuePair>();
			params.add(new BasicNameValuePair("data", data));

			/* 添加请求参数到请求对象 */
			httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			/* 发送请求并等待响应 */
			HttpResponse httpResponse = new DefaultHttpClient()
					.execute(httpRequest);
			/* 若状态码为200 ok */
			if (httpResponse.getStatusLine().getStatusCode() != 200) {
				Log.d("Code", httpResponse.getStatusLine().toString());
				return false;
			}

		} catch (Exception e) {
			for(StackTraceElement s:e.getStackTrace())
				Log.d("Exception",s.toString());
			Log.d("Exception",e.getLocalizedMessage());
			
			return false;
		}

		return true;
	}
 
   发表时间:2011-02-18  
   
  FileInputStream in = new FileInputStream(file);  
            byte[] buffer = new byte[(int) file.length() + 100];  
            int length = in.read(buffer);  
            String data = Base64.encodeToString(buffer, 0, length,  
            Base64.DEFAULT);  


你这个搞不定大文件,特别是在手机上,比如上M的。
基本思路是:大文件应该将文件base64编码到磁盘上,然后再通过http流来搞。
0 请登录后投票
   发表时间:2011-02-18  
毕竟红尘 写道
   
  FileInputStream in = new FileInputStream(file);  
            byte[] buffer = new byte[(int) file.length() + 100];  
            int length = in.read(buffer);  
            String data = Base64.encodeToString(buffer, 0, length,  
            Base64.DEFAULT);  


你这个搞不定大文件,特别是在手机上,比如上M的。
基本思路是:大文件应该将文件base64编码到磁盘上,然后再通过http流来搞。

有道理。
0 请登录后投票
   发表时间:2011-02-18  
...完全看不出有什么作用 上传就上传 还非要变成String 提交..
0 请登录后投票
   发表时间:2011-02-19  
aa87963014 写道
...完全看不出有什么作用 上传就上传 还非要变成String 提交..

这个也算是情况特殊,我这边的数据保存后端是Google Form。
0 请登录后投票
   发表时间:2011-02-19  
毕竟红尘 写道
   
  FileInputStream in = new FileInputStream(file);  
            byte[] buffer = new byte[(int) file.length() + 100];  
            int length = in.read(buffer);  
            String data = Base64.encodeToString(buffer, 0, length,  
            Base64.DEFAULT);  


你这个搞不定大文件,特别是在手机上,比如上M的。
基本思路是:大文件应该将文件base64编码到磁盘上,然后再通过http流来搞。

非常感谢你的提醒,大文件确实没有考虑到。
0 请登录后投票
   发表时间:2011-02-22  
还有一点,就是向下不兼容,用google提供的base64类,在1.0和2.2中间的版本,都被去掉不支持的。
0 请登录后投票
   发表时间:2011-02-22  
LuoYer 写道
还有一点,就是向下不兼容,用google提供的base64类,在1.0和2.2中间的版本,都被去掉不支持的。

老兄绝对是个高手啊,这么丰富的经验。谢谢提醒了。
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics