Java Code Examples for javax.servlet.http.HttpServletRequest.getParts()

The following are Jave code examples for showing how to use getParts() of the javax.servlet.http.HttpServletRequest class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Mastering-Java-EE-Development-with-WildFly   File: FileUploadServlet.java   View Source Code Vote up 13 votes
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	response.setContentType("text/html;charset=UTF-8");
	try (PrintWriter out = response.getWriter()) {
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<head>");
		out.println("<title>File Upload Servlet</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>File Upload Servlet</h1>");
		out.println("Receiving the uploaded file ...<br>");
		out.println("Received " + request.getParts().size() + " parts ...<br>");
		String fileName = "";
		for (Part part : request.getParts()) {
			fileName = part.getSubmittedFileName();
			out.println("... writing " + fileName + " part<br>");
			part.write(fileName);
			out.println("... written<br>");
		}
		out.println("... uploaded to: /tmp/" + fileName);
		out.println("</body>");
		out.println("</html>");
	}
}
 
Example 2
Project: lams   File: StandardMultipartHttpServletRequest.java   View Source Code Vote up 7 votes
private void parseRequest(HttpServletRequest request) {
	try {
		Collection<Part> parts = request.getParts();
		this.multipartParameterNames = new LinkedHashSet<String>(parts.size());
		MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<String, MultipartFile>(parts.size());
		for (Part part : parts) {
			String filename = extractFilename(part.getHeader(CONTENT_DISPOSITION));
			if (filename != null) {
				files.add(part.getName(), new StandardMultipartFile(part, filename));
			}
			else {
				this.multipartParameterNames.add(part.getName());
			}
		}
		setMultipartFiles(files);
	}
	catch (Exception ex) {
		throw new MultipartException("Could not parse multipart servlet request", ex);
	}
}
 
Example 3
Project: oryx2   File: AbstractOryxResource.java   View Source Code Vote up 7 votes
protected final Collection<Part> parseMultipart(HttpServletRequest request) throws OryxServingException {
  Collection<Part> parts;
  try {
    try {
      // Prefer container's standard JavaEE multipart parsing:
      parts = request.getParts();
    } catch (UnsupportedOperationException uoe) {
      // Grizzly (used in tests) doesn't support this; fall back until it does
      parts = parseMultipartWithCommonsFileUpload(request);
    }
  } catch (IOException | ServletException e) {
    throw new OryxServingException(Response.Status.BAD_REQUEST, e.getMessage());
  }
  check(!parts.isEmpty(), "No parts");
  return parts;
}
 
Example 4
Project: yacy_grid_mcp   File: RemoteAccess.java   View Source Code Vote up 6 votes
public static Map<String, byte[]> getPostMap(HttpServletRequest request) throws IOException {
    Map<String, byte[]> map = new HashMap<>();
    Map<String, String[]> pm = request.getParameterMap();
    if (pm != null && pm.size() > 0) {
        for (Map.Entry<String, String[]> entry: pm.entrySet()) {
            String[] v = entry.getValue();
            if (v != null && v.length > 0) map.put(entry.getKey(), v[0].getBytes(StandardCharsets.UTF_8));
        }
    } else try {
        request.setAttribute("org.eclipse.jetty.multipartConfig", multipartConfigElement); // without that we get a IllegalStateException in getParts()
        final byte[] b = new byte[1024];
        for (Part part: request.getParts()) {
            String name = part.getName();
            InputStream is = part.getInputStream();
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int c;
            try {while ((c = is.read(b, 0, b.length)) > 0) {
                baos.write(b, 0, c);
            }} finally {is.close();}
            map.put(name, baos.toByteArray());
        }
    } catch (IOException | ServletException | IllegalStateException e) {
        Data.logger.debug("Parsing of POST multipart failed", e);
        throw new IOException(e.getMessage());
    }
    return map;
}
 
Example 5
Project: bibliometrics   File: CSVUploadServlet.java   View Source Code Vote up 6 votes
public void doPost(MCRServletJob job) throws Exception {
    HttpServletRequest req = job.getRequest();
    Collection<Part> parts = req.getParts();
    File uploadDir = new File(inputDir + "/upload"); 
    if (!uploadDir.exists()) uploadDir.mkdirs();
    byte[] buffer = new byte[8 * 1024];
    for (Part part : parts) {
    File uploadedFile = new File(uploadDir,getFileName(part));
    
    InputStream input = part.getInputStream();
    try {
      OutputStream output = new FileOutputStream(uploadedFile);
      try {
        int bytesRead;
        while ((bytesRead = input.read(buffer)) != -1) {
          output.write(buffer, 0, bytesRead);
        }
      } finally {
        output.close();
      }
    } finally {
      input.close();
    }
    LOGGER.info("saved csv-file " + uploadedFile);
    job.getResponse().sendRedirect("getScopusAuthorData?listName="+ uploadedFile);
    }
    
}
 
Example 6
Project: lams   File: RequestParamMethodArgumentResolver.java   View Source Code Vote up 5 votes
@Override
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest) throws Exception {
	Object arg;

	HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
	MultipartHttpServletRequest multipartRequest =
		WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);

	if (MultipartFile.class.equals(parameter.getParameterType())) {
		assertIsMultipartRequest(servletRequest);
		Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?");
		arg = multipartRequest.getFile(name);
	}
	else if (isMultipartFileCollection(parameter)) {
		assertIsMultipartRequest(servletRequest);
		Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?");
		arg = multipartRequest.getFiles(name);
	}
	else if(isMultipartFileArray(parameter)) {
		assertIsMultipartRequest(servletRequest);
		Assert.notNull(multipartRequest, "Expected MultipartHttpServletRequest: is a MultipartResolver configured?");
		arg = multipartRequest.getFiles(name).toArray(new MultipartFile[0]);
	}
	else if ("javax.servlet.http.Part".equals(parameter.getParameterType().getName())) {
		assertIsMultipartRequest(servletRequest);
		arg = servletRequest.getPart(name);
	}
	else if (isPartCollection(parameter)) {
		assertIsMultipartRequest(servletRequest);
		arg = new ArrayList<Object>(servletRequest.getParts());
	}
	else if (isPartArray(parameter)) {
		assertIsMultipartRequest(servletRequest);
		arg = RequestPartResolver.resolvePart(servletRequest);
	}
	else {
		arg = null;
		if (multipartRequest != null) {
			List<MultipartFile> files = multipartRequest.getFiles(name);
			if (!files.isEmpty()) {
				arg = (files.size() == 1 ? files.get(0) : files);
			}
		}
		if (arg == null) {
			String[] paramValues = webRequest.getParameterValues(name);
			if (paramValues != null) {
				arg = paramValues.length == 1 ? paramValues[0] : paramValues;
			}
		}
	}

	return arg;
}