2013-03-25 57 views
-1

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

回答

2

动作类应具有以下三个属性。
•[inputName]文件
•[inputName]文件名
•[inputName]的ContentType

[inputName]是在JSP文件标签(一个或多个)的名称。例如,如果文件标记的名称是uploadphoto,性能将是如下:
•文件uploadphotoFile
•字符串uploadphotoFileName
•字符串uploadphotoContentType

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

现在我得到这个问题的任何一个可以帮助me java.lang.NullPointerException:Source不能为空 at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:630) at org.apache.commons.io.FileUtils.copyFile(FileUtils。 java:606) at com.nre.addproperty.AddProperty.execute(AddPro perty.java:173) 2013年3月25日下午2点14分03秒com.opensymphony.xwork2.util.logging.commons.CommonsLogger信息 信息:删除文件uploadphoto您选择的位置\ upload__1451e278_13da0b7f253__8000_00000011.tmp – Parthiban 2013-04-03 10:41:21

+0

我有相同的问题,答案几乎是正确的,但是,你需要的三个属性是'private File [inputName];','private String [inputName] FileName;','private String [inputName] ContentType;' 所以' java.io.File'属性应该与's:file'标签中给出的名称相同。 – 2015-09-29 09:15:12