读书人

struts2下传文件活动文件名、类型

发布时间: 2012-08-26 16:48:06 作者: rapoo

struts2上传文件活动文件名、类型
public class FileUpload {

private File image;//获取上传文件
private String imageFileName;//获取上传文件名称
private String imageContentType;//获取上传文件类型

public String getImageContentType() {
return imageContentType;
}

public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}

public File getImage() {
return image;
}

public void setImage(File image) {
this.image = image;
}

public String getImageFileName() {
return imageFileName;
}

public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

public String execute(){
String path = ServletActionContext.getServletContext().getRealPath("/images");

System.out.println(path);
if(image != null){
File savefile = new File(new File(path),imageFileName);
if(!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
try {
FileUtils.copyFile(image , savefile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String[] t = imageContentType.split("/");
for(String s : t)
System.out.println(s);
}
return "success";
}
}

读书人网 >编程

热点推荐