items = upload.parseRequest(req);Iterator it = items.iterator();for (FileItem item : items) {if (item.isFormField()) {logger.error("其他类型");} else {String filename = item.getName();String path = req.getSession().getServletContext().getRealPath("/") + "\\1";File file = new File(path);if (!file.exists()) {file.mkdirs();}file = new File(path, filename);item.write(file);logger.debug(filename + "上传成功");Qiniu.uploadPic(filename,path + "\\" + filename);logger.debug("图片地址为:" + Qiniu.picUrl());logger.info(filename + "七牛云上传成功");//把文件名和url存到session中String allFilename = (String) req.getSession().getServletContext().getAttribute("allFilename");if (allFilename == null) {allFilename = "";}logger.debug("allFilename" + allFilename);allFilename = (allFilename + "," + filename);req.getSession().getServletContext().setAttribute("allFilename", allFilename);String allPicUrls = (String) req.getSession().getServletContext().getAttribute("allPicUrls");if (allPicUrls == null) {allPicUrls = "";}logger.debug("allPicUrls" + allPicUrls);allPicUrls = (allPicUrls + "," + Qiniu.picUrl());req.getSession().getServletContext().setAttribute("allPicUrls", allPicUrls);}}} catch (Exception e) {e.printStackTrace();}}} catch (Exception e) {e.printStackTrace();} finally {lock.unlock();logger.debug("解锁");}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}}
package top.sehnsucht;import org.apache.log4j.Logger;import top.sehnsucht.dao.UserPhotoDao;import top.sehnsucht.dao.impl.UserPhotoDaoImpl;import top.sehnsucht.service.UserPhotoService;import top.sehnsucht.service.impl.UserPhotoServiceImpl;import top.sehnsucht.util.DateFormatUtil;import top.sehnsucht.vo.UserInfo;import top.sehnsucht.vo.UserPhoto;import javax.servlet.*;import javax.servlet.http.*;import java.io.IOException;import java.util.ArrayList;public class JDBCServlet extends HttpServlet {private Logger logger = Logger.getLogger(this.getClass());@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {logger.debug(req.getSession().getServletContext().getAttribute("allFilename"));logger.debug(req.getSession().getServletContext().getAttribute("allPicUrls"));logger.debug(req.getParameter("username"));logger.debug(req.getParameter("birthday"));String allFilename = req.getSession().getServletContext().getAttribute("allFilename").toString();String[] splitFilename = allFilename.split(",");String allPicUrls = req.getSession().getServletContext().getAttribute("allPicUrls").toString();String[] splitPicUrls = allPicUrls.split(",");UserPhotoDao userPhotoDao = new UserPhotoDaoImpl();UserPhotoService userPhotoService = new UserPhotoServiceImpl(userPhotoDao);UserInfo userInfo = new UserInfo(req.getParameter("username"), DateFormatUtil.tran(req.getParameter("birthday")));Integer id = userPhotoService.addUser(userInfo);ArrayList userPhotos = new ArrayList<>();for (int i = 1; i < splitFilename.length; i++) {UserPhoto userPhoto = new UserPhoto(id, splitFilename[i], splitPicUrls[i]);userPhotos.add(userPhoto);}userPhotoService.addUserAndPhoto(userPhotos);String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort() + "/list.jsp";resp.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);resp.setHeader("Location", strBackUrl);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}}
list.jsp
列表="${pageContext.request.contextPath}/layui/layui.js">="${pageContext.request.contextPath}/js/jquery-3.6.0.js">