static String readContent(Part part) throws IOException { InputStream is = part.getInputStream(); final int n = is.available(); byte [] bytes = new byte[n]; // Allocate room is.read(bytes, 0, n); return new String(bytes, "UTF-8"); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { for (final Part part : request.getParts()) { if (part.getContentType() == null) continue; String content = readContent(part); System.out.println("content=" + content); } }
Programming Tips - Java: In a Servlet, how to read the content from a Part
Date: 2014aug26
Update: 2025oct9
Language: Java
Platform: Servlet
Q. Java: In a Servlet, how to read the content from a Part
A. Here's method for that: