1.文件上传的action,各位看具体实现思路就行。
package cn.ljj.action.back;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import cn.ljj.service.ICategoryService;
import cn.ljj.service.ICategorysecondService;
import cn.ljj.service.IProductService;
import cn.ljj.utils.PageBean;
import cn.ljj.vo.Category;
import cn.ljj.vo.Categorysecond;
import cn.ljj.vo.Product;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
*
* @author ljj
*
*/
public class AdminProductAction extends ActionSupport implements
ModelDriven<Product> {
private Product product = new Product();
public Product getModel() {
return product;
}
private Integer page;
public void setPage(Integer page) {
this.page = page;
}
public Integer getPage() {
return page;
}
// 注入ProductService
private IProductService productService;
private ICategoryService categoryService;
public void setCategoryService(ICategoryService categoryService) {
this.categoryService = categoryService;
}
public void setProductService(IProductService productService) {
this.productService = productService;
}
// struts2文件上传属性
private File upload;
private String uploadFileName;
private String uploadContentType;
private int cid;
public void setCid(int cid) {
this.cid = cid;
}
public void setUpload(File upload) {
this.upload = upload;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
// 查找所有商品
public String findAll() {
PageBean<Product> pageBean = productService.findAll(page);
//将pageBean放入值栈
ActionContext.getContext().getValueStack().set("pageBean", pageBean);
return "getAllProduct";
}
// 保存商品
public String save() throws IOException {
// product.setImage(image);
if(upload != null){
String path = ServletActionContext.getServletContext().getRealPath(
"/products");
String fileName = UUID.randomUUID()+"."+this.uploadContentType.split("/")[1];
File diskFile = new File(path + "//" + fileName);
FileUtils.copyFile(upload, diskFile);
product.setImage("products/" + fileName);
}
product.setPdate(new Date());
productService.save(product);
return "findAll";
}
// 删除商品
public String delete() {
product = productService.findById(product.getPid());
String path = ServletActionContext.getServletContext().getRealPath(
"/" + product.getImage());
File file = new File(path);
file.delete();
productService.delete(product);
return "findAll";
}
// 编辑
public String edit() {
List<Categorysecond> csList = this.productService.listCategorysecond();
ActionContext.getContext().getValueStack().set("csList", csList);
product = productService.findById(product.getPid());
return "update";
}
// 更新
public String update() throws IOException {
if(upload != null ){
String delPath = ServletActionContext.getServletContext().getRealPath(
"/" + product.getImage());
File file = new File(delPath);
file.delete();
String path = ServletActionContext.getServletContext().getRealPath(
"/products");
String fileName = UUID.randomUUID()+"."+this.uploadContentType.split("/")[1];
File diskFile = new File(path + "//" + fileName);
FileUtils.copyFile(upload, diskFile);
product.setImage("products/" + fileName);
}
product.setPdate(new Date());
productService.update(product);
return "findAll";
}
public void findByCid(){
Category category = this.categoryService.findById(cid);
Set<Categorysecond> list = category.getCategoryseconds();
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
try {
PrintWriter out = response.getWriter();
for(Categorysecond categorysecond:list){
out.println("<option value='"+categorysecond.getCsid()+"'>"+categorysecond.getCsname()+"</option>");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.对于需要上传的文件进行类型以及大小的限定,可以在相应的action中配置拦截器
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/gif,image/jpeg,image/jpg
</param>
<param name="maximumSize">1677216</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>