struts2文件上传Demo

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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值