📄 productaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.longHua.web.struts;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import org.apache.struts.validator.DynaValidatorForm;
import com.longHua.domain.Product;
import com.longHua.util.TimeNow;
public class ProductAction extends BaseDispatchAction {
public ActionForward addProduct(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
ActionMessages messages=new ActionMessages();
//得到物品图片的路径
String uploadDir=servlet.getServletContext().getRealPath("/upload");
String ext=null;
String nowTime=null;
//限定上传图片类型
List<String> list=new ArrayList<String>();
list.add("jpg");
list.add("jpeg");
list.add("gif");
list.add("bmp");
list.add("png");
if(!uploadDir.endsWith("/"))
uploadDir.concat("/");
DynaValidatorForm productForm = (DynaValidatorForm) form;
//首先判断是否有图片,如果有.后缀是否正确
FormFile pictureFile=(FormFile)productForm.get("productPictrue");
if(pictureFile ==null){
productForm.set("productPictrue", ".\\"+uploadDir+"\\"+"noPicture.gif");
}else{
//获取图片名称
String pictureName=pictureFile.getFileName();
//获取图片大小
int pictureSize=pictureFile.getFileSize();
//获取图片的扩展名
ext=pictureName.substring(pictureName.lastIndexOf(".")+1, pictureName.length());
//扩展名全部转换成小写
ext=ext.toLowerCase();
//判断是否可上传的类型
if(!list.contains(ext)){
//该文件不能上传,返回的页面
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("user.upload.picture.ext"));
return mapping.findForward("fail");
}
//判断文件的大小
if(pictureSize> 1024 * 1024 * 5){
//该文件不能上传,返回的页面
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("user.upload.picture.size"));
return mapping.findForward("fail");
}
//以防上传的图片有重复的名称,所这里用当前的时间作为图片的名称,首先得到当前时间
nowTime=String.valueOf(System.currentTimeMillis());
//用输入流来读取用户上传的图片
InputStream pictureIn=null;
//将用户上传的图片保存到服务器上特定的路径上
OutputStream pictureOut=null;
try {
pictureIn=pictureFile.getInputStream();
pictureOut= new FileOutputStream(uploadDir+"\\"+nowTime+"."+ext);
int byteread=0;
byte[] bytes=new byte[8192];
while((byteread = pictureIn.read(bytes, 0,8192)) !=-1){
pictureOut.write(bytes,0,byteread);
}
pictureIn.close();
pictureOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
try {
pictureIn.close();
pictureOut.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
//数据库操作,先复制值
Product product=new Product();
product.setProductName(productForm.getString("productName"));
product.setKeywords(productForm.getString("keywords"));
product.setSendType(productForm.getString("sendType"));
product.setPayType(productForm.getString("payType"));
product.setProductUnits(productForm.getString("productUnits"));
product.setNowPlace(productForm.getString("nowPlace"));
product.setProductModel(productForm.getString("productModel"));
product.setProductPictrue(".\\upload\\"+nowTime+"."+ext);
product.setSupplier(productForm.getString("supplier"));
product.setNowPrice(Float.valueOf(productForm.getString("nowPrice")));
product.setProductContent(productForm.getString("productContent"));
product.setCarriagePay(productForm.getString("carriagePay"));
product.setProductDesc(productForm.getString("productDesc"));
product.setProductPrice(Float.valueOf(productForm.getString("productPrice")));
product.setProductCount(Integer.valueOf(productForm.getString("productCount")));
product.setDuration(Date.valueOf("2008-07-12"));
product.setBuyNum(0);
product.setLookNum(0);
product.setRegisterTime(TimeNow.getNowTime());
product.setIsPreferred("N");
product.setIsValid("N");
product.setProductId(0);
product.setCatId(0);
getLongHua().addProduct(product);
return mapping.findForward("success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -