//添加食品信息
private void addProduct(HttpServletRequest request, HttpServletResponse response) throws IOException{
PrintWriter out = response.getWriter();
/*Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date1=format.format(date);
String productName=request.getParameter("produceName");
int typeid=Integer.parseInt(request.getParameter("typename"));
String details=request.getParameter("details");
Float price =Float.parseFloat(request.getParameter("price"));
int stock=Integer.parseInt(request.getParameter("stock"));
String fileName=request.getParameter("fileName");
fileName="images_ganguo/"+fileName;
ProductInfo product=new ProductInfo();
product.setP_typeId(typeid);
product.setProductName(productName);
product.setPrice(price);
product.setDetails(details);
product.setStock(stock);
product.setDate(date1);
product.setFileName(fileName);
int rel=proService.addProductInfo(product);
if(rel>0){
//添加商品成功之后转发到查询所有的商品界面
out.print("<script>alert('恭喜您,添加商品成功!');location.href='ProductInfoServlet?tag=show';</script>");
}else{
out.print("<script>alert('很抱歉,添加商品失败!');location.href='ProductInfoServlet?tag=show';</script>");
}*/
String typename="";//类型名称
String brand="";//品牌
String produceName="";//商品名称
String details="";//商品详情
String price="";//价格
int stock=0;//库存
request.setCharacterEncoding("utf-8");
String uploadFileName="";//上传的文件名(大图)
String uploadFileName1="";//上传的文件名(小图1)
String uploadFileName2="";//上传的文件名(小图2)
String uploadFileName3="";//上传的文件名(小图3)
String uploadFileName4="";//上传的文件名(小图4)
String fileName="";//表单字段元素的name属性值
String filedName="";
String picPath="";
//请求信息中的内容是否是multipart类型
boolean isMultipart =ServletFileUpload.isMultipartContent(request);
//上传文件的存储路径(服务器文件系统上的绝对文件路径)
String uploadFilePath=request.getSession().getServletContext().getRealPath("upload/");
if(isMultipart){
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
try
{
//解析from表单中所有文件
List<FileItem> items=upload.parseRequest(request);
Iterator<FileItem> iter=items.iterator();
while(iter.hasNext()){
FileItem item=(FileItem)iter.next();
if(item.isFormField()){//判断,是普通表单
fileName=item.getFieldName();//表单字段的name属性
if(fileName.equals("typename")){
typename=item.getString("utf-8");
}else if(fileName.equals("brand")){
brand=item.getString("utf-8");
}else if(fileName.equals("produceName")){
produceName=item.getString("utf-8");
}else if(fileName.equals("details")){
details=item.getString("utf-8");
}else if(fileName.equals("price")){
price=item.getString("utf-8");
}else if(fileName.equals("stock")){
stock=Integer.parseInt(item.getString("utf-8"));
}
}else{
//文件表单字段
//表单字段的name属性
fileName=item.getFieldName();
if(fileName.equals("bigfile")){
filedName=item.getName();
if(fileName!=null&&!filedName.equals("")){
File fullFile=new File(item.getName());
File saveFile=new File(uploadFilePath,fullFile.getName());
item.write(saveFile);
uploadFileName=fullFile.getName();
//ServletContext application=this.getServletContext();
picPath=saveFile.toString();
}
}else if(fileName.equals("smallfile1")){
filedName=item.getName();
if(fileName!=null&&!filedName.equals("")){
File fullFile=new File(item.getName());
File saveFile=new File(uploadFilePath,fullFile.getName());
item.write(saveFile);
uploadFileName1=fullFile.getName();
//ServletContext application=this.getServletContext();
picPath=saveFile.toString();
}
}else if(fileName.equals("smallfile2")){
filedName=item.getName();
if(fileName!=null&&!filedName.equals("")){
File fullFile=new File(item.getName());
File saveFile=new File(uploadFilePath,fullFile.getName());
item.write(saveFile);
uploadFileName2=fullFile.getName();
//ServletContext application=this.getServletContext();
picPath=saveFile.toString();
}
}else if(fileName.equals("smallfile3")){
filedName=item.getName();
if(fileName!=null&&!filedName.equals("")){
File fullFile=new File(item.getName());
File saveFile=new File(uploadFilePath,fullFile.getName());
item.write(saveFile);
uploadFileName3=fullFile.getName();
//ServletContext application=this.getServletContext();
picPath=saveFile.toString();
}
}
}
}
}catch(Exception ex){
ex.printStackTrace();
}
Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date1=format.format(date);
ProductInfo product=new ProductInfo();
int typeid=Integer.parseInt(typename);
product.setP_typeId(typeid);
product.setProductName(produceName);
float price1=Integer.parseInt(price);
product.setPrice(price1);
product.setDetails(details);
product.setStock(stock);
product.setDate(date1);
product.setFileName("upload/"+uploadFileName);
product.setFileName_1("upload/"+uploadFileName1);
product.setFileName_2("upload/"+uploadFileName2);
product.setFileName_3("upload/"+uploadFileName3);
int rel=proService.addProductInfo(product);
if(rel>0){
//添加商品成功之后转发到查询所有的商品界面
out.print("<script>alert('恭喜您,添加商品成功!');location.href='ProductInfoServlet?tag=show';</script>");
}else{
out.print("<script>alert('很抱歉,添加商品失败!');location.href='ProductInfoServlet?tag=show';</script>");
}
}
}