zoukankan      html  css  js  c++  java
  • java上传不同类型图片,保存数据库(Base64位图转网络图片)

    直接上代码好不好

    方法名:

    GenerateImage
    传参:
    base64Img:上传的base64码
    realPath:生成的图片路径 
    imgTypes :图片类型

     
    String imgBase64 = request.getParameter("userImgbase64");
    // 获取当前服务器路径
     String realPath = request.getServletContext().getRealPath("")+"\img"; 
    System.out.println(
    realPath ); //  http://192.168.191.1:8080/MyWeb/img


    String[] imgType = imgBase64.split(",");

    // 获取图片类型(如果没有类型,貌似会报错)
    String imgTypes = imgType[0].toString().substring(11, imgType[0].toString().length()-7);


    String userImgbase64 = GenerateImage(imgBase64,realPath,imgTypes);
    System.out.println(realPath );   //  http://192.168.191.1:8080/MyWeb/img/1532493648717.png

    //
    对字节数组字符串进行Base64解码并生成图片 public static String GenerateImage(String base64Img,String realPath,String imgTypes ){ String ret_fileName = null; // 临时文件路径 File file_normer = new File(realPath+"/"); if (!file_normer.exists()) { file_normer.mkdirs(); } if (base64Img == null) // 图像数据为空 return "图像数据为空"; base64Img = base64Img.replaceAll("data:image/"+imgTypes+";base64,", ""); BASE64Decoder decoder = new BASE64Decoder(); try { // Base64解码 byte[] b = decoder.decodeBuffer(base64Img); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) {// 调整异常数据 b[i] += 256; } } // 生成jpeg图片 ret_fileName = System.currentTimeMillis()+"."+imgTypes+""; File file = new File(realPath + "/" + ret_fileName ); OutputStream out = new FileOutputStream(file); out.write(b); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return ret_fileName ; }
  • 相关阅读:
    hdu 1548 升降梯
    hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题
    hdu 4463 有一条边必须加上 (2012杭州区域赛K题)
    poj 1679 判断MST是不是唯一的 (次小生成树)
    poj 1751 输出MST中新加入的边
    poj 2349 求MST中第S大的权值
    HDU 4389 X mod f(x) (数位DP)
    HDU 5908 Abelian Period (暴力)
    HDU 5907 Find Q (水题)
    HDU 4514 湫湫系列故事――设计风景线 (树形DP)
  • 原文地址:https://www.cnblogs.com/zhuyupingit/p/9454354.html
Copyright © 2011-2022 走看看