本文整理汇总了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);
}
}
示例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;
}
示例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;
}
示例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);
}
}
示例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;
}