android上传图片到服务器(使用base64字节流的形式通过 AsyncHttpClient框架传输)

组件:http://loopj.com/Android-async-http

public static void reg(final Context cont,Bitmap photodata,String regData) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			
			//将bitmap一字节流输出 Bitmap.CompressFormat.PNG 压缩格式,100:压缩率,baos:字节流
			photodata.compress(Bitmap.CompressFormat.PNG, 100, baos);
			baos.close();
			byte[] buffer = baos.toByteArray();
			System.out.println("图片的大小:"+buffer.length);
			
			//将图片的字节流数据加密成base64字符输出
			String photo = Base64.encodeToString(buffer, 0, buffer.length,Base64.DEFAULT);

			//photo=URLEncoder.encode(photo,"UTF-8");
			RequestParams params = new RequestParams();
           	        params.put("photo", photo);
                        params.put("name", "woshishishi");//传输的字符数据
                        String url = "http://10.0.2.2:8080/IC_Server/servlet/RegisterServlet1";
 
            
                        AsyncHttpClient client = new AsyncHttpClient();
                        client.post(url, params, new AsyncHttpResponseHandler() {
            	    	@Override  
                	public void onSuccess(int statusCode, String content){  
            		Toast.makeText(cont, "头像上传成功!"+content, 0)
                   	 .show(); 
               			 }  
                	@Override  
                	public void onFailure(Throwable e, String data){  
                	Toast.makeText(cont, "头像上传失败!", 0)
                    	.show(); 
                }
            });
 
        } catch (Exception e) {
            e.printStackTrace();
        }

	}

服务器中 servelet中的代码:

package uestc.app.ic.server.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.misc.BASE64Decoder;

public class RegisterServlet1 extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		String photo = request.getParameter("photo");
		String name = request.getParameter("name");

		try {

			// 对base64数据进行解码 生成 字节数组,不能直接用Base64.decode();进行解密
			byte[] photoimg = new BASE64Decoder().decodeBuffer(photo);
			for (int i = 0; i < photoimg.length; ++i) {
				if (photoimg[i] < 0) {
					// 调整异常数据
					photoimg[i] += 256;
				}
			}

			// byte[] photoimg = Base64.decode(photo);//此处不能用Base64.decode()方法解密,我调试时用此方法每次解密出的数据都比原数据大  所以用上面的函数进行解密,在网上直接拷贝的,花了好几个小时才找到这个错误(菜鸟不容易啊)
			System.out.println("图片的大小:" + photoimg.length);
			File file = new File("e:", "decode.png");
			File filename = new File("e:\\name.txt");
			if (!filename.exists()) {
				file.createNewFile();
			}
			if (!file.exists()) {
				file.createNewFile();
			}
			FileOutputStream out = new FileOutputStream(file);
			FileOutputStream out1 = new FileOutputStream(filename);
			out1.write(name.getBytes());
			out.write(photoimg);
			out.flush();
			out.close();
			out1.flush();
			out1.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值