【Java开源代码栏目提醒】:网学会员Java开源代码为您提供AddProductAction.java参考,解决您在AddProductAction.java学习中工作中的难题,参考学习。
package cn.com.shopadmin;
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.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;
public final class AddProductAction extends Action{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
DynaValidatorForm productForm = (DynaValidatorForm) 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");
/*
* 鍊熷姪Hibernate瀹屾垚涓庢暟鎹簱鐩稿叧鐨勬搷浣?鎻掑叆鍟嗗搧璁板綍
*/
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("/"); //取锟斤拷前
系统鍙栧綋鍓嶇郴缁熻矾寰?
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());
}
db.saveOrUpdate(product);//淇濆瓨鍟嗗搧瀵硅薄
/*
*杞嚦淇濆瓨鎴愬姛鎻愮ず椤甸潰
*/
ActionMessages errors = new ActionMessages();
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("label.saveOk"));
if (!errors.isEmpty()) {
saveErrors(request, errors);
}
return mapping.findForward("toWrong"); //淇濆瓨鎴愬姛鎻愮ず椤甸潰
}
}