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 { } } }
时间: 2024-04-28 10:19:38 浏览: 143
这段代码用于处理 HTTP POST 请求中的文件上传。首先通过判断请求是否为 multipart/form-data 类型来确定是否需要进行文件上传处理。如果是文件上传请求,就创建一个 DiskFileItemFactory 对象,并设置上传文件的存储路径。然后创建一个 ServletFileUpload 对象,并设置上传文件的大小限制。接着调用 ServletFileUpload 对象的 parseRequest() 方法解析请求,并将解析结果存储到 items 变量中。在解析结果中,第一个元素表示上传的标题,第二个元素表示上传的图片文件,第三个元素表示上传的内容。通过调用 getString() 方法获取标题和内容的字符串值,并通过 Info.getUTFStr() 方法将其转换为 UTF-8 编码的字符串。最后,通过获取图片文件的 FileItem 对象并调用 write() 方法将其写入到指定路径中,完成文件上传操作。
相关问题
// 新增图片 if (ac.equals("imgadvaddold")) { try { String img = ""; String imgtype = ""; 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); imgtype = ((FileItem) items.get(1)).getString(); imgtype = Info.getUTFStr(imgtype); FileItem fileItem = (FileItem) items.get(0); 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 cksql = "select * from imgadv where imgtype='banner'"; ArrayList cklist = (ArrayList) dao.select(cksql); if (imgtype.equals("banner") && cklist.size() != 0) { request.setAttribute("no", ""); go("/admin/imgadvadd.jsp", request, response); } else { String sql = "insert into imgadv (filename,imgtype) " + "values('" + img + "','" + imgtype + "')"; dao.commOper(sql); request.setAttribute("suc", ""); go("/admin/imgadvadd.jsp", request, response); } } catch (Exception e1) { e1.printStackTrace(); request.setAttribute("no", ""); request.getRequestDispatcher("/admin/imgadvadd.jsp").forward( request, response); } }
这是一个Java Servlet中的一段代码,用于处理上传图片的逻辑,包括获取上传的文件、文件类型,保存到指定目录,并将文件信息插入到数据库中。其中使用了Apache Commons FileUpload库来处理文件上传。在上传图片之前,会先检查数据库中是否已经存在同类型的图片,如果存在则不允许上传。
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 页面进行显示。
阅读全文