在struts2中上传图像

时间:2012-09-13 07:09:25

标签: struts2 multipartform-data

在Struts2中上传图片时遇到问题。

我正在尝试将图像从jsp页面上传到struts2中的action类

我的代码已成功运行,但最多执行System.out.println(“2”)并且图像未复制到指定位置。

请帮我解决这个问题

我的行动课程如下:

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

}

我的jsp页面是:

<form action="test.action" method="post" enctype="multipart/form-data">
                <input type="file" name="image">
                <input type="submit" value="upload"/>
</form>

1 个答案:

答案 0 :(得分:1)

// 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; 
}

}