2014-12-23 121 views
0

在客戶端我使用Jquery將圖像從圖像轉換爲Base64字符串並將其發送回服務器。在服務器上,我將字符串保存爲原樣,並且當我將圖像很好地顯示出來時,它也在所需的頁面上呈現。但是,當我在下面使用這個類時,我無法將圖像存儲在服務器上的磁盤上,或者使用ImageIO.read或使用輸出流,它不會在病房後顯示。從Base64字符串保存圖像字符串不會打開

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字符串。我也使用ImageReader使用ImageIO.read,但沒有去。似乎數據中出現了問題。

謝謝。

回答

0

你可以很容易地使用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,", ""); 
} 
+0

你沒有很好地閱讀後..我說我在服務器上有問題..上面的代碼是服務器端代碼..我只是想保存base64字符串作爲圖像...在服務器上...爲什麼標記我失望...我沒有任何關於客戶端的問題。 。! – Abhijeet