2014-09-15 5 views
1

이미지를 mySQL 데이터베이스에 업로드하고 struts2 태그를 사용하여 JSP에 표시하려고합니다. 이미지를 서버에 업로드 할 수는 있지만 JSP로 표시 할 수는 없습니다. 나는 인터넷에서 수색을하고 내가 할 수있는 모든 가능한 변화를했다. 여전히 나는 이미지가 업로드되었는지 아닌지에 대해 새로운 의심을 품은 jsp에 이미지를 보여줄 수 없었다. 제 코드를 살펴보고 제가 실수를하고있는 곳을 말해주십시오. 고맙습니다.struts2를 사용하여 업로드 된 이미지를 표시 할 수 없습니다.

여기 내 jsp입니다.

<%@ taglib prefix="s" uri="/struts-tags"%> 
<div id="form_container" align="center"> 
    <s:form action="upload" method="post" enctype="multipart/form-data"> 
     . 
     . 
     <s:file   name="image"     label="Retinal Image"/> 
     <s:submit  value="upload"/> 
    </s:form> 
</div> 

액션 클래스.

private File image; 
private String imageContentType; 
private String imageFileName; 
//getters and setters 

String filePath = servletRequest.getSession().getServletContext().getRealPath("/"); 
System.out.println("Server path:" + filePath); 
File fileToCreate = new File(filePath, uploadBean.getImageFileName()); 

System.out.println("fileToCreate="+fileToCreate.getName()); 
FileUtils.copyFile(uploadBean.getImage(), fileToCreate); 
Map session  = ActionContext.getContext().getSession();  
session.put("image",filePath); 
//some code for keeping image in database which is showing as success as I can see blob in the table after this code was run 

UploadBean.java 마지막 이미지는 성공 페이지로 표시한다. Uploaded.jsp

<s:property value="imageFileName"/> 
<s:property value="imageContentType"/> 
<s:property value="image"/> 
<img src="#session.image"/> 

내 struts.xml은

<action name="upload"  class="com.ActionClasses.UploadAction"> 
    <interceptor-ref name="fileUpload"> 
     <param name="maximumSize">3145728</param> 
     <param name="allowedTypes">image/png,image/gif,image/jpeg,image/pjpeg, image/jpg</param> 
    </interceptor-ref> 
    <interceptor-ref name="defaultStack"></interceptor-ref> 
    <result name="success" type="tiles"> uploaded </result> 
    <result name="error" type="tiles"> upload  </result> 
    <result name="input" type="tiles"> upload  </result> 
</action> 

나에게 내가했던 실수를 알려주십시오입니다. 고맙습니다.

+0

PLZ 통해 UR struts.xml을 게시에

imageRelPath="./abc.jpg" i.e session.put("image","./"+uploadBean.getImageFileName()); 

저장되지 않습니다 귀하의 경우

imageRelPath="./image/abc.jpg" 

될 것입니다 too –

+1

세션 객체 안에 파일 경로 만 넣습니다. 파일 이름도 갖고 있니? 세션 객체를 넣기 전에 값을 확인하십시오. –

+0

@AnkitSharma 당신이 말한 변화를 만들었습니다. 그러나 정지 영상은 표시되지 않습니다. – Pawan

답변

0

이미지 경로이어야 상대 경로 .IE relativepath를 \ + 여기서 filename.ext

string imageRelPath=<relativePath>+"\\"+uploadBean.getImageFileName() 

session.put("image"imageRelPath) 

예 : (당신이 이미지 폴더

문자열적인 filePath = servletRequest.getSession에서 파일 abc.jpg을 업로드 Supose) .getServletContext(). getRealPath ("/");

filePath=filePath+"\\"image; 

다음 "imageRelPath는"당신이 하위 폴더

+0

답장을 보내 주셔서 감사합니다. 아직도 나는 JSP로 이미지를 보여줄 수 없다. – Pawan

+0

"filePath"및 "uploadBean.getImageFileName()"에 대한 값을 제공 할 수 있습니까? – SeeTheC

+0

/home/saipavan/apache-tomcat-7.0.54/wtpwebapps/DReST/O15091400007. 이것은 filePath + uploadBean.getImageFileName() – Pawan

관련 문제