package com.itheima.tfy.web.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.ServletException;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.itheima.tfy.domain.Book;
import com.itheima.tfy.domain.Category;
import com.itheima.tfy.serice.impl.BusinessServiceImpl;
import com.itheima.tfy.util.BookStoreLogger;
import com.itheima.tfy.util.IdGenertor;
public class ManagerServlet extends HttpServlet {
private BusinessServiceImpl bs=new BusinessServiceImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String operation=request.getParameter("operation");
if("addCategory".equals(operation)){
addCategory(request,response);
}
if("ShowAllCategory".equals(operation)){
ShowAllCategory(request,response);
}
if("addBookUI".equals(operation)){
addBookUI(request,response);
}
if("addBook".equals(operation)){
addBook(request,response);
}
}
//转向添加图书页面,主要目的是显示图书分类,便于选择
private void addBookUI(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List<Category> cs=bs.findAllCategory();
BookStoreLogger.info("转向添加图书页面,显示图书分类,查询结果为:"+cs.size());
System.out.println("addBookUI");
request.setAttribute("cs", cs);
request.getRequestDispatcher("/manager/addBook.jsp").forward(request, response);
}
//添加图书,保存图书信息,上传图片
private void addBook(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//保存上传路径
String storePath=getServletContext().getRealPath("/images");
try{
Book book=new Book();
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
boolean isMultipart=ServletFileUpload.isMultipartContent(request);
if(isMultipart){
List<FileItem> items=upload.parseRequest(request);
for(FileItem item:items){
if(item.isFormField()){
//普通字段
String fileName=item.getName();
String fileValue=item.getString("UTF-8");
BeanUtils.setProperty(book, fileName, fileValue);
}else{
//上传字段
String fileName=item.getName();
fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
fileName=IdGenertor.getId()+"_"+fileName;
String storeFile=storePath+"\\"+fileName;
InputStream in=item.getInputStream();
OutputStream out=new FileOutputStream(storeFile);
int len=-1;
byte [] b=new byte[1024];
while((len=in.read(b))!=-1){
out.write(b, 0, len);
}
in.close();
out.close();
item.delete();
book.setImage("/images"+fileName);
}
}
}
BookStoreLogger.info("将要保存的书的信息内容"+book);
bs.addBook(book);
request.setAttribute("message", "添加成功");
}catch(Exception e){
BookStoreLogger.error(e.getMessage());
request.setAttribute("message", "服务器忙");
}
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
//查询显示所有的分类信息
private void ShowAllCategory(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List<Category> cs=bs.findAllCategory();
BookStoreLogger.info("查询显示所有的分类信息,分类结果:"+cs.size());
request.setAttribute("cs",cs);
request.getRequestDispatcher("/manager/listCategory.jsp").forward(request, response);
}
//添加分类
private void addCategory(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try{
Category c=new Category();
BeanUtils.populate(c, request.getParameterMap());
BookStoreLogger.info("将要保存的分类内容"+c);
bs.addCategory(c);www.zzzyk.com
request.setAttribute("message", "添加成功");
}catch(Exception e){
BookStoreLogger.error(e.getMessage());
request.setAttribute("message", "服务器忙");
}
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}