上传头像等图片比较简单的一种就是直接把图片--- >String 再作为参数post给服务端

之所以用post是因为string都会很长~

下面见代码

try {ByteArrayOutputStream stream = new ByteArrayOutputStream();photo.compress(Bitmap.CompressFormat.JPEG, 60, stream);byte[] b = stream.toByteArray();// 将图片流以字符串形式存储下来String tp = new String(Base64Coder.encodeLines(b));//tp 就是最终的参数} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}

为了检验本地可以尝试把string - 〉图片

                                byte[] tempb = Base64Coder.decode(tp);Bitmap bitmap = BitmapFactory.decodeByteArray(tempb, 0, tempb.length);ImageVIew.setImageBitmap(bitmap);

另外这个方法用到两个java类在下面


import java.io.IOException;import android.graphics.Bitmap;
import android.graphics.BitmapFactory;public class Base64 {/** prevents anyone from instantiating this class */private Base64() {}/*** This character array provides the alphabet map from RFC1521.*/private final static char ALPHABET[] = {// 0 1 2 3 4 5 6 7'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', // 0'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', // 1'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', // 2'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', // 3'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', // 4'o', 'p', 'q', 'r', 's', 't', 'u', 'v', // 5'w', 'x', 'y', 'z', '0', '1', '2', '3', // 6'4', '5', '6', '7', '8', '9', '+', '/' // 7};/*** Decodes a 7 bit Base64 character into its binary value.*/private static int valueDecoding[] = new int[128];/*** initializes the value decoding array from the character map*/static {for (int i = 0; i < valueDecoding.length; i++) {valueDecoding[i] = -1;}for (int i = 0; i < ALPHABET.length; i++) {valueDecoding[ALPHABET[i]] = i;}}/*** Converts a byte array into a Base64 encoded string.* * @param data*            bytes to encode* @param offset*            which byte to start at* @param length*            how many bytes to encode; padding will be added if needed* @return base64 encoding of data; 4 chars for every 3 bytes*/public static String encode(byte[] data, int offset, int length) {int i;int encodedLen;char[] encoded;// 4 chars for 3 bytes, run input up to a multiple of 3encodedLen = (length + 2) / 3 * 4;encoded = new char[encodedLen];for (i = 0, encodedLen = 0; encodedLen < encoded.length; i += 3, encodedLen += 4) {encodeQuantum(data, offset + i, length - i, encoded, encodedLen);}return new String(encoded);}/*** Encodes 1, 2, or 3 bytes of data as 4 Base64 chars.* * @param in*            buffer of bytes to encode* @param inOffset*            where the first byte to encode is* @param len*            how many bytes to encode* @param out*            buffer to put the output in* @param outOffset*            where in the output buffer to put the chars*/private static void encodeQuantum(byte in[], int inOffset, int len,char out[], int outOffset) {byte a = 0, b = 0, c = 0;a = in[inOffset];out[outOffset] = ALPHABET[(a >>> 2) & 0x3F];if (len > 2) {b = in[inOffset + 1];c = in[inOffset + 2];out[outOffset + 1] = ALPHABET[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];out[outOffset + 2] = ALPHABET[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];out[outOffset + 3] = ALPHABET[c & 0x3F];} else if (len > 1) {b = in[inOffset + 1];out[outOffset + 1] = ALPHABET[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];out[outOffset + 2] = ALPHABET[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];out[outOffset + 3] = '=';} else {out[outOffset + 1] = ALPHABET[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];out[outOffset + 2] = '=';out[outOffset + 3] = '=';}}/*** Converts a Base64 encoded string to a byte array.* * @param encoded*            Base64 encoded data* @return decode binary data; 3 bytes for every 4 chars - minus padding* @exception IOException*                is thrown, if an I/O error occurs reading the data*/public static byte[] decode(String encoded) throws IOException {return decode(encoded, 0, encoded.length());}/*** Converts an embedded Base64 encoded string to a byte array.* * @param encoded*            a String with Base64 data embedded in it* @param offset*            which char of the String to start at* @param length*            how many chars to decode; must be a multiple of 4* @return decode binary data; 3 bytes for every 4 chars - minus padding* @exception IOException*                is thrown, if an I/O error occurs reading the data*/public static byte[] decode(String encoded, int offset, int length)throws IOException {int i;int decodedLen;byte[] decoded;// the input must be a multiple of 4if (length % 4 != 0) {throw new IOException("Base64 string length is not multiple of 4");}// 4 chars for 3 bytes, but there may have been pad bytesdecodedLen = length / 4 * 3;if (encoded.charAt(offset + length - 1) == '=') {decodedLen--;if (encoded.charAt(offset + length - 2) == '=') {decodedLen--;}}decoded = new byte[decodedLen];for (i = 0, decodedLen = 0; i < length; i += 4, decodedLen += 3) {decodeQuantum(encoded.charAt(offset + i),encoded.charAt(offset + i + 1),encoded.charAt(offset + i + 2),encoded.charAt(offset + i + 3), decoded, decodedLen);}return decoded;}/*** Decode 4 Base64 chars as 1, 2, or 3 bytes of data.* * @param in1*            first char of quantum to decode* @param in2*            second char of quantum to decode* @param in3*            third char of quantum to decode* @param in4*            forth char of quantum to decode* @param out*            buffer to put the output in* @param outOffset*            where in the output buffer to put the bytes*/private static void decodeQuantum(char in1, char in2, char in3, char in4,byte[] out, int outOffset) throws IOException {int a = 0, b = 0, c = 0, d = 0;int pad = 0;a = valueDecoding[in1 & 127];b = valueDecoding[in2 & 127];if (in4 == '=') {pad++;if (in3 == '=') {pad++;} else {c = valueDecoding[in3 & 127];}} else {c = valueDecoding[in3 & 127];d = valueDecoding[in4 & 127];}if (a < 0 || b < 0 || c < 0 || d < 0) {throw new IOException("Invalid character in Base64 string");}// the first byte is the 6 bits of a and 2 bits of bout[outOffset] = (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3));if (pad < 2) {// the second byte is 4 bits of b and 4 bits of cout[outOffset + 1] = (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf));if (pad < 1) {// the third byte is 2 bits of c and 4 bits of dout[outOffset + 2] = (byte) (((c << 6) & 0xc0) | (d & 0x3f));}}}/*** 将字符串转换成Bitmap类型* * @param string* @return*/public static Bitmap stringtoBitmap(String string) {Bitmap bitmap = null;try {byte[] bitmapArray;bitmapArray = decode(string);bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,bitmapArray.length);} catch (Exception e) {e.printStackTrace();}return bitmap;}}
public class Base64Coder {// The line separator string of the operating system.private static final String systemLineSeparator = System.getProperty("line.separator");// Mapping table from 6-bit nibbles to Base64 characters.private static char[] map1 = new char[64];static {int i = 0;for (char c = 'A'; c <= 'Z'; c++)map1[i++] = c;for (char c = 'a'; c <= 'z'; c++)map1[i++] = c;for (char c = '0'; c <= '9'; c++)map1[i++] = c;map1[i++] = '+';map1[i++] = '/';}// Mapping table from Base64 characters to 6-bit nibbles.private static byte[] map2 = new byte[128];static {for (int i = 0; i < map2.length; i++)map2[i] = -1;for (int i = 0; i < 64; i++)map2[map1[i]] = (byte) i;}/*** Encodes a string into Base64 format. No blanks or line breaks are* inserted.* * @param s*            A String to be encoded.* @return A String containing the Base64 encoded data.*/public static String encodeString(String s) {return new String(encode(s.getBytes()));}/*** Encodes a byte array into Base 64 format and breaks the output into lines* of 76 characters. This method is compatible with* <code>sun.misc.BASE64Encoder.encodeBuffer(byte[])</code>.* * @param in*            An array containing the data bytes to be encoded.* @return A String containing the Base64 encoded data, broken into lines.*/public static String encodeLines(byte[] in) {return encodeLines(in, 0, in.length, 76, systemLineSeparator);}/*** Encodes a byte array into Base 64 format and breaks the output into* lines.* * @param in*            An array containing the data bytes to be encoded.* @param iOff*            Offset of the first byte in <code>in</code> to be processed.* @param iLen*            Number of bytes to be processed in <code>in</code>, starting*            at <code>iOff</code>.* @param lineLen*            Line length for the output data. Should be a multiple of 4.* @param lineSeparator*            The line separator to be used to separate the output lines.* @return A String containing the Base64 encoded data, broken into lines.*/public static String encodeLines(byte[] in, int iOff, int iLen,int lineLen, String lineSeparator) {int blockLen = (lineLen * 3) / 4;if (blockLen <= 0)throw new IllegalArgumentException();int lines = (iLen + blockLen - 1) / blockLen;int bufLen = ((iLen + 2) / 3) * 4 + lines * lineSeparator.length();StringBuilder buf = new StringBuilder(bufLen);int ip = 0;while (ip < iLen) {int l = Math.min(iLen - ip, blockLen);buf.append(encode(in, iOff + ip, l));buf.append(lineSeparator);ip += l;}return buf.toString();}/*** Encodes a byte array into Base64 format. No blanks or line breaks are* inserted in the output.* * @param in*            An array containing the data bytes to be encoded.* @return A character array containing the Base64 encoded data.*/public static char[] encode(byte[] in) {return encode(in, 0, in.length);}/*** Encodes a byte array into Base64 format. No blanks or line breaks are* inserted in the output.* * @param in*            An array containing the data bytes to be encoded.* @param iLen*            Number of bytes to process in <code>in</code>.* @return A character array containing the Base64 encoded data.*/public static char[] encode(byte[] in, int iLen) {return encode(in, 0, iLen);}/*** Encodes a byte array into Base64 format. No blanks or line breaks are* inserted in the output.* * @param in*            An array containing the data bytes to be encoded.* @param iOff*            Offset of the first byte in <code>in</code> to be processed.* @param iLen*            Number of bytes to process in <code>in</code>, starting at*            <code>iOff</code>.* @return A character array containing the Base64 encoded data.*/public static char[] encode(byte[] in, int iOff, int iLen) {int oDataLen = (iLen * 4 + 2) / 3; // output length without paddingint oLen = ((iLen + 2) / 3) * 4; // output length including paddingchar[] out = new char[oLen];int ip = iOff;int iEnd = iOff + iLen;int op = 0;while (ip < iEnd) {int i0 = in[ip++] & 0xff;int i1 = ip < iEnd ? in[ip++] & 0xff : 0;int i2 = ip < iEnd ? in[ip++] & 0xff : 0;int o0 = i0 >>> 2;int o1 = ((i0 & 3) << 4) | (i1 >>> 4);int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);int o3 = i2 & 0x3F;out[op++] = map1[o0];out[op++] = map1[o1];out[op] = op < oDataLen ? map1[o2] : '=';op++;out[op] = op < oDataLen ? map1[o3] : '=';op++;}return out;}/*** Decodes a string from Base64 format. No blanks or line breaks are allowed* within the Base64 encoded input data.* * @param s*            A Base64 String to be decoded.* @return A String containing the decoded data.* @throws IllegalArgumentException*             If the input is not valid Base64 encoded data.*/public static String decodeString(String s) {return new String(decode(s));}/*** Decodes a byte array from Base64 format and ignores line separators, tabs* and blanks. CR, LF, Tab and Space characters are ignored in the input* data. This method is compatible with* <code>sun.misc.BASE64Decoder.decodeBuffer(String)</code>.* * @param s*            A Base64 String to be decoded.* @return An array containing the decoded data bytes.* @throws IllegalArgumentException*             If the input is not valid Base64 encoded data.*/public static byte[] decodeLines(String s) {char[] buf = new char[s.length() + 3];int p = 0;for (int ip = 0; ip < s.length(); ip++) {char c = s.charAt(ip);if (c != ' ' && c != '\r' && c != '\n' && c != '\t')buf[p++] = c;}while ((p % 4) != 0)buf[p++] = '0';return decode(buf, 0, p);}/*** Decodes a byte array from Base64 format. No blanks or line breaks are* allowed within the Base64 encoded input data.* * @param s*            A Base64 String to be decoded.* @return An array containing the decoded data bytes.* @throws IllegalArgumentException*             If the input is not valid Base64 encoded data.*/public static byte[] decode(String s) {return decode(s.toCharArray());}/*** Decodes a byte array from Base64 format. No blanks or line breaks are* allowed within the Base64 encoded input data.* * @param in*            A character array containing the Base64 encoded data.* @return An array containing the decoded data bytes.* @throws IllegalArgumentException*             If the input is not valid Base64 encoded data.*/public static byte[] decode(char[] in) {return decode(in, 0, in.length);}/*** Decodes a byte array from Base64 format. No blanks or line breaks are* allowed within the Base64 encoded input data.* * @param in*            A character array containing the Base64 encoded data.* @param iOff*            Offset of the first character in <code>in</code> to be*            processed.* @param iLen*            Number of characters to process in <code>in</code>, starting*            at <code>iOff</code>.* @return An array containing the decoded data bytes.* @throws IllegalArgumentException*             If the input is not valid Base64 encoded data.*/public static byte[] decode(char[] in, int iOff, int iLen) {if (iLen % 4 != 0)throw new IllegalArgumentException("Length of Base64 encoded input string is not a multiple of 4.");while (iLen > 0 && in[iOff + iLen - 1] == '=')iLen--;int oLen = (iLen * 3) / 4;byte[] out = new byte[oLen];int ip = iOff;int iEnd = iOff + iLen;int op = 0;while (ip < iEnd) {int i0 = in[ip++];int i1 = in[ip++];int i2 = ip < iEnd ? in[ip++] : 'A';int i3 = ip < iEnd ? in[ip++] : 'A';if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)throw new IllegalArgumentException("Illegal character in Base64 encoded data.");int b0 = map2[i0];int b1 = map2[i1];int b2 = map2[i2];int b3 = map2[i3];if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)throw new IllegalArgumentException("Illegal character in Base64 encoded data.");int o0 = (b0 << 2) | (b1 >>> 4);int o1 = ((b1 & 0xf) << 4) | (b2 >>> 2);int o2 = ((b2 & 3) << 6) | b3;out[op++] = (byte) o0;if (op < oLen)out[op++] = (byte) o1;if (op < oLen)out[op++] = (byte) o2;}return out;}// Dummy constructor.private Base64Coder() {}}

图片转为String传个给服务端相关推荐

  1. android 快传 源码_安卓APP仿茄子快传源码,Android项目源码类似茄子快传的快传项目包括服务端...

    适用范围:安卓APP仿茄子快传源码,Android项目源码类似茄子快传的快传项目包括服务端 演示地址:(以截图为准) 运行环境:Android+PC+web 其他说明: 本项目是一个基于安卓的类似茄子 ...

  2. Java之~hutool批量压缩多个图片文件上传到云服务(InputStream )

    用的hutool工具类 import cn.hutool.core.util.ZipUtil; 批量多张图片进行压缩.我这边是将上传到华为云的图片进行批量打包压缩. //图片批量压缩     @Tes ...

  3. 【2017-05-30】WebForm文件上传。从服务端删除文件

    用 FileUpload控件进行上传文件. <asp:FileUpload ID="FileUpload1"  runat="server" /> ...

  4. Winform中实现FTP客户端并定时扫描指定路径下文件上传到FTP服务端然后删除文件

    场景 Windows10上怎样开启FTP服务: Windows10上怎样开启FTP服务_BADAO_LIUMANG_QIZHI的博客-CSDN博客 上面在Windows上搭建FTP服务器之后,会接收客 ...

  5. android显示服务器端文件夹,Android上传文件到服务端并显示进度条

    最近在做上传文件的服务,简单看了网上的教程.结合实践共享出代码. 由于网上的大多数没有服务端的代码,这可不行呀,没服务端怎么调试呢. Ok,先上代码. Android 上传比较简单,主要用到的是 Ht ...

  6. 在浏览器进行大文件分片上传(java服务端实现)

    微信搜索:"二十同学" 公众号,欢迎关注一条不一样的成长之路 最近在做web网盘的系统,网盘最基本的功能便是文件上传,但是文件上传当遇到大文件的时候,在web端按传统方式上传简直是 ...

  7. android 快传 源码_app源码之-仿茄子快传源码,Android项目源码类似茄子快传的快传项目包括服务端...

    商品属性 品牌其他 语言PHP 数据库 移动端无 大小48 MB 规格无 授权无 源文件无 安装环境 安装服务 主机类型 伪静态 操作系统 安装方式 web服务 商品介绍 本项目是一个基于安卓的 ...

  8. java 将本地图片批量上传到oss服务

    上段时间在后台搞了一个图片上传的功能,但是由于项目上传的图片量过大,如果用后台上传效率较慢,所以搞了一个程序直接从把本地的图片上传的oss. import com.alibaba.fastjson.J ...

  9. Android multipart 上传文件到服务端

    安卓端代码: public static void sendFile(String filePath){//要发送的文件File file = new File(filePath);OkHttpCli ...

最新文章

  1. 不需xp_cmdshell支持在有注入漏洞的SQL服务器上运行CMD命令
  2. 新手科普 | 探索机器学习模型,保障账户安全
  3. Dynamics 365 for CRM: Sitemap站点图的可视化编辑功能
  4. USACO shuttle
  5. 隐藏响应的server,X-Powered-By
  6. 李开复唱衰互联网手机:大部分公司会失败
  7. java按钮随机移动_java – 使按钮移动触摸我们触摸的确切位置
  8. 4918字,详解商品系统的存储架构设计
  9. 《软件工程实践》第三次作业-原型设计(结对第一次)
  10. Linux下DNS服务管理
  11. docker gpu 创建 训练环境_巧用 Docker 快速部署 GPU 环境
  12. c# asp.net页面传值方法总结
  13. RBG-D深度相机的相关资料
  14. Axure RP 9.0 软件安装教程
  15. MediaCreationTool2004 U盘安装系统
  16. PowerBuilder 2018
  17. linux+开机启动sshd_Linux sshd服务自动启动
  18. 网络工程师笔记--广域网和接入网
  19. ODN中主干光交和配线光交的数量比例
  20. What Makes a Great Maintainer of Open Source Projects?

热门文章

  1. 单反拍摄技巧:简单构图
  2. 明日方舟找回密码服务器出错,明日方舟登录常见问题汇总及解决方案介绍
  3. GNN在下拉推荐的应用
  4. CUDA C 矩阵乘优化
  5. 怎么关闭苹果手机自动扣费_iPhone 关闭支付宝自动扣费服务提示“无法解约”怎么办?...
  6. 游戏设计与开发_独立游戏开发:怎样设计游戏新手教学?
  7. linux中vlc命令,在Ubuntu 14.10上安装VLC播放器
  8. 简简单单教你制作 闪光字
  9. 二、MySQL——多表查询内容
  10. 去国外出差的经验总结