当前位置: 首页>>代码示例>>Java>>正文


Java ServletFileUpload.getItemIterator方法代码示例

本文整理汇总了Java中org.apache.commons.fileupload.servlet.ServletFileUpload.getItemIterator方法的典型用法代码示例。如果您正苦于以下问题:Java ServletFileUpload.getItemIterator方法的具体用法?Java ServletFileUpload.getItemIterator怎么用?Java ServletFileUpload.getItemIterator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.commons.fileupload.servlet.ServletFileUpload的用法示例。


在下文中一共展示了ServletFileUpload.getItemIterator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getRequestData

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
private Map<String, String> getRequestData(HttpServletRequest request) {
  Map<String, String> requestData = new HashMap<>();

  ServletFileUpload upload = new ServletFileUpload();
  try {
    FileItemIterator itemIterator = upload.getItemIterator(request);
    while (itemIterator.hasNext()) {
      FileItemStream item = itemIterator.next();
      InputStream itemStream = item.openStream();
      String value = Streams.asString(itemStream, CharEncoding.UTF_8);
      requestData.put(item.getFieldName(), value);
    }
  } catch (FileUploadException | IOException e) {
    LOGGER.error("Failed to process request", e);
  }

  return requestData;
}
 
开发者ID:Cognifide,项目名称:aet,代码行数:19,代码来源:SuiteServlet.java

示例2: blockingIoMultipart

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
/**
 * <p> Example of parsing the multipart request using commons file upload. In this case the parsing happens in blocking io.
 *
 * @param request The {@code HttpServletRequest}
 * @return The {@code VerificationItems}
 * @throws Exception if an exception happens during the parsing
 */
@RequestMapping(value = "/blockingio/fileupload/multipart", method = RequestMethod.POST)
public @ResponseBody VerificationItems blockingIoMultipart(final HttpServletRequest request) throws Exception {

    assertRequestIsMultipart(request);

    final ServletFileUpload servletFileUpload = new ServletFileUpload();
    final FileItemIterator fileItemIterator = servletFileUpload.getItemIterator(request);

    final VerificationItems verificationItems = new VerificationItems();
    Metadata metadata = null;
    while (fileItemIterator.hasNext()){
        FileItemStream fileItemStream = fileItemIterator.next();
        if (METADATA_FIELD_NAME.equals(fileItemStream.getFieldName())){
            if (metadata != null){
                throw new IllegalStateException("Found more than one metadata field");
            }
            metadata = unmarshalMetadata(fileItemStream.openStream());
        }else {
            VerificationItem verificationItem = buildVerificationItem(fileItemStream.openStream(), fileItemStream.getFieldName(), fileItemStream.isFormField());
            verificationItems.getVerificationItems().add(verificationItem);
        }
    }
    processVerificationItems(verificationItems, metadata, false, request.getHeader(VERIFICATION_CONTROL_HEADER_NAME));
    return verificationItems;
}
 
开发者ID:synchronoss,项目名称:nio-multipart,代码行数:33,代码来源:MultipartController.java

示例3: MultipartUploadReader

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
/**
 * Creates a new Reader for reading the given HTTP request. This must be a multipart request.
 * @param pFoxRequest
 */
public MultipartUploadReader(FoxRequest pFoxRequest) {
  try {
    // New file upload handler
    mServletFileUploadUpload = new ServletFileUpload();

    // Create a progress listener for this upload
    mFiletransferProgressListener = new FiletransferProgressListener();

    // Attach the progress listener to this upload
    mServletFileUploadUpload.setProgressListener(mFiletransferProgressListener);
    //mUploadInfo.setProgressListener(mFiletransferProgressListener);

    // Parse the upload request
    mItemIterator = mServletFileUploadUpload.getItemIterator(pFoxRequest.getHttpRequest());
  }
  catch (Throwable ex) {
    throw new ExInternal("Error encountered while trying to initialise a file upload work item.\nOriginal error: " + ex.getMessage());
  }
}
 
开发者ID:Fivium,项目名称:FOXopen,代码行数:24,代码来源:MultipartUploadReader.java

示例4: getInputStreamFromRequest

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
private InputStream getInputStreamFromRequest(HttpServletRequest request) {
	InputStream inputStream=null;
	DiskFileItemFactory dff = new DiskFileItemFactory();
	try {
		ServletFileUpload sfu = new ServletFileUpload(dff);
		FileItemIterator fii = sfu.getItemIterator(request);
		while (fii.hasNext()) {
			FileItemStream item = fii.next();
			// 普通参数存储
			if (!item.isFormField()) {
				// 只保留一个
				if (inputStream == null) {
					inputStream = item.openStream();
					return inputStream;
				}
			} 
		}
	} catch (Exception e) {
	}
	return inputStream;
}
 
