本文整理汇总了Java中org.apache.commons.fileupload.FileItemIterator类的典型用法代码示例。如果您正苦于以下问题:Java FileItemIterator类的具体用法?Java FileItemIterator怎么用?Java FileItemIterator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FileItemIterator类属于org.apache.commons.fileupload包,在下文中一共展示了FileItemIterator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getRequestData
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的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;
}
示例2: blockingIoMultipart
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的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;
}
示例3: dumpFileIterator
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的package包/类
void dumpFileIterator(final FileItemIterator fileItemIterator){
int partIndex = 0;
try {
log.info("-- COMMONS FILE UPLOAD --");
while (fileItemIterator.hasNext()) {
log.info("-- Part " + partIndex++);
FileItemStream fileItemStream = fileItemIterator.next();
FileItemHeaders fileItemHeaders = fileItemStream.getHeaders();
Iterator<String> headerNames = fileItemHeaders.getHeaderNames();
while(headerNames.hasNext()){
String headerName = headerNames.next();
log.info("Header: " + headerName+ ": " + Joiner.on(',').join(fileItemHeaders.getHeaders(headerName)));
}
log.info("Body:\n" + IOUtils.toString(fileItemStream.openStream()));
}
log.info("-- ------------------- --");
}catch (Exception e){
log.error("Error dumping the FileItemIterator", e);
}
}
示例4: getInputStreamFromRequest
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的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;
}
示例5: parseMultipartData
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的package包/类
private void parseMultipartData(RestServiceRequest rsr, IMendixObject argO,
JSONObject data) throws IOException, FileUploadException {
boolean hasFile = false;
for(FileItemIterator iter = servletFileUpload.getItemIterator(rsr.request); iter.hasNext();) {
FileItemStream item = iter.next();
if (!item.isFormField()){ //This is the file(?!)
if (!isFileSource) {
RestServices.LOGPUBLISH.warn("Received request with binary data but input argument isn't a filedocument. Skipping. At: " + rsr.request.getRequestURL().toString());
continue;
}
if (hasFile)
RestServices.LOGPUBLISH.warn("Received request with multiple files. Only one is supported. At: " + rsr.request.getRequestURL().toString());
hasFile = true;
Core.storeFileDocumentContent(rsr.getContext(), argO, determineFileName(item), item.openStream());
}
else
data.put(item.getFieldName(), IOUtils.toString(item.openStream()));
}
}
示例6: provideHeaders
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的package包/类
/**
* Provides parsed email headers from the "headers" param in a multipart/form-data request.
* <p>
* Although SendGrid parses some headers for us, it doesn't parse "reply-to", so we need to do
* this. Once we are doing it, it's easier to be consistent and use this as the sole source of
* truth for information that originates in the headers.
*/
@Provides
@Singleton
InternetHeaders provideHeaders(FileItemIterator iterator) {
try {
while (iterator != null && iterator.hasNext()) {
FileItemStream item = iterator.next();
// SendGrid sends us the headers in the "headers" param.
if (item.getFieldName().equals("headers")) {
try (InputStream stream = item.openStream()) {
// SendGrid always sends headers in UTF-8 encoding.
return new InternetHeaders(new ByteArrayInputStream(
CharStreams.toString(new InputStreamReader(stream, UTF_8.name())).getBytes(UTF_8)));
}
}
}
} catch (MessagingException | FileUploadException | IOException e) {
// If we fail parsing the headers fall through returning the empty header object below.
}
return new InternetHeaders(); // Parsing failed or there was no "headers" param.
}
示例7: parse
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的package包/类
public void parse(MultipartRequestCallback callback) throws IOException, FileUploadException, StatusServletException {
if (!ServletFileUpload.isMultipartContent(request)) {
LOGGER.error("Request content is not multipart.");
throw new StatusServletException(Response.SC_PRECONDITION_FAILED);
}
final FileItemIterator iterator = new ServletFileUpload(new DiskFileItemFactory()).getItemIterator(request);
while (iterator.hasNext()) {
// Gets the first HTTP request element.
final FileItemStream item = iterator.next();
if (item.isFormField()) {
final String value = Streams.asString(item.openStream(), "UTF-8");
properties.put(item.getFieldName(), value);
} else if(callback != null) {
callback.onInputStream(item.openStream(), item.getFieldName(), item.getContentType());
}
}
}
示例8: uploadFiles
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的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;
}
示例9: getRequestStream
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的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");
}
示例10: getRequestStream
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的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");
}
示例11: parseMultipartParameters
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的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;
}
示例12: handleMultipart
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的package包/类
private String handleMultipart(RecordedRequest request) {
RecordedUpload upload = new RecordedUpload(request);
Exception exception;
try {
Map<String,String> params = new HashMap<>();
FileItemIterator iter = upload.getItemIterator();
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
String value = Streams.asString(stream);
System.out.println("Form field " + name + " with value "
+ value + " detected.");
params.put(name,value);
} else {
System.out.println("File field " + name + " with file name "
+ item.getName() + " detected.");
params.put(name, "file->"+item.getName());
}
}
return "Multipart:"+JSON.toJSONString(params);
} catch (Exception e) {
exception = e;
}
return "Multipart:error->"+exception;
}
示例13: blockingIOAdapterFunctionalTest
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的package包/类
@Test
public void blockingIOAdapterFunctionalTest() throws Exception {
log.info("BLOCKING IO ADAPTER FUNCTIONAL TEST [ " + testCase.getDescription() + " ]");
if (log.isDebugEnabled()){
log.debug("Request body\n" + IOUtils.toString(testCase.getBodyInputStream()));
}
final FileUpload fileUpload = new FileUpload();
final FileItemIterator fileItemIterator = fileUpload.getItemIterator(testCase.getRequestContext());
try(final CloseableIterator<ParserToken> parts = Multipart.multipart(testCase.getMultipartContext()).forBlockingIO(testCase.getBodyInputStream())) {
while (parts.hasNext()) {
ParserToken parserToken = parts.next();
ParserToken.Type partItemType = parserToken.getType();
if (ParserToken.Type.NESTED_END.equals(partItemType) || ParserToken.Type.NESTED_START.equals(partItemType)) {
// Commons file upload is not returning an item representing the start/end of a nested multipart.
continue;
}
assertTrue(fileItemIterator.hasNext());
FileItemStream fileItemStream = fileItemIterator.next();
assertEquals(parserToken, fileItemStream);
}
}
}
示例14: iterator
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的package包/类
@Override
public Iterator<PartPayload> iterator() {
try {
final FileItemIterator itemIterator = new ServletFileUpload().getItemIterator(httpRequest);
return new PayloadIterator(itemIterator);
}
catch (FileUploadException | IOException e) {
throw new RuntimeException(e);
}
}
示例15: getFileItem
import org.apache.commons.fileupload.FileItemIterator; //导入依赖的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);
}