|
@@ -0,0 +1,107 @@
|
|
|
+package cn.servlet;
|
|
|
+
|
|
|
+import cn.domain.Product;
|
|
|
+import cn.service.ProductService;
|
|
|
+import cn.service.impl.ProductServiceImpl;
|
|
|
+import cn.util.UUIDUtils;
|
|
|
+import org.apache.catalina.connector.Request;
|
|
|
+import org.apache.commons.fileupload.FileItem;
|
|
|
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
|
+import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
|
|
+
|
|
|
+import javax.servlet.ServletException;
|
|
|
+import javax.servlet.annotation.MultipartConfig;
|
|
|
+import javax.servlet.annotation.WebServlet;
|
|
|
+import javax.servlet.http.HttpServlet;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.List;
|
|
|
+@MultipartConfig
|
|
|
+@WebServlet("/AddProductServlet/*")
|
|
|
+public class AddProductServlet extends HttpServlet {
|
|
|
+ ProductService productService = new ProductServiceImpl();
|
|
|
+ Product product = new Product();
|
|
|
+ @Override
|
|
|
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
+ this.doPost(request,response);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
+ response.setContentType("text/html;charset=UTF-8");
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ request.setCharacterEncoding("UTF-8");
|
|
|
+ String serverPath = getServletContext().getRealPath("/").replace("\\", "/");
|
|
|
+ String path = "D:/idea project/shop/web/products";
|
|
|
+ //临时文件目录
|
|
|
+ String tmpPath = "D:/idea project/shop/web/tmp";
|
|
|
+ //检查我们是否有文件上传请求
|
|
|
+ boolean isMultipart = ServletFileUpload.isMultipartContent(request);
|
|
|
+ //1,声明DiskFileItemFactory工厂类,用于在指定磁盘上设置一个临时目录
|
|
|
+ DiskFileItemFactory disk = new DiskFileItemFactory(1024*10,new File(tmpPath));
|
|
|
+ //2,声明ServletFileUpload,接收上边的临时文件。也可以默认值
|
|
|
+ ServletFileUpload products = new ServletFileUpload(disk);
|
|
|
+ InputStream in = null;
|
|
|
+ String pcompany = null;
|
|
|
+ try {
|
|
|
+ List<FileItem> list = products.parseRequest(request);
|
|
|
+ FileItem id = (FileItem)list.get(0);
|
|
|
+ int pid =Integer.parseInt(id.getString("UTF-8"));
|
|
|
+
|
|
|
+ FileItem name = (FileItem)list.get(2);
|
|
|
+ String pname = name.getString("UTF-8");
|
|
|
+
|
|
|
+ FileItem detail = (FileItem)list.get(3);
|
|
|
+ String pdetail = detail.getString("UTF-8");
|
|
|
+
|
|
|
+ FileItem price = (FileItem)list.get(4);
|
|
|
+ Double pprice = Double.valueOf(price.getString("UTF-8"));
|
|
|
+
|
|
|
+ FileItem company = (FileItem)list.get(5);
|
|
|
+ pcompany = company.getString("UTF-8");
|
|
|
+
|
|
|
+ product.setId(pid);
|
|
|
+ product.setName(pname);
|
|
|
+ product.setPrice(pprice);
|
|
|
+ product.setDetail(pdetail);
|
|
|
+ product.setScompany(pcompany);
|
|
|
+ product.setMid(UUIDUtils.getCode());
|
|
|
+ FileItem file = (FileItem)list.get(1);
|
|
|
+ if(file.getName()=="") {
|
|
|
+ product.setImg("*");
|
|
|
+ }else {
|
|
|
+ //获取文件名:
|
|
|
+ String fileName = file.getName();
|
|
|
+ //获取文件的类型:
|
|
|
+ product.setImg(fileName);
|
|
|
+ String fileType = file.getContentType();
|
|
|
+ //获取文件的字节码:
|
|
|
+ in = file.getInputStream();
|
|
|
+ //文件大小
|
|
|
+ int size = file.getInputStream().available();
|
|
|
+ //声明输出字节流
|
|
|
+ OutputStream out = new FileOutputStream(path+"/"+fileName);
|
|
|
+ //文件copy
|
|
|
+ byte[] b = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ while((len=in.read(b))!=-1){
|
|
|
+ out.write(b, 0, len);
|
|
|
+ }
|
|
|
+ productService.addProduct(product);
|
|
|
+ //request.getRequestDispatcher("index.jsp").forward(request,response);
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ //删除上传生成的临时文件
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ e.getMessage();
|
|
|
+ }
|
|
|
+ //URLEncoder.encode(pcompany,"UTF-8") --- 重定向redirect(参数中文乱码)
|
|
|
+ response.sendRedirect("ProductManagementServlet?scompany="+ URLEncoder.encode(pcompany,"UTF-8"));
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|