我用base64上传图片,为什么用线程传不到服务器

String url = Definition.PATH_SERVLET + "ImageUpServlet";
int count = 0;
final String tempPath =  count + "png";

ByteArrayOutputStream baso = new ByteArrayOutputStream();
bitmaps[0].compress(Bitmap.CompressFormat.JPEG, 100, baso);
byte aa[] = baso.toByteArray(); 
//Log.i("yy", String.valueOf(aa.length));
//DialogHelper.showAlertDialog(PostActivity.this, "图片大小为"+aa.length/1024 +"KB");
final String photo = Base64.encodeToString(aa,0, aa.length,Base64.DEFAULT);   
                                                //一张图片大概40KB
//Log.i("yy", String.valueOf(photo.length()));
Map<String, String> params = new HashMap<String, String>();

params.put("photo",photo);
params.put("name", tempPath);

StringBuilder sb = new StringBuilder(url);
sb.append('?');
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), "UTF-8"))
.append('&');
}            //这是拼接地址  http://192.168.23.4:8080/Neighbours/servlet/GetConInfoServlet?.....
sb.deleteCharAt(sb.length() - 1);
URL urls = new URL(sb.toString());      
HttpURLConnection conn = (HttpURLConnection) urls.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();   //此时code会等于400   服务器无响应
if (code == 200) {
System.out.println("sssssssssssss");
} else{
System.out.println("fffffffffffffffs");
}



服务端代码:
  protected 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.jpg");  
            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、付费专栏及课程。

余额充值