Thursday, April 7, 2011

uploading image and displaying

FileUploadAction.java


package net.viralpatel.struts2;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport implements
        ServletRequestAware {
 private File userImage;
 private String userImageContentType;
 private String userImageFileName;

 private HttpServletRequest servletRequest;

    @Override
  public String execute() {
       try {

           String filePath = servletRequest.getRealPath("/");
           System.out.println("Server path:" + filePath);
           File fileToCreate = new File(filePath, this.userImageFileName);

           FileUtils.copyFile(this.userImage, fileToCreate);
       } catch (Exception e) {
            addActionError(e.getMessage());

         return INPUT;
       }
       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;
 }

   @Override
   public void setServletRequest(HttpServletRequest servletRequest) {
      this.servletRequest = servletRequest;

   }
}





UserImage.jsp


This is done using the struts2.0  framework,you need to have the plugin for it.


<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Upload User Image</title>
</head>
<body>
<h2>Struts2 File Upload & Save Example</h2>
<s:actionerror />
<s:form action="userImage" method="post" enctype="multipart/form-data">
<s:file name="userImage" label="User Image" />

<s:submit value="Upload" align="center" />
</s:form>
</body>
</html>

SuccessUserImage
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Success: Upload User Image</title>

</head>
<body>
   <h2>Struts2 File Upload Example</h2>
  
 User Image: <s:property value="userImage"/>

 Content Type: <s:property value="userImageContentType"/>

 File Name: <s:property value="userImageFileName"/>

 Uploaded Image:


 <img src="<s:property value="userImageFileName"/>"/>
</body>
</html>


struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <include file="example.xml"/>
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
        <action name="userImage"
    class="net.viralpatel.struts2.FileUploadAction">
    <interceptor-ref name="fileUpload">
          <param name="maximumSize">2097152</param>

          <param name="allowedTypes">
              image/png,image/gif,image/jpeg,image/pjpeg
         </param>
     </interceptor-ref>
       <interceptor-ref name="defaultStack"></interceptor-ref>
    <result name="success">SuccessUserImage.jsp</result>

      <result name="input">UserImage.jsp</result>
</action>
    </package>
</struts>
 
output




8 comments:

  1. It's working...thanks for this post

    ReplyDelete
  2. thanks a lot...
    every example in here is first being tested then,posted.

    ReplyDelete
  3. my uploaded image will display only after refreshing the page..
    i want it to be display without refreshing....
    Any Solutions...???

    ReplyDelete
  4. Replies
    1. hi all please visit my codemyday.com for more tutorials.

      Delete
  5. hi all please visit my codemyday.com for more tutorials.

    ReplyDelete
  6. Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. image data entry services

    ReplyDelete

java-8-streams-map-examples

package com.mkyong.java8; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; im...