开发者ID:huanzhou,项目名称:jeecms6,代码行数:22,代码来源:SnapScreenServlet.java

示例5: uploadFiles

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
private List<FileEntry> uploadFiles(HttpServletRequest request, String spaceGuid)
    throws FileUploadException, IOException, FileStorageException, SLException {
    ServletFileUpload upload = getFileUploadServlet();
    long maxUploadSize = getConfiguration().getMaxUploadSize();
    upload.setSizeMax(maxUploadSize);

    List<FileEntry> uploadedFiles = new ArrayList<FileEntry>();
    FileItemIterator fileItemIterator = null;
    try {
        fileItemIterator = upload.getItemIterator(request);
    } catch (SizeLimitExceededException ex) {
        throw new SLException(MessageFormat.format(Messages.MAX_UPLOAD_SIZE_EXCEEDED, maxUploadSize));
    }
    while (fileItemIterator.hasNext()) {
        FileItemStream item = fileItemIterator.next();
        if (item.isFormField()) {
            continue; // ignore simple (non-file) form fields
        }

        InputStream in = null;
        try {
            in = item.openStream();
            FileEntry entry = getFileService().addFile(spaceGuid, item.getName(),
                getConfiguration().getFileUploadProcessor(), in);
            uploadedFiles.add(entry);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
    return uploadedFiles;
}
 
开发者ID:SAP,项目名称:cf-mta-deploy-service,代码行数:32,代码来源:FilesApiServiceImpl.java

示例6: getRequestStream

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
private InputStream getRequestStream(HttpServletRequest req, String expectedFieldName)
      throws Exception {
    ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator iterator = upload.getItemIterator(req);
    while (iterator.hasNext()) {
      FileItemStream item = iterator.next();
//      LOG.info(item.getContentType());
      if (item.getFieldName().equals(expectedFieldName)) {
        return item.openStream();
      }
    }
    throw new IllegalArgumentException("Field " + expectedFieldName + " not found in upload");
  }
 
开发者ID:mit-cml,项目名称:appinventor-extensions,代码行数:14,代码来源:GalleryServlet.java

示例7: getRequestStream

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
private InputStream getRequestStream(HttpServletRequest req, String expectedFieldName)
    throws Exception {
  ServletFileUpload upload = new ServletFileUpload();
  FileItemIterator iterator = upload.getItemIterator(req);
  while (iterator.hasNext()) {
    FileItemStream item = iterator.next();
    if (item.getFieldName().equals(expectedFieldName)) {
      return item.openStream();
    }
  }

  throw new IllegalArgumentException("Field " + expectedFieldName + " not found in upload");
}
 
开发者ID:mit-cml,项目名称:appinventor-extensions,代码行数:14,代码来源:UploadServlet.java

示例8: parseMultipartParameters

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
/**
 *
 * @param req
 * @return
 * @throws IOException
 */
private static Map<String, String[]> parseMultipartParameters(HttpServletRequest req) throws IOException {
    if (isMultipartContent(req)) {
        Map<String, String[]> multipartParameters = new HashMap();
        Map<String, List<String>> map = new HashMap();
        try {
            ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator iter = upload.getItemIterator(req);
            req.setAttribute(REQ_ATT_MULTIPART_ITERATOR, iter);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                if (!item.isFormField()) {
                    req.setAttribute(REQ_ATT_MULTIPART_CURRENT_ITEM, item);
                    break;
                }
                List<String> list = map.get(item.getFieldName());
                if (list == null) {
                    list = new ArrayList();
                    map.put(item.getFieldName(), list);
                }
                String encoding = req.getCharacterEncoding();
                if (encoding == null) {
                    encoding = "UTF-8";
                }
                list.add(Miscellaneous.toString(item.openStream(), encoding));
            }
        } catch (FileUploadException ex) {
            throw new RuntimeException(ex);
        }
        for (Map.Entry<String, List<String>> entrySet : map.entrySet()) {
            String key = entrySet.getKey();
            List<String> value = entrySet.getValue();
            multipartParameters.put(key, value.toArray(new String[value.size()]));
        }
        return multipartParameters;
    }
    return null;
}
 
开发者ID:brutusin,项目名称:Brutusin-RPC,代码行数:44,代码来源:RpcServlet.java

示例9: getFileItem

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
private void getFileItem(HttpServletRequest request) throws FileUploadException, IOException {
	boolean isMultipart = ServletFileUpload.isMultipartContent(request);
	
	if (!isMultipart) {
		throw new IllegalArgumentException("Not multipart...");
	}
	
	
		ServletFileUpload upload = new ServletFileUpload();
		
		List<String> mdrEntries = new ArrayList<String>();
		
		// Parse the request
		FileItemIterator iter = upload.getItemIterator(request);
		while (iter.hasNext()) {
			FileItemStream item = iter.next();
			
			String name = item.getFieldName();
			InputStream stream = item.openStream();
			if (item.isFormField()) {
				System.out.println("Form field " + name + " with value "
						+ Streams.asString(stream) + " detected.");
			} else {
				System.out.println("File field " + name + " with file name "
						+ item.getName() + " detected.");
				// Process the input stream
			}
			String mdrEntry = handleInput(name, stream);
			mdrEntries.add(mdrEntry);
		}
		commitContent(mdrEntries);
}
 
开发者ID:luox12,项目名称:onecmdb,代码行数:33,代码来源:ChangeUploadCommand.java

示例10: doPost

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
protected void doPost(HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException {
	long gameId = 0l;
	String auth = null;
	try {
		ServletFileUpload upload = new ServletFileUpload();
		FileItemIterator iter = upload.getItemIterator(req);

		String json = "";
		while (iter.hasNext()) {
			FileItemStream item = iter.next();
			String name = item.getFieldName();
			InputStream stream = item.openStream();
			if (item.isFormField()) {
				String value = Streams.asString(stream);
				if ("gameId".equals(name)) gameId = Long.parseLong(value);
				if ("auth".equals(name)) auth = value;
				
			} else {
				json = Streams.asString(stream);

			}
		}

		res.setContentType("text/plain");
		JSONObject jObject = new JSONObject(json);
		Object deserialized = JsonBeanDeserializer.deserialize(json);
		
		if (deserialized instanceof GamePackage && ((GamePackage) deserialized).getGame() != null)
			unpackGame((GamePackage) deserialized, req, auth);
		if (deserialized instanceof RunPackage && ((RunPackage) deserialized ).getRun() != null)
			unpackRun((RunPackage) deserialized, req, gameId, auth);

	} catch (Exception ex) {
		throw new ServletException(ex);
	}
}
 
开发者ID:WELTEN,项目名称:dojo-ibl,代码行数:38,代码来源:UploadGameServlet.java

示例11: doPost

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        try {
            ServletFileUpload upload = new ServletFileUpload();
            res.setContentType("text/html");
            res.setCharacterEncoding("UTF-8");
            FileItemIterator iterator = upload.getItemIterator(req);
            while (iterator.hasNext()) {
                FileItemStream item = iterator.next();
                InputStream stream = item.openStream();
                if (item.isFormField()) {
                    log.warning("Got a form field: " + item.getFieldName());
                } else {
                    log.warning("Got an uploaded file: " + item.getFieldName()+ ", name = " + item.getName());

                    // You now have the filename (item.getName() and the
                    // contents (which you can read from stream). Here we just
                    // print them back out to the servlet output stream, but you
                    // will probably want to do something more interesting (for
                    // example, wrap them in a Blob and commit them to the
                    // datastore).
/*                    int len;
                    byte[] buffer = new byte[8192];
                    while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
                        res.getOutputStream().write(buffer, 0, len);
                    }*/
                    String content = IOUtils.toString(stream, "UTF-8");
                    tbx2rdfServlet.formatOutput(res,content);

                }
            }
        } catch (Exception ex) {
            //throw new ServletException(ex);
        }
    }
 
开发者ID:cimiano,项目名称:tbx2rdf,代码行数:35,代码来源:FileUploadServlet.java

示例12: uploadMultipart

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
private void uploadMultipart(HttpServletRequest req, HttpServletResponse resp,
    OutputStream repoItemOutputStrem) throws IOException {

  log.debug("Multipart detected");

  ServletFileUpload upload = new ServletFileUpload();

  try {

    // Parse the request
    FileItemIterator iter = upload.getItemIterator(req);
    while (iter.hasNext()) {
      FileItemStream item = iter.next();
      String name = item.getFieldName();
      try (InputStream stream = item.openStream()) {
        if (item.isFormField()) {
          // TODO What to do with this?
          log.debug("Form field {} with value {} detected.", name, Streams.asString(stream));
        } else {

          // TODO Must we support multiple files uploading?
          log.debug("File field {} with file name detected.", name, item.getName());

          log.debug("Start to receive bytes (estimated bytes)",
              Integer.toString(req.getContentLength()));
          int bytes = IOUtils.copy(stream, repoItemOutputStrem);
          resp.setStatus(SC_OK);
          log.debug("Bytes received: {}", Integer.toString(bytes));
        }
      }
    }

  } catch (FileUploadException e) {
    throw new IOException(e);
  }
}
 
开发者ID:Kurento,项目名称:kurento-java,代码行数:37,代码来源:RepositoryHttpServlet.java

示例13: asMultipartRequest

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
private static MultipartRequest asMultipartRequest(HttpServletRequest request) throws Exception {
    String encoding = request.getCharacterEncoding();

    MultipartRequest req = new MultipartRequest(request);
    ServletFileUpload upload = new ServletFileUpload();
    upload.setHeaderEncoding(encoding);
    FileItemIterator it = upload.getItemIterator(request);

    while (it.hasNext()) {
        FileItemStream item = it.next();
        String fieldName = item.getFieldName();
        InputStream stream = item.openStream();
        try {
            if (item.isFormField()) {
                req.setParameter(fieldName, Streams.asString(stream, encoding));
            } else {
                String originalFilename = item.getName();
                File diskFile = getTempFile(originalFilename);
                OutputStream fos = new FileOutputStream(diskFile);

                try {
                    IoUtils.copy(stream, fos);
                } finally {
                    IoUtils.closeQuietly(fos);
                }

                FilePart filePart = new FilePart(fieldName, originalFilename, diskFile);
                req.addFile(filePart);
            }
        } finally {
            IoUtils.closeQuietly(stream);
        }
    }

    return req;
}
 
开发者ID:subchen,项目名称:jetbrick-all-1x,代码行数:37,代码来源:FileUploaderUtils.java

示例14: service

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {
        ServletFileUpload upload = new ServletFileUpload();
        res.setContentType("text/plain");

        FileItemIterator iterator = upload.getItemIterator(req);
        while (iterator.hasNext()) {
            FileItemStream item = iterator.next();
            InputStream stream = item.openStream();

            if (item.isFormField()) {
                logger.info("Got a form field: " + item.getFieldName());
            } else {
                logger.info("Got an uploaded file: " + item.getFieldName() +
                        ", name = " + item.getName());
                AdressValidationService adressValidationService = new AdressValidationService();
                List<Optician> opticians = readOpticians(stream);
                for (Optician optician : opticians) {
                    optician = adressValidationService.validateAdress(optician);
                    optician.setName(Optician.beautifyName(optician.getName()));
                    try {
                        logger.info("wait...");
                        Thread.sleep(1000); // sleep to get no trouble with service quota
                    } catch (InterruptedException ie) {
                        logger.error("uh... unexpected.", ie);
                    }
                    OfyService.ofy().save().entity(optician).now();
                }
            }
        }
    } catch (Exception ex) {
        throw new ServletException(ex);
    }
}
 
开发者ID:mpoehler,项目名称:optiker-verzeichnis,代码行数:36,代码来源:FileUploadServlet.java

示例15: doPost

import org.apache.commons.fileupload.servlet.ServletFileUpload; //导入方法依赖的package包/类
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServletFileUpload upload = new ServletFileUpload();

    try{
        FileItemIterator iterator = upload.getItemIterator(request);
        if (iterator.hasNext()) {
            FileItemStream item = iterator.next();
            String name = item.getFieldName();

            logger.debug("Uploading file '{}' with item name '{}'", item.getName(), name);

            InputStream stream = item.openStream();

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            Streams.copy(stream, out, true);

            byte[] data = out.toByteArray();

            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType("text/html");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(new String(data));
            response.flushBuffer();
        }
        else {
            logger.error("No file found in post request!");
            throw new RuntimeException("No file found in post request!");
        }
    }
    catch(Exception e){
        logger.error("Unexpected error in FileUploadServlet.doPost: ", e);
        throw new RuntimeException(e);
    }
}
 
开发者ID:kaaproject,项目名称:avro-ui,代码行数:36,代码来源:FileUploadServlet.java


注:本文中的org.apache.commons.fileupload.servlet.ServletFileUpload.getItemIterator方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。