package com.bian.servlet;
import com.bian.biz.PhotoBiz;
import com.bian.dao.LeiBieDAO;
import com.bian.pojo.*;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
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.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class PhotoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String action = request.getParameter("action");
PhotoBiz photoBiz = PhotoBiz.getInstance();
int pageSize = 6; //将页面显示的图片数量设置为域变量
if("photoAdd".equals(action)) { //当点击添加图片链接时
List<Leibie> categoryList = null;
try {
categoryList = new LeiBieDAO().getAllCategories();
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("categoryList", categoryList);
request.getRequestDispatcher("photoAdd.jsp").forward(request, response);
//当点击表单项:上传 时
} else if("add".equals(action)) {
Boolean flag = false;
Calendar calendar = Calendar.getInstance();
String newFileName = String.valueOf(calendar.getTimeInMillis());
FileItemFactory factory = new DiskFileItemFactory(); //创建一个文件项工厂
ServletFileUpload upload = new ServletFileUpload(factory); //create a new file upload handler
List items=null;
Photo photo = new Photo();
try {
items = upload.parseRequest(request); //将请求解析(请求包含该表单中所有的parameter,按参数传递过来的顺序)
//process the uploaded items
Iterator iter = items.iterator();
while(iter.hasNext()) {
FileItem item = (FileItem)iter.next();
if(item.isFormField()) { //是表单项时
String fieldName = item.getFieldName();
//因为直接用item.getString()获取的变量是乱码,可以用下面这种方式来转换编码
String fieldValue = new String(item.get(),"utf-8");
if(fieldName.equals("name")) {
photo.setName(fieldValue);
} else if(fieldName.equals("category")) {
Leibie c = new Leibie();
c.setId(Integer.parseInt(fieldValue));
photo.setCategory(c); //因为在Photo类中存储的是category对象,而不只是类别id
} else if(fieldName.equals("description")) {
photo.setDescription(fieldValue);
}
} else { //不是表单项时
/*if("file1".equals(item.getFieldName())) {
}*/
String fieldName = item.getFieldName();
String fileName = item.getName(); //因为是文件类型的参数,所以用getName()
String ext = fileName.substring(fileName.lastIndexOf('.')+1); //取出要上传的文件的路径中的文件的扩展名
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
String saveurl = request.getSession().getServletContext().getRealPath("/") + "uploading\\";
System.out.print("jjjjjjjjjjjjjjj"+saveurl);
String newFileNameWithExt = newFileName + "." + ext; //新的文件名字(用文件上传的时间.ext作为新的名字,这样就不会重名)
File uploadedFile = new File(saveurl + newFileNameWithExt); //文件的路径
System.out.print("sssssssssssssssssssssss"+uploadedFile);
try {
item.write(uploadedFile); //A convenience method to write an uploaded item to disk
} catch (Exception e) {
e.printStackTrace();
}
// String photoPath = request.getContextPath() + "/uploading/" + newFileNameWithExt;
String photoPath = newFileNameWithExt;
photo.setPath(photoPath);
photo.setContentTime(newFileName);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
//以上就生成了一个photo对象,下面将该对象存储到DB中
String msg = "";
try {
msg = photoBiz.uploadPhoto(photo);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("msg", msg);
request.getRequestDispatcher("admin/result.jsp").forward(request, response);
} /*else if("photoList".equals(action)) { //未进行分页时的跳转
String strId = request.getParameter("id");
int id = Integer.parseInt(strId);
List<Photo> photoList = null;
try {
photoList = photoBiz.getPhotoByCategoryId(id);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("photoList", photoList);
request.getRequestDispatcher("admin/photoList.jsp").forward(request, response);
} */else if("photoListByPage".equals(action)) { //将图片进行分页显示
int categoryId = Integer.parseInt(request.getParameter("categoryId"));
int pageNo = Integer.parseInt(request.getParameter("pageNo"));
Pager pager = null;
try {
pager = photoBiz.getPhotosByCIdandPageNo(categoryId, pageSize, pageNo);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("pager", pager);
request.setAttribute("categoryId", categoryId);
request.getRequestDispatcher("admin/photoList.jsp").forward(request, response);
} else if("GoTo".equals(action)) { //根据传入的页码进行显示
int categoryId = Integer.parseInt(request.getParameter("categoryId"));
int pageNo = Integer.parseInt(request.getParameter("pageNo"));
Pager pager = null;
try {
pager = photoBiz.getPhotosByCIdandPageNo(categoryId, pageSize, pageNo);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("categoryId", categoryId); //也应该将categoryId传递进去,这里出错了
request.setAttribute("pager", pager);
request.getRequestDispatcher("admin/photoList.jsp").forward(request, response);
} else if("browsePhoto".equals(action)) { //点击浏览大图,及图片相关信息
int id = Integer.parseInt(request.getParameter("id"));
Photo photo = null;
List<Pinglun> pcList = new ArrayList<Pinglun>(); //存放该图片的所有评论集合
int total = 0; //存放该图片的评论条数
try {
photo = photoBiz.getPhotoById(id);
total = photoBiz.getPhotoCommentListByPid(id, pcList);
} catch(SQLException e) {
e.printStackTrace();
}
request.setAttribute("photo", photo);
request.setAttribute("total", total);
request.setAttribute("pcList", pcList);
request.getRequestDispatcher("admin/photoBrowse.jsp").forward(request, response);
//response.sendRedirect("admin/photoBrowse.jsp");
} else if("delete".equals(action)) { //删除图片操作
int id = Integer.parseInt(request.getParameter("id"));
String msg = "";
try {
msg = photoBiz.deletePhotoById(id);
} catch(SQLException e) {
e.printStackTrace();
}
request.setAttribute("msg", msg);
request.getRequestDispatcher("admin/result.jsp").forward(request, response);
} else if("modify1".equals(action)) { //修改图片信息
int id = Integer.parseInt(request.getParameter("id"));
Photo p = null;
try {
p = photoBiz.getPhotoById(id);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("photo", p);
request.getRequestDispatcher("admin/photoModify.jsp").forward(request, response);
} else if("modify2".equals(action)) { //修改图片信息操作
int id = Integer.parseInt(request.getParameter("id"));
String name = request
- 1
- 2
- 3
- 4
前往页