2013-03-25 75 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