2013-11-27 1 views
0

내 응용 프로그램에 업로드 된 기능이 있습니다. 아파치 - 커먼즈 파일 업로드 및 스프링 멀티 파트를 사용했으며 프로젝트 컨텍스트가 아닌 디렉터리 폴더에 이미지를 저장했습니다.). 내가 붙어있는 문제는 이미지를 가져 와서 jsp에서 렌더링하는 것입니다. Buffered image와 ImageIO를 사용하여 해당 폴더에서 이미지를 읽으려고했지만 img 태그를 사용하여 JSP로 렌더링하는 방법을 알 수 없습니다. 도움이 될 것입니다.스프링 및 아파치를 사용하여 웹에서 이미지를 표시합니다.

//Code for reading 
BufferedImage im=ImageIO.read(new File(imagePath)); 
+0

http://stackoverflow.com/questions/5243726/how-to-display-an-image-in-jsp – Patison

답변

3

마지막으로 img 태그를 사용하여 웹 브라우저에 이미지를 표시 할 수 있습니다. 지금 제가 따르는 단계 : 1. BufferedImage를 사용하여 이미지를 읽었습니다. 2. ByteArrayOutputStream을 사용하여 bufferedImage를 바이트로 변환합니다. 아파치 커먼스 코덱 LIB를 사용 Base64로로 스트리밍

//Pseudo Code 
    BufferedImage bufferedImage=ImageIO.read(new File(imagePath)); 

    //imageDao contains the image name that i stored in the database 
    String []formatSplit=imageDao.split("\\."); 

    if(formatSplit.length==2){ 
    String format=formatSplit[1]; 

    //ImageUtility is class that contain code for converting bufferedimage to string 
    String traineeImage=ImageUtility.encodeToString(bufferedImage,format); 

    model.addAttribute("imagePath", traineeImage); 
    } 


    //ImageUtilty class -method 

    public static String encodeToString(BufferedImage image, String type) { 

     String imageString=null; 
     String encodedImage=null; 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

     try { 

      ImageIO.write(image, type, bos); 
      byte[] imageBytes = bos.toByteArray(); 


      encodedImage=org.apache.commons.codec.binary.Base64.encodeBase64String(imageBytes); 

      imageString = "data:image/"+type+";base64,"+encodedImage; 

      bos.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return imageString; 
    } 

그리고 난 imageString 그것을 통과 img 태그의 src 속성에 IMG 태그를 사용하여 문자열 4.Returned HTML에 화상의 스트링 값으로 coverted 3.Encoded 일했다. 내가 찾던 것을 달성하는 데 도움이되는 stackoverflow 및 다른 블로그에서 찾은 힌트의 많은 솔루션을 찾으십시오. 감사합니다. .