📄 productmanagerservlet.java
字号:
/*
* ProductAddServlet.java
*
* Created on 2007年4月30日, 上午9:58
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ebuy.web.servlets;
import com.ebuy.businessfacade.ProductFacade;
import com.ebuy.entities.ProductEntity;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
*
* @author Administrator
*/
public class ProductManagerServlet extends HttpServlet
{
private File tempDir;
public void init()
{
// String path= this.getServletContext().getRealPath("temp");
String path="c:\\temp\\";
this.tempDir=new File(path);
if(!this.tempDir.exists())
{
this.tempDir.mkdirs();
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException
{
// String type=request.getParameter("exectype");
// if(type.equalsIgnoreCase("add"))
{
this.execAdd(request,response);
}
}
private void execAdd(HttpServletRequest request,HttpServletResponse response) throws IOException
{
String productname=null;
String strcategoryid=null;
String strprice=null;
String strquantity=null;
String strdown=null;
String script=null;
DiskFileItemFactory factory=new DiskFileItemFactory();
factory.setSizeThreshold(1024*4);
factory.setRepository(this.tempDir);
ServletFileUpload upload=new ServletFileUpload(factory);
upload.setSizeMax(1024*1000);
try
{
java.util.List list = upload.parseRequest(request);
java.util.Iterator<FileItem> iter=list.iterator();
FileItem item=null;
FileItem postfile=null;
while(iter.hasNext())
{
item=iter.next();
if(item.isFormField())
{
String paraname=item.getFieldName();
if(paraname.equalsIgnoreCase("productname"))
{
String temp=item.getString();
byte[] b=temp.getBytes("ISO8859_1");
productname=new String(b,"gb2312");
}
else if(paraname.equalsIgnoreCase("categoryid"))
{
strcategoryid=item.getString();
}
else if(paraname.equalsIgnoreCase("unitlprice"))
{
strprice=item.getString();
}
else if(paraname.equalsIgnoreCase("quantity"))
{
strquantity=item.getString();
}
else if(paraname.equalsIgnoreCase("downbound"))
{
strdown=item.getString();
}
}
else
{
postfile=item;
}
}
String postfilename=postfile.getName();
int pos=postfilename.lastIndexOf(".");
String filetype=postfilename.substring(pos);
String imagename=System.currentTimeMillis()+filetype;
String imageDir=this.getServletContext().getRealPath("ProductImage");
ProductEntity p=new ProductEntity();
p.setCategoryid(Integer.parseInt(strcategoryid));
p.setDownbound(Integer.parseInt(strdown));
p.setImagename(imagename);
p.setProductname(productname);
p.setQuantity(Integer.parseInt(strquantity));
p.setUnitprice(Double.parseDouble(strprice));
//数据库操作成功以后
boolean b= ProductFacade.addPrdouct(p);
if(b)
{
File f=new File(imageDir,imagename);
postfile.write(f);
script="<script>window.alert('保存成功');window.location='productadd.jsp';</script>";
}
else
{
script="<script>window.alert('数据库错误');window.location='productadd.jsp';</script>";
}
}
catch (Exception ex)
{
script="<script>window.alert('文件操作失败');window.location='productadd.jsp';</script>";
ex.printStackTrace();
}
PrintWriter out=response.getWriter();
out.write(script);
out.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -