本文整理汇总了Java中org.apache.commons.fileupload.FileItemFactory类的典型用法代码示例。如果您正苦于以下问题:Java FileItemFactory类的具体用法?Java FileItemFactory怎么用?Java FileItemFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileItemFactory类属于org.apache.commons.fileupload包,在下文中一共展示了FileItemFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getServiceResult
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
protected void getServiceResult(HttpServletRequest request, Document document) throws Exception {
// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart)
throw new IllegalArgumentException("Not multipart content!");
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List<FileItem> items = GenericUtils.cast(upload.parseRequest(request));
// Process the uploaded items
handleFormFields(request);
for (FileItem item : items) {
doUpload(request, document, item);
}
}
示例2: extractAttachments
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
/**
* extracts attachments from the http request.
*
* @param httpRequest
* request containing attachments DefaultMultipartHttpServletRequest is supported
* @param request
* {@link Request}
* @return ids of extracted attachments
* @throws IOException
* throws exception if problems during attachments accessing occurred
* @throws FileUploadException
* Exception.
* @throws AttachmentIsEmptyException
* Thrown, when the attachment is of zero size.
* @throws AuthorizationException
* in case there is no authenticated user
*/
private List<AttachmentResource> extractAttachments(HttpServletRequest httpRequest,
Request request) throws FileUploadException, IOException,
AttachmentIsEmptyException, AuthorizationException {
List<AttachmentResource> result = new ArrayList<>();
if (httpRequest.getContentType().startsWith(AttachmentResourceHelper.MULTIPART_FORM_DATA)) {
FileItemFactory factory = new DiskFileItemFactory();
FileUpload upload = new FileUpload(factory);
List<FileItem> items = upload.parseRequest(new ServletRequestContext(httpRequest));
for (FileItem file : items) {
if (!file.isFormField()) {
AttachmentTO attachmentTo = new AttachmentStreamTO(file.getInputStream(),
AttachmentStatus.UPLOADED);
AttachmentResourceHelper.assertAttachmentSize(file.getContentType(),
file.getSize(), false);
attachmentTo.setMetadata(new ContentMetadata());
attachmentTo.getMetadata().setFilename(getFileName(file.getName()));
attachmentTo.getMetadata().setContentSize(file.getSize());
result.add(persistAttachment(request, attachmentTo));
}
}
}
return result;
}
示例3: extractAttachments
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
/**
* extracts attachments from the http request.
*
* @param httpRequest
* request containing attachments DefaultMultipartHttpServletRequest is supported
* @param request
* {@link Request}
* @return ids of extracted attachments
* @throws MaxLengthReachedException
* attachment size is to large
* @throws IOException
* throws exception if problems during attachments accessing occurred
* @throws FileUploadException
* Exception.
* @throws AttachmentIsEmptyException
* Thrown, when the attachment is of zero size.
* @throws AuthorizationException
* in case there is no authenticated user
*/
private List<AttachmentResource> extractAttachments(HttpServletRequest httpRequest,
Request request) throws MaxLengthReachedException, FileUploadException, IOException,
AttachmentIsEmptyException, AuthorizationException {
List<AttachmentResource> result = new ArrayList<AttachmentResource>();
if (httpRequest.getContentType().startsWith(AttachmentResourceHelper.MULTIPART_FORM_DATA)) {
FileItemFactory factory = new DiskFileItemFactory();
FileUpload upload = new FileUpload(factory);
List<FileItem> items = upload.parseRequest(new ServletRequestContext(httpRequest));
for (FileItem file : items) {
if (!file.isFormField()) {
AttachmentTO attachmentTo = new AttachmentStreamTO(file.getInputStream(),
AttachmentStatus.UPLOADED);
AttachmentResourceHelper.assertAttachmentSize(file.getContentType(),
file.getSize(), false);
attachmentTo.setMetadata(new ContentMetadata());
attachmentTo.getMetadata().setFilename(getFileName(file.getName()));
attachmentTo.getMetadata().setContentSize(file.getSize());
result.add(persistAttachment(request, attachmentTo));
}
}
}
return result;
}
示例4: extractAttachments
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
/**
* extracts attachments from the http request.
*
* @param httpRequest
* request containing attachments DefaultMultipartHttpServletRequest is supported
* @param request
* {@link Request}
* @return ids of extracted attachments
* @throws MaxLengthReachedException
* attachment size is to large
* @throws IOException
* throws exception if problems during attachments accessing occurred
* @throws FileUploadException
* Exception.
* @throws AuthorizationException
* in case there is no authenticated user
*/
private List<AttachmentResource> extractAttachments(HttpServletRequest httpRequest,
Request request) throws MaxLengthReachedException, FileUploadException, IOException,
AuthorizationException {
List<AttachmentResource> result = new ArrayList<AttachmentResource>();
if (httpRequest.getContentType().startsWith(AttachmentResourceHelper.MULTIPART_FORM_DATA)) {
FileItemFactory factory = new DiskFileItemFactory();
FileUpload upload = new FileUpload(factory);
List<FileItem> items = upload.parseRequest(new ServletRequestContext(httpRequest));
for (FileItem file : items) {
if (!file.isFormField()) {
AttachmentTO attachmentTo = new AttachmentStreamTO(file.getInputStream(),
AttachmentStatus.UPLOADED);
AttachmentResourceHelper.assertAttachmentSize(file.getContentType(),
file.getSize(), false);
attachmentTo.setMetadata(new ContentMetadata());
attachmentTo.getMetadata().setFilename(getFileName(file.getName()));
attachmentTo.getMetadata().setContentSize(file.getSize());
result.add(persistAttachment(request, attachmentTo));
}
}
}
return result;
}
示例5: extractAttachments
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
/**
* extracts attachments from the http request.
*
* @param httpRequest
* request containing attachments DefaultMultipartHttpServletRequest is supported
* @param request
* {@link Request}
* @return ids of extracted attachments
* @throws MaxLengthReachedException
* attachment size is to large
* @throws IOException
* throws exception if problems during attachments accessing occurred
* @throws FileUploadException
* Exception.
* @throws AuthorizationException
* in case there is no authenticated user
*/
private List<AttachmentResource> extractAttachments(HttpServletRequest httpRequest,
Request request) throws MaxLengthReachedException, FileUploadException, IOException,
AuthorizationException {
List<AttachmentResource> result = new ArrayList<AttachmentResource>();
if (httpRequest.getContentType().startsWith(AttachmentResourceHelper.MULTIPART_FORM_DATA)) {
FileItemFactory factory = new DiskFileItemFactory();
FileUpload upload = new FileUpload(factory);
List<FileItem> items = upload.parseRequest(new ServletRequestContext(httpRequest));
for (FileItem file : items) {
if (!file.isFormField()) {
AttachmentTO attachmentTo = new AttachmentStreamTO(file.getInputStream(),
AttachmentStatus.UPLOADED);
AttachmentResourceHelper.checkAttachmentSize(file);
attachmentTo.setMetadata(new ContentMetadata());
attachmentTo.getMetadata().setFilename(getFileName(file.getName()));
attachmentTo.getMetadata().setContentSize(file.getSize());
result.add(persistAttachment(request, attachmentTo));
}
}
}
return result;
}
示例6: simpleExtract
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
/***
* Simplest way of handling files that are uploaded from form. Just put them
* as name-value pair into service data. This is useful ONLY if the files
* are reasonably small.
*
* @param req
* @param data
* data in which
*/
public void simpleExtract(HttpServletRequest req, ServiceData data) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
/**
* ServerFileUpload still deals with raw types. we have to have
* workaround that
*/
List<?> list = upload.parseRequest(req);
if (list != null) {
for (Object item : list) {
if (item instanceof FileItem) {
FileItem fileItem = (FileItem) item;
data.addValue(fileItem.getFieldName(),
fileItem.getString());
} else {
Spit.out("Servlet Upload retruned an item that is not a FileItem. Ignorinig that");
}
}
}
} catch (FileUploadException e) {
Spit.out(e);
data.addError("Error while parsing form data. " + e.getMessage());
}
}
示例7: getMultipartContent
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
public Map<String, FileItem> getMultipartContent() throws FileUploadException, IllegalAccessException {
if (!ServletFileUpload.isMultipartContent(request))
throw new IllegalAccessException();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
Map<String, FileItem> result = new HashMap<String, FileItem>();
@SuppressWarnings("unchecked")
List<FileItem> items = upload.parseRequest(request);
for (FileItem fi : items) {
result.put(fi.getFieldName(), fi);
}
return result;
}
示例8: processFileUpload
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
/** Process file upload */
private void processFileUpload(HttpRequest request, File uploadDir, String id) throws Exception {
FileItemFactory factory = new DiskFileItemFactory(Config.THRESHOLD_UPLOAD, uploadDir);
HttpServFileUpload fileUpload = new HttpServFileUpload(factory);
fileUpload.setProgressListener(new MyProgressListener(id));
List<FileItem> fileItems = fileUpload.parseRequest(new HttpServRequestContext(request));
Iterator<FileItem> iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (item.isFormField()) {
processFormField(item);
} else {
processUploadedFile(item, uploadDir);
}
}
}
示例9: handleFileUpload
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
/**
* Handles a file contained in the specified {@code request}. If you use
* FileUpload, please make sure to add the needed dependencies, i.e.
* {@code commons-fileupload} and {@code servlet-api} (later will be removed
* in a newer version of the {@code commons-fileupload}.
*
* @param request
* the request to handle the file upload from
* @param factory
* the {@code FileItemFactory} used to handle the request
*
* @return the {@code List} of loaded files
*
* @throws FileUploadException
* if the file upload fails
*/
public static List<FileItem> handleFileUpload(final HttpRequest request,
final FileItemFactory factory) throws FileUploadException {
final List<FileItem> items;
if (request instanceof HttpEntityEnclosingRequest
&& "POST".equals(request.getRequestLine().getMethod())) {
// create the ServletFileUpload
final FileUpload upload = new FileUpload(factory);
final HttpEntityEnclosingRequest entityRequest = (HttpEntityEnclosingRequest) request;
items = upload.parseRequest(new RequestWrapper(entityRequest));
} else {
items = new ArrayList<FileItem>();
}
return items;
}
示例10: getItems
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
public HashMap getItems(HttpServletRequest request) throws ServiceException{
HashMap itemMap=null;
try {
String tempPath = System.getProperty("java.io.tmpdir");
FileItemFactory factory = new DiskFileItemFactory(4096,new File(tempPath));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(1000000);
List fileItems = upload.parseRequest(request);
Iterator iter = fileItems.iterator();
itemMap=new HashMap();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
itemMap.put(item.getFieldName(), item.getString());
} else {
itemMap.put(item.getFieldName(), item);
}
}
} catch (Exception e) {
e.printStackTrace();
throw ServiceException.FAILURE("FileUploadHandler.getItems", e);
}
return itemMap;
}
示例11: getImgFileItem
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
/**
* This function gets the fileItem from the form
*
* @param items the list of fields sent to this servlet
* @return the imgItem if not found it returns null
*/
private FileItem getImgFileItem(final HttpServletRequest request) throws FileUploadException
{
// Create a factory for disk-based file items
final FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
final ServletFileUpload upload = new ServletFileUpload(factory);
final List< ? > items = upload.parseRequest(request); // get the items sent by the form
FileItem fileItem = null;
final Iterator< ? > iter = items.iterator();
// iterate over the items and if the required field is found break the loop
while (iter.hasNext()) {
fileItem = (FileItem) iter.next();
if (fileItem.isFormField() == false) {
break;
}
}
return fileItem;
}
示例12: getStringParametersMap
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
public static ParsedRequest getStringParametersMap(HttpServletRequest request) throws FileUploadException {
HashMap<String, String> textMap = new HashMap<>();
ParsedRequest parsedRequest = new ParsedRequest(textMap, null);
try {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> uploadItems = upload.parseRequest(request);
for (FileItem item : uploadItems) {
String fieldName = item.getFieldName();
String value = item.getString();
textMap.put(fieldName, value);
}
} catch (Exception e) {
loggerFactory.error(e);
}
return parsedRequest;
}
示例13: getParametersMap
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
public static ParsedRequest getParametersMap(HttpServletRequest request) throws FileUploadException {
HashMap<String, String> textMap = new HashMap<>();
HashMap<String, File> fileMap = new HashMap<>();
ParsedRequest parsedRequest = new ParsedRequest(textMap, fileMap);
try {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> uploadItems = upload.parseRequest(request);
for (FileItem item : uploadItems) {
String fieldName = item.getFieldName();
if (item.isFormField()) {
String value = item.getString();
textMap.put(fieldName, value);
} else {
String filename = FilenameUtils.getName(item.getName());
InputStream fileContent = item.getInputStream();
File file = new File(ServerConsts.tempFileStore + "/" + filename);
FileHelper.saveToFile(fileContent, file);
fileMap.put(fieldName, file);
}
}
} catch (Exception e) {
loggerFactory.error(e);
}
return parsedRequest;
}
示例14: handleRequestInternal
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
@RequestMapping(method = RequestMethod.POST)
protected ModelAndView handleRequestInternal(HttpServletRequest request) throws Exception {
String username = securityService.getCurrentUsername(request);
// Check that we have a file upload request.
if (!ServletFileUpload.isMultipartContent(request)) {
throw new Exception("Illegal request.");
}
Map<String, Object> map = new HashMap<String, Object>();
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<?> items = upload.parseRequest(request);
// Look for file items.
for (Object o : items) {
FileItem item = (FileItem) o;
if (!item.isFormField()) {
String fileName = item.getName();
byte[] data = item.get();
if (StringUtils.isNotBlank(fileName) && data.length > 0) {
createAvatar(fileName, data, username, map);
} else {
map.put("error", new Exception("Missing file."));
LOG.warn("Failed to upload personal image. No file specified.");
}
break;
}
}
map.put("username", username);
map.put("avatar", settingsService.getCustomAvatar(username));
return new ModelAndView("avatarUploadResult","model",map);
}
示例15: handlePost
import org.apache.commons.fileupload.FileItemFactory; //导入依赖的package包/类
@RequestMapping(method = RequestMethod.POST)
protected String handlePost(RedirectAttributes redirectAttributes,
HttpServletRequest request
) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
try {
if (ServletFileUpload.isMultipartContent(request)) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<?> items = upload.parseRequest(request);
for (Object o : items) {
FileItem item = (FileItem) o;
if ("file".equals(item.getFieldName()) && !StringUtils.isBlank(item.getName())) {
if (item.getSize() > MAX_PLAYLIST_SIZE_MB * 1024L * 1024L) {
throw new Exception("The playlist file is too large. Max file size is " + MAX_PLAYLIST_SIZE_MB + " MB.");
}
String playlistName = FilenameUtils.getBaseName(item.getName());
String fileName = FilenameUtils.getName(item.getName());
String format = StringUtils.lowerCase(FilenameUtils.getExtension(item.getName()));
String username = securityService.getCurrentUsername(request);
Playlist playlist = playlistService.importPlaylist(username, playlistName, fileName,
item.getInputStream(), null);
map.put("playlist", playlist);
}
}
}
} catch (Exception e) {
map.put("error", e.getMessage());
}
redirectAttributes.addFlashAttribute("model", map);
return "redirect:importPlaylist";
}