package com.wyw.bookstroc.servlet.manager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
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;
import org.apache.commons.io.IOUtils;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.wyw.bookstroe.domain.Products;
import com.wyw.bookstroe.service.ProductsService;
import com.wyw.bookstroe.service.impl.ProductsServiceimpl;
import com.wyw.bookstroe.utils.IdUtils;
import sun.nio.ch.IOUtil;
import sun.print.PrinterJobWrapper;
@WebServlet("/addProduct")
public class addProduct extends HttpServlet {
private static final long serialVersionUID = 1L;
public addProduct() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
//创建Products,将上传数据封装
Products p = new Products();
//用于保存id和图片路径
Map<String, String> map=new HashMap<>();
//封装id
map.put("id", IdUtils.getUUID());
//实例化作用:可以设置缓存大小以及临时文件保存位置
DiskFileItemFactory factory=new DiskFileItemFactory();
//设置临时文件位置
factory.setRepository(new File(this.getServletContext().getRealPath("/temp")));
//
factory.setSizeThreshold(1024*1024*10);
ServletFileUpload upload=new ServletFileUpload(factory);
//处理上传文件中文乱码
upload.setHeaderEncoding("utf-8");
try {
List<FileItem> items=upload.parseRequest(request);
for(FileItem item:items){
if(item.isFormField()){
String fileName=item.getFieldName();
String value=item.getString("utf-8");
map.put(fileName, value);
}else{
String fileName=item.getName();
fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
fileName=UUID.randomUUID().toString()+"_"+fileName;
SimpleDateFormat sdFormat=new SimpleDateFormat("yyyyMMdd");
String date=sdFormat.format(new Date());
fileName=date+fileName;
String imgurl_parent="/productImg/";
File parentDir = new File(this.getServletContext().getRealPath(imgurl_parent));
if (!parentDir.exists()) {
parentDir.mkdir();
}
String imgurl=imgurl_parent+"/"+fileName;
map.put("imgurl", imgurl);
IOUtils.copy(item.getInputStream(), new FileOutputStream(new File(parentDir,fileName)));
item.delete();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
BeanUtils.populate(p, map);
} catch (IllegalAccessException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ProductsService service=new ProductsServiceimpl();
try {
service.addProducts(p);
response.sendRedirect(request.getContextPath()+"/ListProducts");
} catch (SQLException e) {
// TODO Auto-generated catch block
response.getWriter().write("添加商品失败");
e.printStackTrace();
return;
}
}
}
评论0