在struts2中上载图像

在struts2中上载图像,struts2,multipartform-data,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.ServletA

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

}
导入java.io.File;
导入java.util.List;
导入javax.servlet.http.HttpServletRequest;
导入org.apache.struts2.ServletActionContext;
导入org.apache.tomcat.util.http.fileupload.FileItem;
导入org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
导入org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
导入com.opensymphony.xwork2.ActionSupport;
公共类上载扩展了ActionSupport{
公共字符串execute()引发异常
{
试一试{
HttpServletRequest=ServletActionContext.getRequest();
系统输出打印项次(“1”);
列表项=新建ServletFileUpload(新建DiskFileItemFactory()).parseRequest(请求);
系统输出打印项次(“2”);
用于(文件项:项)
{
系统输出打印项次(“3”);
如果(!item.isFormField()){
字符串fieldname=item.getFieldName();
System.out.println(字段名);
系统输出打印项次(“4”);
File File=新文件(“F:/www/test/Rohit/workspace_Rohit/uploadWithStruts2/WebContent/uploadimage”,“hi.jpg”);
项目。写入(文件);
}
}
}
捕获(例外e){
系统输出打印ln(e);
}
回归成功;
}
}
我的jsp页面是:

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

//用于在Struts2中上载图像
//Jsp页面是:
//Struts.xml
welcome.jsp
//Test.java
导入java.io.File;
导入java.text.DateFormat;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.StringTokenizer;
导入com.opensymphony.xwork2.ActionSupport;
导入org.apache.commons.io.FileUtils;
公共类测试扩展了ActionSupport{
私有静态最终长serialVersionUID=1L;
私有文件;
公共文件getImageFile(){
返回图像文件;
}
公共无效setImageFile(文件imageFile){
this.imageFile=imageFile;
}
公共字符串execute()引发异常
{
试一试{
//图像随机名称代码
//从这里开始到
DateFormat格式=新的SimpleDataFormat(“yyyy/MM/dd HH:MM:ss”);
日期=新日期();
字符串dt=格式。格式(日期);
字符串名称=”;
StringTokenizer str=新的StringTokenizer(dt);
while(str.hasMoreElements())
{
字符串nm=(字符串)str.nextElement();
名称+=nm;
}
字符串名称1=“”;
StringTokenizer strg=新的StringTokenizer(名称“/”);
while(strg.hasMoreElements())
{
字符串nam=(字符串)strg.nextElement();
name1+=nam;
}
字符串imgname=“”;
StringTokenizer strge=新的StringTokenizer(名称1,“:”);
while(strge.hasMoreElements())
{
字符串na=(字符串)strge.nextElement();
imgname+=na;
}
//用于将图像复制到特定路径的代码
字符串sourceFilePath=imageFile.getAbsolutePath();
//System.out.println(sourceFilePath);
File sourceFile=新文件(sourceFilePath);
File destnationFile=新文件(“E:/Jaydip_Baldha/workspace_new/struts2 upload/WebContent/upload_image/”+imgname+“.jpg”);
copyFile(sourceFile,destnationFile);
}
捕获(例外e)
{
e、 printStackTrace();
}
回归成功;
}
}

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