jsp连mysql存图片_JSP实现图片上传并保存到数据库

本文介绍了一个基于Servlet 3.0及以上版本的文件上传处理示例,展示了如何使用MultipartConfig注解和HttpServletRequest的getPart方法来接收并处理上传的图片文件,包括文件类型验证、文件名处理及保存等关键步骤。
摘要由CSDN通过智能技术生成

c1a3af5b0e5b6b792b51dea37a561a9c.png

慕函数4003404

servlet里面有一个request.getPart()方法,通过这个文件可以获得图片,前提是你的servlet版本必须是3.0以上+tomcat7,具体参考以下@WebServlet("/articleManage")@MultipartConfig(maxFileSize = 1024 * 1024 * 10)// 最大10MBpublic class ArticleManage extends HttpServlet {private static final long serialVersionUID = 1L;public ArticleManage() {super();}protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String method = request.getParameter("method");if (method != null) {if (method.equals("add")) {addArticle(request, response);}}private void addArticle(HttpServletRequest request,HttpServletResponse response) {ArticleService service = new ArticleServiceImpl();ArticleTypeService typeService=new ArticleTypeServiceImpl();String content = null;String email = null;String tag = null;String type=null;try {email = request.getParameter("email");content = request.getParameter("content");tag = request.getParameter("tag");type=request.getParameter("type");Part img = request.getPart("img");String imgName = null;if (img != null) {// 设置文件路径,写到硬盘String head = img.getHeader("content-disposition");int index = head.lastIndexOf("=") + 2;imgName = head.substring(index, head.length() - 1); // 上传文件时的文件名imgName.lastIndexOf(".");String suffix = imgName.substring(imgName.lastIndexOf(".")); // 文件后缀if (!suffix.equals(".jpeg") && !suffix.equals(".jpg")&& !suffix.equals(".png") && !suffix.equals(".gif")&& !suffix.equals(".bmp")) {System.out.println("innn******************");// 非法文件request.setAttribute("content", content);request.setAttribute("email", email);request.setAttribute("tag", tag);request.setAttribute("type", type);request.setAttribute("errorinfo","*您上传的文件不合法,只能上后缀为jpg,bmp,png,gif,jpeg的图片");request.getRequestDispatcher("add.jsp").forward(request,response); // 重新导航到表单页return;}imgName = System.currentTimeMillis() + suffix;img.write(this.getServletContext().getRealPath("/image/upload")+ File.separator + imgName); // 写到硬盘}Article msg = new Article();msg.setContent(content);msg.setImg("image/upload/" + imgName);msg.setEmail(email);msg.setKeyWord(tag);msg.setType(typeService.querySingle(Integer.parseInt(type)));// 从session中取UserUser user = (User) request.getSession().getAttribute("user");msg.setUser(user);service.addMsg(msg);response.setCharacterEncoding("utf-8");response.setContentType("text/html");response.getWriter().write("

投稿成功两秒后跳转到首页...");response.setHeader("refresh", "2;url=index.jsp");} catch (Exception e) {e.printStackTrace();request.setAttribute("content", content);request.setAttribute("email", email);request.setAttribute("tag", tag);request.setAttribute("type", type);request.setAttribute("errorinfo", "投稿失败,请检查上传文件的大小,不能大于10MB");try {request.getRequestDispatcher("add.jsp").forward(request,response);} catch (ServletException | IOException e1) {e1.printStackTrace();}return;} finally {try {service.closeConnResources();} catch (SQLException e) {e.printStackTrace();}}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值