⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addproductaction.java

📁 网上商城一个购物网站具有登陆会员注册添加购物车
💻 JAVA
字号:
package com.hit.struts.action;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.upload.FormFile;

import com.hit.persistence.*;
import com.hit.bean.*;

public final class AddProductAction extends Action{  
	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,  
		HttpServletResponse response) throws Exception {
	
   		DynaActionForm productForm = (DynaActionForm) form;         
		Integer id = (Integer)productForm.get("id");
		Integer sortid = (Integer)productForm.get("sortid");
		String name = (String)productForm.get("name");
		String price = (String)productForm.get("price");
		String saleprice = (String)productForm.get("saleprice");
		String descript = (String)productForm.get("descript");
		String contents = (String)productForm.get("contents");
		FormFile theFile =   (FormFile)productForm.get("theFile");
		
		DBOperate db=new DBOperate();
		Product product;
		if (id.intValue()==0) {
			product = new Product();
		}
		else{
			product = db.getProduct(id.intValue());
		}
		Sort sort = db.getSort(sortid.intValue());
		product.setSort(sort);
		product.setName(name);
		product.setPrice(Double.parseDouble(price));
		product.setSaleprice(Double.parseDouble(saleprice));
		product.setDescript(descript);
		product.setContents(contents);
		product.setSalecount(0);
		SimpleDateFormat df  = new SimpleDateFormat("yyyy-MM-dd");
		product.setSaledate( df.format(new Date()));
		
		if (!theFile.getFileName().equals("")){
				try { 
						InputStream stream = theFile.getInputStream();
						//String filePath = request.getRealPath("/");
						String filePath = this.getServlet().getServletContext().getRealPath("/");
						System.out.println(filePath);
						filePath = filePath.substring(0,filePath.length()-1);
						int i = filePath.lastIndexOf("\\");
						System.out.println(i);
						System.out.println(filePath);
						filePath = filePath.substring(0,i);
						filePath = filePath + "\\ShoppingOnline\\images\\";
						OutputStream bos = new FileOutputStream( filePath +  theFile.getFileName());
						int bytesRead = 0; 
						byte[] buffer = new byte[8192]; 
						while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) { 
							bos.write(buffer, 0, bytesRead);
						} 
						bos.close(); 
						stream.close(); 
				} catch (Exception e) { 
						System.err.print(e); 
				} 
				product.setImage("images/"+ theFile.getFileName());
		}

		if (id.intValue()==0) {
			db.save(product);
		} else {
			db.update(product);
		}
		
 		ActionErrors errors = new ActionErrors();
		errors.add(ActionErrors.GLOBAL_MESSAGE,
				new ActionError("label.saveOk"));
		if (!errors.isEmpty()) {
				saveErrors(request, errors);
		} 
  		return  mapping.findForward("toWrong");
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -