【问题标题】:Saving an Image from Base64 String Does not Open [duplicate]从 Base64 字符串保存图像不会打开 [重复]
【发布时间】:2015-02-21 11:56:54
【问题描述】:

在客户端,我使用 Jquery 将图像从图像转换为 Base64 字符串并将其发送回服务器。在服务器上,我按原样存储字符串,并且当我很好地显示图像时,它也会在所需的页面上呈现。但是,当我在下面使用此类时,我无法将图像存储在服务器上的磁盘上,也无法使用 ImageIO.read 或使用输出流,之后它不会显示出来。

public static void saveCompanyLogo(String path,String logo,String comId){
    String s[]=logo.split(";");
    String format=s[0].substring(11);
    File file = new File(path+"SecurePass\\logos\\");
    if (!file.exists()) {
        boolean result = false;

        try{
            File f=new File(path+"SecurePass\\logos\\");
            f.mkdir();
            result = true;
        } catch(SecurityException se){
            //handle it
        }        
        if(result) {    
        }
    }
    byte[] b=Base64.decodeBase64(logo.replaceAll(" ", "+"));
    try (OutputStream stream = new FileOutputStream(path+"SecurePass\\logos\\"+comId+"."+format)) {
        stream.write(b);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

我根据溢出用户的建议使用 replaceAll,但不行。请帮忙。在上面的代码中,“logo”包含 Base64 字符串。我也使用 ImageIO.read 使用 imagereaders 但不行。数据好像有问题。

谢谢。

【问题讨论】:

    标签: javascript jquery image base64 javax.imageio


    【解决方案1】:

    您可以使用canvas 轻松完成。像这样:

    var img =document.getElementbyId('image');
    
    function baseImage(img){ // img is the image object
    var canvas = document.createElement("canvas");
                    canvas.width = img.width;
                    canvas.height = img.height;
    
                    // Copy the image contents to the canvas
                    var ctx = canvas.getContext("2d");
                    ctx.drawImage(img, 0, 0, img.width, img.height);
    
                    // Get the data-URL formatted image
                    var dataURL = canvas.toDataURL("image/jpeg");
    
                    return dataURL.replace("data:image/jpeg;base64,", "");
    }
    

    【讨论】:

    • 你没有很好地阅读这篇文章..我是说我在服务器上有问题..上面的代码是服务器端代码..我只是想将 base64 字符串保存为图像... on服务器...为什么要标记我...我在客户端方面没有问题...!
    【解决方案2】:

    我们必须从客户端字符串中删除字符串..这篇文章的详细信息..

    Convert base64 string to image

    【讨论】:

      猜你喜欢
      • 2020-09-17
      • 2020-09-25
      • 2015-02-07
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多