-1

我是 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");
}
}
4

1 回答 1

2

动作类应该具有以下三个属性。
• [inputName]File
• [inputName]FileName
• [inputName]ContentType

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

String filePath = servletRequest.getRealPath("/");
File fileToCreate = new File(filePath, this.uploadphotoFileName);
FileUtils.copyFile(this.uploadphotoFile, fileToCreate);
于 2013-03-25T05:29:41.040 回答