當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpServletRequest.getParts方法代碼示例

本文整理匯總了Java中javax.servlet.http.HttpServletRequest.getParts方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpServletRequest.getParts方法的具體用法?Java HttpServletRequest.getParts怎麽用?Java HttpServletRequest.getParts使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.servlet.http.HttpServletRequest的用法示例。


在下文中一共展示了HttpServletRequest.getParts方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: processRequest

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
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>");
	}
}
 
開發者ID:PacktPublishing,項目名稱:Mastering-Java-EE-Development-with-WildFly,代碼行數:26,代碼來源:FileUploadServlet.java

示例2: parseRequest

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
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);
	}
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:21,代碼來源:StandardMultipartHttpServletRequest.java

示例3: parseMultipart

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
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;
}
 
開發者ID:oncewang,項目名稱:oryx2,代碼行數:17,代碼來源:AbstractOryxResource.java

示例4: getPostMap

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
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;
}
 
開發者ID:yacy,項目名稱:yacy_grid_mcp,代碼行數:28,代碼來源:RemoteAccess.java

示例5: doPost

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
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);
    }
    
}
 
開發者ID:ETspielberg,項目名稱:bibliometrics,代碼行數:29,代碼來源:CSVUploadServlet.java

示例6: resolveName

import javax.servlet.http.HttpServletRequest; //導入方法依賴的package包/類
@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;
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:54,代碼來源:RequestParamMethodArgumentResolver.java


注:本文中的javax.servlet.http.HttpServletRequest.getParts方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。