前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Base64实现android端图片上传到server端

Base64实现android端图片上传到server端

作者头像
全栈程序员站长
发布2022-07-07 16:53:21
6510
发布2022-07-07 16:53:21
举报

大家好,又见面了,我是全栈君。

首先要下载Base64.java文件http://iharder.sourceforge.net/current/java/base64/

将代码复制到project中。

然后上代码:

android端代码:

private void setPicToView(Intent picdata) { Bundle extras = picdata.getExtras(); if (extras != null) { mBitmap = extras.getParcelable(“data”); view_images.setImageBitmap(mBitmap); LogUtil.i(“运行reg”, “运行了吗?”); ByteArrayOutputStream baos = new ByteArrayOutputStream(); //将bitmap一字节流输出 Bitmap.CompressFormat.PNG 压缩格式,100:压缩率。baos:字节流 mBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); try { baos.close(); } catch (IOException e) { e.printStackTrace(); } byte[] buffer = baos.toByteArray(); LogUtil.i(“图片大小”, buffer.length+””); //将图片的字节流数据加密成base64字符输出 photo = Base64.encodeBytes(buffer); } }

server端代码:

public static void SaveImages(String photo,String filePath){ String imageName = new IPTimeStamp().getIPTimestamp()+”.png”; try { //对base64数据进行解码 生成字节数组。 byte[] photoimg = new BASE64Decoder().decodeBuffer(photo); for(int i=0;i<photoimg.length;i++){ if(photoimg[i]<0){ //调整异常数据 photoimg[i] += 256; } } // SysUtil.SysOut(“图片的大小:” + photoimg.length); File file = new File(filePath,imageName); //创建一个目录 往里面写入图片 if (!file.exists()) { file.createNewFile(); //file.mkdirs()创建一个目录,file.createNewFile()创建一个文件 } FileOutputStream out = new FileOutputStream(file); out.write(photoimg); out.flush(); out.close(); } catch (Exception e) { // TODO: handle exception }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116574.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档