您的位置:首页 > 其它

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

2017-01-02 09:41 351 查看
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();  

        }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