package com.zzy.struts;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zzy.entity.Entity;
import com.zzy.entityimp.EntityImp;
@SuppressWarnings("serial")
public class AdminAdds extends ActionSupport {
private int id;
private String name;
private String author;
private String pulish;
private Double price;
private int currcount;
private File upload;
private String uploadFileName;
public String adds() throws Exception{
EntityImp ei=new EntityImp();
Entity ey=new Entity(name, author, pulish,price,currcount,"upload/"+uploadFileName);
int count=ei.adds(ey);
if(count>0){
String filename=ServletActionContext.getServletContext().getRealPath("/upload");
filename+=File.separator+uploadFileName;
File file=new File(filename);
FileUtils.copyFile(upload, file);
return "success";
}else{
return "error";
}
}
public String dels(){
EntityImp ei=new EntityImp();
Entity ey=ei.selects(id);
String path=ey.getPicture();
if(ei.dels(id)>0){
String filename=ServletActionContext.getServletContext().getRealPath(path);
File f=new File(filename);
if(f.exists()){
f.delete();
}
return SUCCESS;
}else{
return ERROR;
}
}
public String update() throws Exception{
//调用对象方法
EntityImp ei=new EntityImp();
//根据id获取信息
Entity ey=null;
ey=ei.selects(id);
String path=ey.getPicture();
//判断图片是否为空
if(uploadFileName==null && upload==null){
//如果为空,将获取的信息填入到实体中
ey= new Entity(id,name, author, pulish,price,currcount,path);
}else{
ey=new Entity(id,name, author, pulish,price,currcount,"upload/"+uploadFileName);
}
int count=ei.update(ey);
if(count>0){
if(uploadFileName!=null && upload!=null){
String fi=ServletActionContext.getServletContext().getRealPath(path);
File f=new File(fi);
if(f.exists()){
f.delete();
}
String filename=ServletActionContext.getServletContext().getRealPath("/upload");
filename+=File.separator+uploadFileName;
File file=new File(filename);
FileUtils.copyFile(upload, file);
}
return SUCCESS;
}else{
return ERROR;
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPulish() {
return pulish;
}
public void setPulish(String pulish) {
this.pulish = pulish;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public int getCurrcount() {
return currcount;
}
public void setCurrcount(int currcount) {
this.currcount = currcount;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
}