本文整理汇总了Java中org.apache.commons.fileupload.FileItemIterator.hasNext方法的典型用法代码示例。如果您正苦于以下问题:Java FileItemIterator.hasNext方法的具体用法?Java FileItemIterator.hasNext怎么用?Java FileItemIterator.hasNext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.fileupload.FileItemIterator
的用法示例。
在下文中一共展示了FileItemIterator.hasNext方法的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: 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.
}
示例6: 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());
}
}
}
示例7: 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;
}
示例8: 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");
}
示例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();
if (item.getFieldName().equals(expectedFieldName)) {
return item.openStream();
}
}
throw new IllegalArgumentException("Field " + expectedFieldName + " not found in upload");
}
示例10: 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;
}
示例11: 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;
}
示例12: 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);
}
示例13: doPost
import org.apache.commons.fileupload.FileItemIterator; //导入方法依赖的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);
}
}
示例14: provideParameterMap
import org.apache.commons.fileupload.FileItemIterator; //导入方法依赖的package包/类
@Provides
@Singleton
Multimap<String, String> provideParameterMap(
@RequestData("queryString") String queryString,
@RequestData("postBody") Lazy<String> lazyPostBody,
@RequestData("charset") String requestCharset,
FileItemIterator multipartIterator) {
// Calling request.getParameter() or request.getParameterMap() etc. consumes the POST body. If
// we got the "postpayload" param we don't want to parse the body, so use only the query params.
// Note that specifying both "payload" and "postpayload" will result in the "payload" param
// being honored and the POST body being completely ignored.
ImmutableMultimap.Builder<String, String> params = new ImmutableMultimap.Builder<>();
Multimap<String, String> getParams = parseQuery(queryString);
params.putAll(getParams);
if (getParams.containsKey("postpayload")) {
// Treat the POST body as if it was the "payload" param.
return params.put("payload", nullToEmpty(lazyPostBody.get())).build();
}
// No "postpayload" so it's safe to consume the POST body and look for params there.
if (multipartIterator == null) { // Handle GETs and form-urlencoded POST requests.
params.putAll(parseQuery(nullToEmpty(lazyPostBody.get())));
} else { // Handle multipart/form-data requests.
try {
while (multipartIterator != null && multipartIterator.hasNext()) {
FileItemStream item = multipartIterator.next();
try (InputStream stream = item.openStream()) {
params.put(
item.isFormField() ? item.getFieldName() : item.getName(),
CharStreams.toString(new InputStreamReader(stream, requestCharset)));
}
}
} catch (FileUploadException | IOException e) {
// Ignore the failure and fall through to return whatever params we managed to parse.
}
}
return params.build();
}
示例15: Upload
import org.apache.commons.fileupload.FileItemIterator; //导入方法依赖的package包/类
private Upload(final HttpServletRequest request) {
this.upload = new ServletFileUpload();
this.request = request;
super.setStreamProvider(new InputStreamProvider() {
@Override
public InputStream getStream(String path) throws IOException {
try {
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream stm = iter.next();
if (stm.getFieldName().equals(path)) {
return stm.openStream();
}
}
} catch (Exception e) {
throw new IOException(e);
}
throw new IOException(path + " does not exists");
}
@Override
public String getRealPath(String p) {
throw new UnsupportedOperationException();
}
});
}