把file标签与其他标签分开只处理file标签并把其他参数重定向到另外的函数或servlet
class="java" name="code">
public class D_BookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private D_BookService d_BookService = new D_BookServiceImpl();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
System.out.println(method);
if ("add".equals(method)) {
upLoadFile(request, response);
}else if("addbook".equals(method)){
addBook(request, response);
}else if("edit".equals(method)){
showBook(request, response);
}
}
//添加图书
private void addBook(HttpServletRequest request, HttpServletResponse response) throws IOException{
D_Book d_Book = new D_Book();
String product_pic = request.getParameter("product_pic");
String product_name = request.getParameter("product_name"); // varchar(100) NOT NULL,-- 商品名称
String fixed_price = request.getParameter("fixed_price"); // double NOT NULL,-- 商品定价
String dang_price = request.getParameter("dang_price"); //double NOT NULL,-- 当当价
String author = request.getParameter("author") ; //varchar(200) NOT NULL,-- 作者
String publishing = request.getParameter("publishing"); //varchar(200) NOT NULL,-- 出版社
String publish_time = request.getParameter("publish_time"); //date NOT NULL,-- 出版时间
String author_summary= request.getParameter("author_summary"); // varchar(100) NOT NULL,-- 作者简介
String description = request.getParameter("description"); //varchar(100) default NULL,-- 商品描述
d_Book.setProduct_pic(product_pic);
d_Book.setProduct_name(product_name);
d_Book.setDang_price(Double.parseDouble(dang_price));
d_Book.setFixed_price(Double.parseDouble(fixed_price));
d_Book.setAuthor(author);
d_Book.setPublishing(publishing);
d_Book.setPublish_time(Date.valueOf(publish_time));
d_Book.setAuthor_summary(author_summary);
d_Book.setDescription(description);
d_Book.setScore(1);
d_BookService.insertD_Book(d_Book);
response.sendRedirect("/DangDang/Admin/jspfile/editbook.jsp");
}
//上传文件
@SuppressWarnings("unchecked")
public void upLoadFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// 在解析请求之前先判断请求类型是否为文件上传类型
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
String url = "";
if(isMultipart){
// 文件上传处理工厂
FileItemFactory fileItemFactory = new DiskFileItemFactory();
// 创建文件上传处理器
ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
// 开始解析请求信息
try {
List<FileItem> fileItems = servletFileUpload.parseRequest(request);
// 对所有请求信息进行判断
for(FileItem fileItem: fileItems){
//如果提交信息为表单格式设置为新的url
if (fileItem.isFormField()) {
String fieldName = fileItem.getFieldName();
String value = fileItem.getString();
url += "&" + fieldName + "=" + value;
}
//如果提交信息为文件格式
else {
String fileName = fileItem.getName();
String basePath = System.getProperty("user.dir").replace("\\", "/") + "/../webapps/DangDang/BookImage/";
File file = new File(basePath, fileName);
if (!file.exists()) {
file.createNewFile();
}
fileItem.write(file);
url += "&product_pic" + "=" + "BookImage/" + fileName;
}
}
} catch (FileUploadException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//把设置好的url重新转发到servlet或函数
response.sendRedirect("/DangDang/admindbook?method=addbook" + url);
}
}
}