BaseForm.java
package com.itcast.web.formbean.product;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class BaseForm extends ActionForm {
private static Properties properties = new Properties();
static{
try {
properties.load(BaseForm.class.getClassLoader().getResourceAsStream("arrowuploadfiletype.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
private int page;
private String query;
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public int getPage() {
return page<1? 1 : page;
}
public void setPage(int page) {
this.page = page;
}
/**
* 验证上传文件类型是否属于图片格式
* @return
*/
public static boolean validateImageFileType(FormFile formfile){
if(formfile!=null && formfile.getFileSize()>0){
List<String> arrowType = Arrays.asList("image/bmp","image/png","image/gif","image/jpg","image/jpeg","image/pjpeg");
List<String> arrowExtension = Arrays.asList("gif","jpg","bmp","png");
String ext = getExt(formfile);
return arrowType.contains(formfile.getContentType().toLowerCase()) && arrowExtension.contains(ext);
}
return true;
}
public static String getExt(FormFile formfile){
return formfile.getFileName().substring(formfile.getFileName().lastIndexOf('.')+1).toLowerCase();
}
/**
* 验证上传文件是否属于图片/flash动画/word文件/exe文件/pdf文件/TxT文件/xls文件/ppt文件
* @param formfile
* @return
*/
public static boolean validateFileType(FormFile formfile){
if(formfile!=null && formfile.getFileSize()>0){
String ext = formfile.getFileName().substring(formfile.getFileName().lastIndexOf('.')+1).toLowerCase();
List<String> arrowType = new ArrayList<String>();
for(Object key : properties.keySet()){
String value = (String)properties.get(key);
String[] values = value.split(",");
for(String v : values){
arrowType.add(v.trim());
}
}
return arrowType.contains(formfile.getContentType().toLowerCase()) && properties.keySet().contains(ext);
}
return true;
}
/*
public boolean validateFileType(String propertyName) throws Exception{
PropertyDescriptor[] propertydesc = Introspector.getBeanInfo(this.getClass()).getPropertyDescriptors();
boolean exsit = false;
for(PropertyDescriptor property : propertydesc){
if(property.getName().equals(propertyName)){
exsit = true;
Method method = property.getReadMethod();
if(method!=null){
FormFile formfile = (FormFile) method.invoke(this);
if(formfile!=null && formfile.getFileSize()>0){
List<String> arrowType = Arrays.asList("image/bmp","image/png","image/gif","image/jpeg","image/pjpeg");
return arrowType.contains(formfile.getContentType().toLowerCase());
}
}else{
new RuntimeException(propertyName+"属性的getter方法不存在");
}
}
}
if(!exsit) new RuntimeException(propertyName+"属性不存在");
return true;
}*/
}
ProductForm.java
package com.itcast.web.formbean.product;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.upload.FormFile;
import com.itcast.utils.ImageSizer;
public class ProductForm extends BaseForm {
private Integer productid;
private Integer[] productids;
/** 货号 **/
private String code;
/** 产品名称 **/
private String name;
/** 品牌 **/
private String brandid;
/** 型号 **/
private String model;
/** 底价(采购进来的价格) **/
private Float baseprice;
/** 市场价 **/
private Float marketprice;
/** 销售价 **/
private Float sellprice;
/** 重量 单位:克 **/
private Integer weight;
/** 产品简介 **/
private String description;
/** 购买说明 **/
private String buyexplain;
/** 产品类型 **/
private Integer typeid;
/** 性别要求 **/
private String sex;
private Integer[] stylesids;
private String stylename;
private FormFile imagefile;
private Float startsellprice;
private Float endsellprice;
private Float startbaseprice;
private Float endbaseprice;
private Integer productstyleid;
public Integer[] getStylesids() {
return stylesids;
}
public void setStylesids(Integer[] stylesids) {
this.stylesids = stylesids;
}
public Integer getProductstyleid() {
return productstyleid;
}
public void setProductstyleid(Integer productstyleid) {
this.productstyleid = productstyleid;
}
public Integer[] getProductids() {
return productids;
}
public void setProductids(Integer[] productids) {
this.productids = productids;
}
public Float getStartbaseprice() {
return startbaseprice;
}
public void setStartbaseprice(Float startbaseprice) {
this.startbaseprice = startbaseprice;
}
public Float getEndbaseprice() {
return endbaseprice;
}
public void setEndbaseprice(Float endbaseprice) {
this.endbaseprice = endbaseprice;
}
public Float getStartsellprice() {
return startsellprice;
}
public void setStartsellprice(Float startsellprice) {
this.startsellprice = startsellprice;
}
public Float getEndsellprice() {
return endsellprice;
}
public void setEndsellprice(Float endsellprice) {
this.endsellprice = endsellprice;
}
public String getStylename() {
return stylename;
}
public void setStylename(String stylename) {
this.stylename = stylename;
}
public FormFile getImagefile() {
return imagefile;
}
public void setImagefile(FormFile imagefile) {
this.imagefile = imagefile;
}
public Integer getProductid() {
return productid;
}
public void setProductid(Integer productid) {
this.productid = productid;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrandid() {
return brandid;
}
public void setBrandid(String brandid) {
this.brandid = brandid;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public Float getBaseprice() {
return baseprice;
}
public void setBaseprice(Float baseprice) {
this.baseprice = baseprice;
}
public Float getMarketprice() {
return marketprice;
}
public void setMarketprice(Float marketprice) {
this.marketprice = marketprice;
}
public Float getSellprice() {
return sellprice;
}
public void setSellprice(Float sellprice) {
this.sellprice = sellprice;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getBuyexplain() {
return buyexplain;
}
public void setBuyexplain(String buyexplain) {
this.buyexplain = buyexplain;
}
public Integer getTypeid() {
return typeid;
}
public void setTypeid(Integer typeid) {
this.typeid = typeid;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
/**
* 保存产品图片
* @param request
* @param imagefile 上传的产品图片
* @param productTypeId 产品类别id
* @param productId 产品id
* @return
* @throws Exception
*/
public static void saveProductImageFile(HttpServletRequest request, FormFile imagefile,
Integer productTypeId,Integer productId,String filename) throws Exception{
String ext = BaseForm.getExt(imagefile);
String pathdir = "/images/product/"+ productTypeId+ "/"+ productId+ "/prototype";//构建文件保存的目录
//得到图片保存目录的真实路径
String realpathdir = request.getSession().getServletContext().getRealPath(pathdir);
File savedir = new File(realpathdir);
File file = saveFile(savedir, filename, imagefile.getFileData());
String pathdir140 = "/images/product/"+ productTypeId+ "/"+ productId+ "/140x";//140宽度的图片保存目录
String realpathdir140 = request.getSession().getServletContext().getRealPath(pathdir140);
File savedir140 = new File(realpathdir140);
if(!savedir140.exists()) savedir140.mkdirs();//如果目录不存在就创建
File file140 = new File(realpathdir140, filename);
ImageSizer.resize(file, file140, 140, ext);
}
/**
* 保存文件
* @param savedir 存放目录
* @param fileName 文件名称
* @param data 保存的内容
* @return 保存的文件
* @throws Exception
*/
public static File saveFile(File savedir, String fileName, byte[] data) throws Exception{
if(!savedir.exists()) savedir.mkdirs();//如果目录不存在就创建
File file = new File(savedir, fileName);
FileOutputStream fileoutstream = new FileOutputStream(file);
fileoutstream.write(data);
fileoutstream.close();
return file;
}
}
ProductTypeForm.java
package com.itcast.web.formbean.product;
public class ProductTypeForm extends BaseForm {
private Integer parentid;
private String name;
private String note;
private Integer typeid;
public Integer getTypeid() {
return typeid;
}
public void setTypeid(Integer typeid) {
this.typeid = typeid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Integer getParentid() {
return parentid;
}
public void setParentid(Integer parentid) {
this.parentid = parentid;
}
}
BrandForm.java
package com.itcast.web.formbean.product;
import org.apache.struts.upload.FormFile;
public class BrandForm extends BaseForm {
private String name;
private FormFile logofile;
private String code;
private String logoimagepath;
public String getLogoimagepath() {
return logoimagepath;
}
public void setLogoimagepath(String logoimagepath) {
this.logoimagepath = logoimagepath;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public FormFile getLogofile() {
return logofile;
}
public void setLogofile(FormFile logofile) {
this.logofile = logofile;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
FrontProductForm.java
package com.itcast.web.formbean.product;
public class FrontProductForm extends BaseForm {
private String sort;
private Integer typeid;
private String brandid;
private String sex;
private Integer productid;
private String style;
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public Integer getProductid() {
return productid;
}
public void setProductid(Integer productid) {
this.productid = productid;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBrandid() {
return brandid;
}
public void setBrandid(String brandid) {
this.brandid = brandid;
}
public Integer getTypeid() {
return typeid;
}
public void setTypeid(Integer typeid) {
this.typeid = typeid;
}
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
}
CartForm.java
package com.itcast.web.formbean.product;
public class CartForm extends BaseForm {
private Integer productid;
private Integer styleid;
private String buyitemid;
public String getBuyitemid() {
return buyitemid;
}
public void setBuyitemid(String buyitemid) {//productid-styleid
this.buyitemid = buyitemid;
if(this.buyitemid!=null){
String[] values = this.buyitemid.split("-");
if(values.length==2){
this.productid = new Integer(values[0]);
this.styleid = new Integer(values[1]);
}
}
}
public Integer getProductid() {
return productid;
}
public void setProductid(Integer productid) {
this.productid = productid;
}
public Integer getStyleid() {
return styleid;
}
public void setStyleid(Integer styleid) {
this.styleid = styleid;
}
}