package com.enjoy.book.action;
import com.alibaba.fastjson.JSON;
import com.enjoy.book.bean.Book;
import com.enjoy.book.biz.BookBiz;
import com.enjoy.book.util.DateHelper;
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 javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;
@WebServlet("/book.let")
public class BookServlet extends HttpServlet {
BookBiz bookBiz = new BookBiz();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
/**
* /book.let?type=add 添加图书
* /book.let?type=modifypre&id=xx 修改前准备
* /book.let?type=modify 修改
* /book.let?type=remove&id=xx 删除
* /book.let?type=query&pageIndex=1 :分页查询(request:转发)
* /book.let?type=details&id=xx 展示书籍详细信息
* /book.let?type=doajax&name=xx :使用ajax查询图书名对应的图书信息
*
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
//验证用户是否登录
HttpSession session = req.getSession();
if(session.getAttribute("user")==null){
out.println("<script>alert('请登录');parent.window.location.href='login.html';</script>");
return;
}
String type = req.getParameter("type");
switch (type){
case "add":
try {
add(req,resp,out);
} catch (FileUploadException e) {
e.printStackTrace();
resp.sendError(500,"文件上传失败");
} catch (Exception e) {
e.printStackTrace();
resp.sendError(500,e.getMessage());
}
break;
case "modifypre":
//book.let?type=modifypre&id=xx
long bookId = Long.parseLong(req.getParameter("id"));
Book book = bookBiz.getById(bookId);
req.setAttribute("book",book);
req.getRequestDispatcher("book_modify.jsp").forward(req,resp);
break;
case "modify":
try {
modify(req,resp,out);
} catch (Exception e) {
e.printStackTrace();
}
break;
case "remove":
//1.获取删除的bookid
long removeId = Long.parseLong(req.getParameter("id"));
//2. 调用biz删除方法
try {
int count = bookBiz.remove(removeId);
if(count>0){
out.println("<script>alert('图书删除成功'); location.href='book.let?type=query&pageIndex=1';</script>");
}else{
out.println("<script>alert('图书删除失败'); location.href='book.let?type=query&pageIndex=1';</script>");
}
} catch (Exception e) {
e.printStackTrace();
out.println("<script>alert('"+e.getMessage()+"'); location.href='book.let?type=query&pageIndex=1';</script>");
}
//3. 提示+跳转=>out (查询的servlet)
break;
case "query":
query(req,resp,out);
break;
case "details":
details(req,resp,out);
break;
case "doajax":
String name = req.getParameter("name");
Book book2 = bookBiz.getByName(name);
if(book2==null){
out.print("{}");//null json 对象
}else{
out.print(JSON.toJSONString(book2));
}
break;
default:
resp.sendError(404);
}
}
/**
* 修改图书信息的方法
* @param req
* @param resp
* @param out
*/
private void modify(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) throws Exception {
//1.构建一个磁盘工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//1.1 设置大小
factory.setSizeThreshold(1024*9);
//1.2 临时仓库
File file = new File("c:\\temp");
if(!file.exists()){
file.mkdir();//创建文件夹
}
factory.setRepository(file);
//2.文件上传+表单数据
ServletFileUpload fileUpload = new ServletFileUpload(factory);
//3.将请求解析成一个个FileItem(文件+表单元素)
List<FileItem> fileItems = fileUpload.parseRequest(req);
//4.遍历FileItem
Book book =new Book();
for(FileItem item: fileItems){
if(item.isFormField()){
//4.1 元素名称和用户填写的值 name: 文城
String name = item.getFieldName();
String value = item.getString("utf-8");//防止乱码
switch(name){
case "id":
book.setId(Long.parseLong(value));
break;
case "pic":
book.setPic(value);
break;
case "typeId":
book.setTypeId(Long.parseLong(value));
break;
case "name":
book.setName(value);
break;
// case "price":
// book.setPrice(Double.parseDouble(value));
// break;
case "desc":
book.setDesc(value);
break;
case "publish":
book.setPublish(value);
break;
case "author":
book.setAuthor(value);
break;
// case "stock":
// book.setStock(Long.parseLong(value));
// break;
// case "address":
// book.setAddress(value);
// break;
}
}else {
//4.2 文件: 图片的文件名 文城.png,用户不选择图片时,fileName的数据为""
String fileName = item.getName();
//避免文件替换:当前的系统时间.png
if(fileName.trim().length()>0) {
//4.2.1 获取后缀名 .png
String filterName = fileName.substring(fileName.lastIndexOf("."));
//4.2.2 修改文件名 20211117112512234.png
fileName = DateHelper.getImageName() + filterName;
//保存到哪里
//虚拟路径: Images/cover/1-1.png
//文件的读写:实际路径 D://xx --> 虚拟路径: Images/cover对应的实际路径