File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes how to upload  an image to a specific folder & displaying it in struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to upload  an image to a specific folder & displaying it in struts2" Watch "how to upload  an image to a specific folder & displaying it in struts2" New topic
Author

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

xrcwrn ss
Greenhorn

Joined: Apr 04, 2010
Posts: 2

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Apr 14, 2010
Posts: 28
thanks for file upload example.
it works!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to upload an image to a specific folder & displaying it in struts2