org.apache.commons.fileupload.portlet.PortletFileUpload Java Examples

The following examples show how to use org.apache.commons.fileupload.portlet.PortletFileUpload. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdapterPortlet.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Handle multipart form.
 *
 * @param request
 *            the request
 * @param requestContext
 *            the request context
 *
 * @throws Exception
 *             the exception
 */
private void handleMultipartForm(ActionRequest request, RequestContextIFace requestContext) throws Exception {
	SourceBean serviceRequest = requestContext.getServiceRequest();

	// Create a factory for disk-based file items
	FileItemFactory factory = new DiskFileItemFactory();

	// Create a new file upload handler
	PortletFileUpload upload = new PortletFileUpload(factory);

	// Parse the request
	List fileItems = upload.parseRequest(request);
	Iterator iter = fileItems.iterator();
	while (iter.hasNext()) {
		FileItem item = (FileItem) iter.next();

		if (item.isFormField()) {
			String name = item.getFieldName();
			String value = item.getString();
			serviceRequest.setAttribute(name, value);
		} else {
			processFileField(item, requestContext);
		}
	}
}
 
Example #2
Source File: AdapterPortlet.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Handle multipart form.
 * 
 * @param request the request
 * @param requestContext the request context
 * 
 * @throws Exception the exception
 */
private void handleMultipartForm(ActionRequest request, RequestContextIFace requestContext) throws Exception {
	SourceBean serviceRequest = requestContext.getServiceRequest();
	
	// Create a factory for disk-based file items
	FileItemFactory factory = new DiskFileItemFactory();
	
    // Create a new file upload handler
	PortletFileUpload upload = new PortletFileUpload(factory);

    // Parse the request
    List fileItems = upload.parseRequest(request);
    Iterator iter = fileItems.iterator();
    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();

        if (item.isFormField()) {
            String name = item.getFieldName();
            String value = item.getString();
            serviceRequest.setAttribute(name, value);
        } else {
            processFileField(item, requestContext);
        }
    }
}
 
Example #3
Source File: AdapterPortlet.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
private void handleMultipartForm(ActionRequest request, RequestContextIFace requestContext) throws Exception {
	SourceBean serviceRequest = requestContext.getServiceRequest();

	// Create a factory for disk-based file items
	FileItemFactory factory = new DiskFileItemFactory();

	// Create a new file upload handler
	PortletFileUpload upload = new PortletFileUpload(factory);

	// Parse the request
	List fileItems = upload.parseRequest(request);
	Iterator iter = fileItems.iterator();
	while (iter.hasNext()) {
		FileItem item = (FileItem) iter.next();

		if (item.isFormField()) {
			String name = item.getFieldName();
			String value = item.getString();
			serviceRequest.setAttribute(name, value);
		} else {
			processFileField(item, requestContext);
		}
	}
}
 
Example #4
Source File: ActionRequestReader.java    From journaldev with MIT License 6 votes vote down vote up
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
	// Handle default MIME type request
	if(request.getContentType().equals("application/x-www-form-urlencoded")){
		String message = request.getParameter("message");
		System.out.println(message);
	}
	// Handle multipart request
	else if(request.getContentType().contains("multipart/form-data")){
		// Create FileItemFactory
		FileItemFactory factory = new DiskFileItemFactory();
		// Create PortletFileUpload instance
		PortletFileUpload fileUpload = new PortletFileUpload(factory);
		try {
			// Instead of parsing the request ourselves, let Apache PortletFileUpload do that
			List<FileItem> files = fileUpload.parseRequest(request);
			// Iterate over files
			for(FileItem item : files){
				// Print out some of information
				System.out.println("File Uploaded Name Is : "+item.getName()+" , Its Size Is :: "+item.getSize());
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		System.out.println(response.encodeURL("/index.html"));
	}
}
 
Example #5
Source File: PortletUtilities.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Gets the first uploaded file from a portlet request. This method creates a new file upload handler, 
 * parses the request, processes the uploaded items and then returns the first file as an
 * <code>UploadedFile</code> object.
 * @param portletRequest The input portlet request
 * @return	The first uploaded file object.
 */
public static UploadedFile getFirstUploadedFile(PortletRequest portletRequest){
	UploadedFile uploadedFile = null;
	try{
		
		DiskFileItemFactory factory = new DiskFileItemFactory();
		//		 Create a new file upload handler
		PortletFileUpload upload = new PortletFileUpload(factory);
	
		//		 Parse the request
		List /* FileItem */ items = upload.parseRequest((ActionRequest)portletRequest);
	
	
		//		 Process the uploaded items
		Iterator iter = items.iterator();
		boolean endLoop = false;
		while (iter.hasNext() && !endLoop) {
			FileItem item = (FileItem) iter.next();

			if (item.isFormField()) {
				//serviceRequest.setAttribute(item.getFieldName(), item.getString());
			} else {
				uploadedFile = new UploadedFile();
				uploadedFile.setFileContent(item.get());
				uploadedFile.setFieldNameInForm(item.getFieldName());
				uploadedFile.setSizeInBytes(item.getSize());
				uploadedFile.setFileName(item.getName());
				
				endLoop = true;
			}
		}
	}catch(Exception e){
		logger.error("Cannot parse multipart request", e);
	}
	return uploadedFile;
	
}
 
Example #6
Source File: CommonsPortletMultipartResolver.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isMultipart(ActionRequest request) {
	return (request != null && PortletFileUpload.isMultipartContent(request));
}
 
Example #7
Source File: CommonsPortletMultipartResolver.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Initialize the underlying {@code org.apache.commons.fileupload.portlet.PortletFileUpload}
 * instance. Can be overridden to use a custom subclass, e.g. for testing purposes.
 * @return the new PortletFileUpload instance
 */
@Override
protected FileUpload newFileUpload(FileItemFactory fileItemFactory) {
	return new PortletFileUpload(fileItemFactory);
}