if (ac.equals("newsadd")) { try { String title = ""; String img = ""; String note = ""; request.setCharacterEncoding("utf-8"); RequestContext requestContext = new ServletRequestContext( request); if (FileUpload.isMultipartContent(requestContext)) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setRepository(new File(request .getRealPath("/upfile/") + "/")); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(100 * 1024 * 1024); List items = new ArrayList(); items = upload.parseRequest(request); title = ((FileItem) items.get(0)).getString(); title = Info.getUTFStr(title); note = ((FileItem) items.get(2)).getString(); note = Info.getUTFStr(note); FileItem fileItem = (FileItem) items.get(1); if (fileItem.getName() != null && fileItem.getSize() != 0) { if (fileItem.getName() != null && fileItem.getSize() != 0) { File fullFile = new File(fileItem.getName()); img = Info.generalFileName(fullFile.getName()); File newFile = new File( request.getRealPath("/upfile/") + "/" + img); try { fileItem.write(newFile); } catch (Exception e) { e.printStackTrace(); } } else { } } } String sql = "insert into news (title,img,note,savetime,type) " + "values('" + title + "','" + img + "','" + note + "','" + Info.getDateStr() + "','新闻')"; dao.commOper(sql); request.setAttribute("suc", "操作成功!"); go("/admin/newslist.jsp", request, response); } catch (Exception e1) { e1.printStackTrace(); request.setAttribute("error", ""); request.getRequestDispatcher("/admin/newsadd.jsp").forward( request, response); } }
时间: 2024-04-28 13:19:34 浏览: 11
这段代码是一个 Servlet 中的 doPost() 方法,当请求的参数 ac 的值为 "newsadd" 时,执行以下操作:
1. 获取请求中上传的文件(标题、图片、内容)。
2. 将获取到的标题和内容进行编码处理,以处理中文字符乱码的问题。
3. 将上传的图片保存到指定目录下,并将图片名称保存到 img 变量中。
4. 构造 SQL 语句,将标题、图片、内容、保存时间和新闻类型插入到数据库中。
5. 将操作成功的提示信息设置到请求属性中,然后将请求重定向到新闻列表页面。
如果执行过程中发生异常,则将异常信息设置到请求属性中,并将请求转发到新闻添加页面。这个 Servlet 主要用于实现后台管理员添加新闻的功能。当管理员在页面上输入新闻的标题、图片和内容,并提交表单时,该 Servlet 将获取到的数据插入到数据库中,并在页面上显示操作成功的提示信息。
相关问题
string != null && !string.equals("")
This is a condition that evaluates to true if the string is not null. In other words, it checks whether the string variable has a value assigned to it or not. If the string is null, the condition would evaluate to false.
if (ac.equals("newsedit")) { String id = request.getParameter("id"); HashMap map = dao.select("select * from news where id=" + id) .get(0); try { String title = ""; String note = ""; String img = map.get("img").toString(); request.setCharacterEncoding("utf-8"); RequestContext requestContext = new ServletRequestContext( request); if (FileUpload.isMultipartContent(requestContext)) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setRepository(new File(request .getRealPath("/upfile/") + "/")); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(100 * 1024 * 1024); List items = new ArrayList(); items = upload.parseRequest(request); title = ((FileItem) items.get(0)).getString(); title = Info.getUTFStr(title); note = ((FileItem) items.get(2)).getString(); note = Info.getUTFStr(note); FileItem fileItem = (FileItem) items.get(1); if (fileItem.getName() != null && fileItem.getSize() != 0) { if (fileItem.getName() != null && fileItem.getSize() != 0) { File fullFile = new File(fileItem.getName()); img = Info.generalFileName(fullFile.getName()); File newFile = new File( request.getRealPath("/upfile/") + "/" + img); try { fileItem.write(newFile); } catch (Exception e) { e.printStackTrace(); } } else { } } } String sql = "update news set title='" + title + "',note='" + note + "',img='" + img + "' where id=" + id; dao.commOper(sql); request.setAttribute("suc", "操作成功!"); go("/admin/newslist.jsp?id=" + id, request, response); } catch (Exception e1) { e1.printStackTrace(); request.setAttribute("error", ""); request.getRequestDispatcher("/admin/newsedit.jsp?id=" + id) .forward(request, response); } }
这是一个针对新闻编辑的请求处理代码块。首先,通过获取请求参数中的 id 值,从数据库中获取到对应的新闻信息。然后,通过判断请求是否包含文件上传的方式,来决定是否需要对新闻的标题、摘要和图片进行更新。如果请求中包含了上传文件的信息,则通过解析文件并将其保存到服务器上,来更新新闻的图片。最后,将新闻的标题、摘要和图片信息更新到数据库中,将操作成功的提示信息设置到当前请求的属性中,并将请求重定向到 "/admin/newslist.jsp?id=" + id 页面进行显示。如果在处理过程中出现异常,则将错误信息设置到当前请求的属性中,并将请求转发到 "/admin/newsedit.jsp?id=" + id 页面进行显示。