2014-06-12 3 views
0

이미지 파일을 데이터베이스에 업로드하려고합니다. 이미지가 저장되기 전에 사용자에게 표시하고 싶습니다.요청 범위 Bean을 사용하여 이미지를 업로드했습니다.

요청 범위의 콩인 UserInfoBean (요청 범위가 변경되지 않음)을 사용하여 데이터를 저장하려고했지만, 페인트가 호출 될 때 preview 바이트 []가 null입니다. preview 또는 image을 저장할 수있는 방법이 있습니까? paintItem이 호출 될 때 UploadItem에 액세스 할 수 있습니까?

info.xhtml 코드 : UserInfoBean의

<a4j:outputPanel id="preview" 
        <a4j:mediaOutput id="imgPreview" 
            element="img" 
            mimeType="image/jpeg" 
            createContent="#{userInfo.paint}" 
            cacheable="false" 
            style="width:150px; height:100px;" /> 
        <br/> 
</a4j:outputPanel> 

    <a4j:outputPanel id="signatureStampDisplay"> 
      <br/>     
      <rich:fileUpload fileUploadListener="#{userInfo.uploadImage}" 
          id="upload" 
          listHeight="40px" 
          acceptedTypes="#{userInfo.supportedTypes}" 
          ontyperejected="alert('Only JPG, GIF, PNG, BMP, and TIF files are accepted');" 
          maxFilesQuantity="1" 
          immediateUpload="true" 
          autoclear ="true" 
          disabled="#{userInfo.stampSet}"> 
          <a4j:support event="onuploadcomplete" 
              limitToList="true" 
              reRender="preview"/> 
      </rich:fileUpload> 

     </a4j:outputPanel>  

부, UploadItem 이미지와 byte[] 미리보기는 :

public void uploadImage(UploadEvent evt) throws IOException{ 
     image = evt.getUploadItem(); 
     if(image != null){ 
      //preview is a byte[] 
      preview = FileUtils.readFileToByteArray(image.getFile()); 
     } 
    } 

public void paint(OutputStream stream, Object object) throws IOException{ 
     if(preview != null){ 
      stream.write(preview); 
     } 
    } 
+1

에 표시 한 후 사용

. 'a4j : mediaOutput'에 적절한'mimeType'을 설정해야합니다. –

+0

@VasilLukach 불행하게도'paint'는'preview'을 null로 봅니다. 나는 당신의 추천 한 변경을했다, 그래도 고맙다. – zomgcopters

+1

업로드가 성공적 이었습니까? 그렇다면 더 긴 범위 또는 DB를 사용하여 미리보기를 다른 위치에 저장하고 다시 칠하십시오. bean이 요청 범위이므로이 데이터를 사용할 수 없습니다. –

답변

0

이 때문에 빈 것을 요청 범위로 전달 될 수 없습니다. `스트림을 추가 내가 바이트를 저장할 수이었던의 HttpSession 객체 javax.servlet.http.HttpSession;

이 필요 동안 [] preview 세션에서 제거가 paint

public void uploadImage(UploadEvent evt) throws IOException{ 
     image = evt.getUploadItem(); 
     if(image != null){ 
      preview = FileUtils.readFileToByteArray(signature.getFile()); 
      HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true); 
      session.setAttribute("preview", preview); 
     } 
    } 

public void paint(OutputStream stream, Object object) throws IOException{ 
     HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true); 
     preview = (byte[]) session.getAttribute("preview"); 
     if(preview != null){ 
      stream.write(preview); 
      stream.close(); 
      session.removeAttribute("preview"); 
      signaturePreview = null; 
     } 
    } 
+0

이것을 추가하려면 html에 를 사용하면 작동하지 않습니다. 뷰 범위에 업로드 된 정보를 저장합니다.이 범위는 a4j : mediaOutput의 createContent 속성에서 액세스 할 수 없으며이 해결 방법이 필요합니다. – zomgcopters