无法在struts2文件上传中获取文件名

时间:2013-03-25 04:48:38

标签: struts2

我是Struts2的新手。我无法获取文件名和路径。请帮助任何人。

import java.io.File;
import java.util.ResourceBundle;

import org.apache.commons.io.FileUtils;

import nre.dao.DBconnection;

import com.mysql.jdbc.Connection;
import com.opensymphony.xwork2.ActionSupport;

 public class AddProperty extends ActionSupport
{

private String propertyid;
private String propertyname;
private String country;
private String state;
private String city;
private String description;
private File uploadphoto;
private String photofiletype;
private String photoname;

public String getPropertyid()
{
    return propertyid;
}
public void setPropertyid(String propertyid)
{
    this.propertyid = propertyid;
}
public String getPropertyname()
{
    return propertyname;
}
public void setPropertyname(String propertyname)
{
    this.propertyname = propertyname;
}
public String getCountry()
{
    return country;
}
public void setCountry(String country)
{
    this.country = country;
}
public String getState()
{
    return state;
}
public void setState(String state)
{
    this.state = state;
}
public String getCity()
{
    return city;
}
public void setCity(String city)
{
    this.city = city;
}
public String getDescription()
{
    return description;
}
public void setDescription(String description)
{
    this.description = description;
}
public File getUploadphoto()
{
    return uploadphoto;
}
public void setUploadphoto(File uploadphoto)
{
    this.uploadphoto = uploadphoto;
}

public String getPhotofiletype() {
    return photofiletype;
}

public void setPhotofiletype(String photofiletype) {
    this.photofiletype = photofiletype;
}

public String getPhotoname() {
    return photoname;
}

public void setPhotoname(String photoname) {
    this.photoname = photoname;
}

public String execute(){

    DBconnection connection=new DBconnection();
    connection.getConnection();

    try{

        String filepath=connection.filepath;

        System.out.println("filepath : : "+filepath);
        System.out.println("photoname : : "+photoname);

        if(filepath!=null && photoname!=null){
            File filetocreate=new File(filepath,photoname);
            FileUtils.copyFile(uploadphoto, filetocreate);  
        }

    }catch(Exception e){
        e.printStackTrace();
        addActionError(e.getMessage());

        return INPUT;
    }

    String query ="insert into addproperty(propertyid,propertyname,propertycity,propertystate,propertycountry,addedby,addeddate,removeddate) values ('"+propertyid+"','"+propertyname+"','"+city+"','"+state+"','"+country+"','Parthi',now(),NULL)";
    connection.executeUpdate(query);

    System.out.println("Completed Inserting");

    return SUCCESS;

    //System.out.println("Class completed");
}
}

1 个答案:

答案 0 :(得分:2)

Action类应具有以下三个属性。
•[inputName]文件
•[inputName] FileName
•[inputName] ContentType

[inputName]是JSP上文件标记的名称。例如,如果文件标签的名称为uploadphoto,则属性如下:
•文件uploadphotoFile
•String uploadphotoFileName
•String uploadphotoContentType

String filePath = servletRequest.getRealPath("/");
File fileToCreate = new File(filePath, this.uploadphotoFileName);
FileUtils.copyFile(this.uploadphotoFile, fileToCreate);