• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to upload an image to a specific folder & displaying it in struts2

 
xrcwrn ss
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

home.jsp

<s:form action="doUpload" method="post" enctype="multipart/form-data">
<s:file name="userImage" label="User Image" />
<s:submit value="Upload" align="center" />
</s:form>
-----------------------------------------------------------------
success.jsp


<b> User Image:</b>
<s:property value="userImage" /><br/>
<b>Content Type:</b>
<s:property value="userImageContentType" /><br/>
<b>File Name:</b>
<s:property value="userImageFileName" /><br/>

<b> Uploaded Image:</b>

<img src="<s:property value="userImageFileName"/>" />
--------------------------------------------------------------------------------------------
struts.xml
<action name="doUpload" class="action.ImageUpload">
<result name="success">/pages/login/UploadedImage.jsp</result>
<result name="input">/pages/login/Home.jsp</result>
</action>

------------------------------------------------------------------------------------------
ImageUpload.java
public class ImageUpload extends ActionSupport implements ServletRequestAware {

private File userImage;
private String userImageContentType;
private String userImageFileName;

private HttpServletRequest servletRequest;
File fp;
public String execute() {

try {
String filePath = servletRequest.getRealPath("/") ;
File fileToCreate = new File(filePath, this.userImageFileName);
FileUtils.copyFile(this.userImage, fileToCreate);
} catch (Exception e) {

e.printStackTrace();
addActionError(e.getMessage());
return INPUT;
}
System.out
.println(" *************************inside image upload*******************8 ");


return SUCCESS;
}

public File getUserImage() {

return userImage;
}

public void setUserImage(File userImage) {

this.userImage = userImage;
}

public String getUserImageContentType() {

return userImageContentType;
}

public void setUserImageContentType(String userImageContentType) {

this.userImageContentType = userImageContentType;
}

public String getUserImageFileName() {

return userImageFileName;
}

public void setUserImageFileName(String userImageFileName) {

this.userImageFileName = userImageFileName;
}

public void setServletRequest(HttpServletRequest servletRequest) {

this.servletRequest = servletRequest;
}

public HttpServletRequest getServletRequest() {
return servletRequest;
}
}



this program is working properly & uploading the image at / & displaying the image in success.jsp.
but when uploading image at /images image is uploaded successfully but in success.jsp in is not showing the image??


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.

You can edit your post by using the button.

Also, please see the JavaRanch naming policy. Please change your display name to conform with this policy. Thanks!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not providing enough information to help; I'd assume that either the full path isn't being used in the image tag or it's missing context information.
 
Girish Wakure
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for file upload example.
it works!
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic