前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >图片转二进制流并通过HTTP上传到静态文件服务器

图片转二进制流并通过HTTP上传到静态文件服务器

作者头像
麦克劳林
发布2019-08-09 11:21:40
2.6K0
发布2019-08-09 11:21:40
举报
1 图片转化成base64字符串
//图片转化成base64字符串    
public static String getImageBinary(String imgFile){
    BASE64Encoder encoder = new sun.misc.BASE64Encoder();
    File f = new File(imgFile);
    BufferedImage bi;
    try {
        bi = ImageIO.read(f);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi, "jpg", baos);
        byte[] bytes = baos.toByteArray();
        return encoder.encodeBuffer(bytes).trim();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "";
}

注意:在图片转成base64String中可能会包含\r\n,要将其去掉

 String imageBinary = base64String.replaceAll("\r\n", "").replaceAll("\r", "").replaceAll("\n", "");
2 base64字符串转化成图片
//base64字符串转化成图片
public static boolean GenerateImage(String imgStr, String imgFilePath, String imgFileName) {
    //对字节数组字符串进行Base64解码并生成图片
    //图像数据为空        
    if (imgStr == null) {
        return false;
    }
        
    if(imgFilePath == null || imgFileName == null) { // 存储路径为空
        return false;
    }
    File filePath = new File(imgFilePath);
    if(!filePath.exists()) {
        filePath.mkdirs();
    }
    imgFilePath = imgFilePath + imgFileName;
    BASE64Decoder decoder = new BASE64Decoder();
    try {
        //Base64解码
        byte[] b = decoder.decodeBuffer(imgStr);
        for(int i=0;i<b.length;++i) {
            if(b[i]<0) {//调整异常数据
                b[i]+=256;
            }
        }
        //生成jpeg图片
        //String imgFilePath = "d://222.jpg";//新生成的图片
        OutputStream out = new FileOutputStream(imgFilePath);
        out.write(b);
        out.flush();
        out.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

imgFileName后面记得加.jpg

3 post方法采用的okhttp3
/**
 * POST TO A SERVER
 * @param url
 * @param data
 * @return
 * @throws IOException
 */
public static String postRequest(String url,String data,MediaType type)throws IOException {
    OkHttpClient client = new OkHttpClient();
    RequestBody body = RequestBody.create(type, data);
    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
    Response response = client.newCall(request).execute();
    if(response.isSuccessful()){
        ResponseBody responseBody = response.body();
        if (responseBody != null) {
            // 返回的是string 类型
            String str = responseBody.string();
            logger.debug("getPostRequest response =>" + str);
            return str;
        }
    }
    return null;
}

type设置为MediaType.parse("application/json; charset=utf-8")

data是我封装的json字符串,把base64String键值进去。接口端直接解析data就行。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 图片转化成base64字符串
  • 2 base64字符串转化成图片
  • 3 post方法采用的okhttp3
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档