Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9219167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:02:47+00:00 2026-06-18T03:02:47+00:00

I have problem when upload image in Struts2. I am trying to upload an

  • 0

I have problem when upload image in Struts2.

I am trying to upload an image from jsp page to action class in struts2

My code is successfully run but executes up to System.out.println(“2”) and the image is not copied to the specified location.

Please help me to solve this problem

My Action class is below:

import java.io.File;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import com.opensymphony.xwork2.ActionSupport;


public class upload extends ActionSupport  {

public String execute()throws Exception
{
    try{

    HttpServletRequest request = ServletActionContext.getRequest();

    System.out.println("1");
    List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

    System.out.println("2");
    for (FileItem item : items)
    {
        System.out.println("3");
            if (!item.isFormField()){
            String fieldname = item.getFieldName();
            System.out.println(fieldname);

            System.out.println("4");
            File file = new File("F:/www/test/Rohit/workspace_Rohit/uploadWithStruts2/WebContent/uploadimage","hi.jpg");        
            item.write(file);
         }
    }

    }
    catch (Exception e) {
    System.out.println(e);
    }

    return SUCCESS;
    }

}

My jsp page is:

<form action="test.action" method="post" enctype="multipart/form-data">
                <input type="file" name="image">
                <input type="submit" value="upload"/>
</form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:02:50+00:00Added an answer on June 18, 2026 at 3:02 am
    // For upload Image in Struts2
    
    // Jsp Page is: 
    
    <s:form method="post" action="test.action" enctype="multipart/form-data">
    <s:file name="imageFile" label="User Image" />
    <s:submit value="submit"></s:submit>
    
    
    //Struts.xml
    
    <struts>
     <package name="default" extends="struts-default">
    <action name="test" class="Test">
            <result name="success">welcome.jsp</result>
        </action>
      </package>
    </struts>
    
    
    //Test.java
    
    import java.io.File;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.StringTokenizer;
    
    import com.opensymphony.xwork2.ActionSupport;
    import org.apache.commons.io.FileUtils;
    public class test extends ActionSupport{
    
    private static final long serialVersionUID = 1L;
    
    private File imageFile;
    
    public File getImageFile() {
        return imageFile;
    }
    
    public void setImageFile(File imageFile) {
        this.imageFile = imageFile;
    }
    
    public String execute()throws Exception
    {
        try{
    
            //code for image random name
            // start from here to 
    
             DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
             Date date = new Date();
             String dt = format.format(date);
    
             String name = "";
             StringTokenizer str = new StringTokenizer(dt);
             while (str.hasMoreElements())
             {
                 String nm=(String) str.nextElement();
                 name+=nm;
             }
    
             String name1="";
             StringTokenizer strg = new StringTokenizer(name,"/");
                     while (strg.hasMoreElements())
                     {
                         String nam=(String) strg.nextElement();
                         name1+=nam;
                     }
    
             String imgname="";   
             StringTokenizer strge = new StringTokenizer(name1,":");
                     while (strge.hasMoreElements())
                     {
                         String na=(String) strge.nextElement();
                         imgname+=na;   
                     }
    
    
        //code for copy image to specific path
    
        String sourceFilePath=imageFile.getAbsolutePath();
        //System.out.println(sourceFilePath);
        File sourceFile=new File(sourceFilePath);   
        File destnationFile=new File("E:/Jaydip_Baldha/workspace_new/Struts2Upload/WebContent/upload_image/"+imgname+".jpg");
        FileUtils.copyFile(sourceFile, destnationFile);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return SUCCESS; 
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem with my code, iam trying to upload an image to
i'm new, so i cann't upload the image, but i have a problem when
I have a small problem with my PHP image upload code, I`m not getting
I have following client code to upload image from phone $(#placeorder).click( function() { if
i have tried this code to upload an image from gallery to sqllite database
I have a problem here im trying to upload a file first time it
I have a problem with saving the path in doctrine from a file upload.
I have a problem to upload an image using Symfony. I have a form
I have a problem with an image upload in spring 3 mvc. I search
I have the following problem. User can upload image, and I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.