Search in sources :

Example 31 with FileItem

use of org.apache.commons.fileupload.FileItem in project xwiki-platform by xwiki.

the class XWikiAttachmentContent method getContentOutputStream.

/**
 * Set the content of the attachment by writing to a provided OutputStream. Content is *not* appended, this method
 * clears the content and creates new content. If you want to append content, you can call
 * {@link #getContentInputStream()} and copy the content of that into the provided OutputStream. Before closing this
 * OutputStream the content will remain the old content prior to the change.
 *
 * @return an OutputStream into which the caller can set the content of the attachments.
 * @since 4.2M3
 */
public OutputStream getContentOutputStream() {
    final FileItem fi = getNewFileItem();
    final XWikiAttachmentContent xac = this;
    final OutputStream fios;
    try {
        fios = fi.getOutputStream();
    } catch (IOException e) {
        // so unless it is modified, this should not happen.
        throw new RuntimeException("Exception getting attachment OutputStream.", e);
    }
    return (new ProxyOutputStream(fios) {

        @Override
        public void close() throws IOException {
            super.close();
            xac.file = fi;
            xac.setContentDirty(true);
            if (xac.attachment != null) {
                xac.attachment.setLongSize(xac.getLongSize());
            }
        }
    });
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) OutputStream(java.io.OutputStream) ProxyOutputStream(org.apache.commons.io.output.ProxyOutputStream) ProxyOutputStream(org.apache.commons.io.output.ProxyOutputStream) IOException(java.io.IOException)

Example 32 with FileItem

use of org.apache.commons.fileupload.FileItem in project activityinfo by bedatadriven.

the class AttachmentServlet method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        String key = request.getParameter("blobId");
        Integer siteId = Integer.valueOf(request.getParameter("siteId"));
        FileItem fileItem = getFirstUploadFile(request);
        String fileName = fileItem.getName();
        InputStream uploadingStream = fileItem.getInputStream();
        service.upload(key, fileItem, uploadingStream);
        CreateSiteAttachment siteAttachment = new CreateSiteAttachment();
        siteAttachment.setSiteId(siteId);
        siteAttachment.setBlobId(key);
        siteAttachment.setFileName(fileName);
        siteAttachment.setBlobSize(fileItem.getSize());
        siteAttachment.setContentType(fileItem.getContentType());
        dispatcher.execute(siteAttachment);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Error handling upload", e);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) InputStream(java.io.InputStream) CreateSiteAttachment(org.activityinfo.legacy.shared.command.CreateSiteAttachment) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 33 with FileItem

use of org.apache.commons.fileupload.FileItem in project wicket by apache.

the class FileUploadField method getFileUploads.

/**
 * @return a list of all uploaded files. The list is empty if no files were selected. It will return more than one files if:
 *         <ul>
 *         <li>HTML5 &lt;input type="file" <strong>multiple</strong> /&gt; is used</li>
 *         <li>the browser supports <em>multiple</em> attribute</li>
 *         <li>the user has selected more than one files from the <em>Select file</em> dialog</li>
 *         </ul>
 */
public List<FileUpload> getFileUploads() {
    if (fileUploads != null) {
        return fileUploads;
    }
    fileUploads = new ArrayList<>();
    // Get request
    final Request request = getRequest();
    // If we successfully installed a multipart request
    if (request instanceof IMultipartWebRequest) {
        // Get the item for the path
        final List<FileItem> fileItems = ((IMultipartWebRequest) request).getFile(getInputName());
        if (fileItems != null) {
            for (FileItem item : fileItems) {
                // WICKET-6270 detect empty field by missing file name
                if (Strings.isEmpty(item.getName()) == false) {
                    fileUploads.add(new FileUpload(item));
                }
            }
        }
    }
    return fileUploads;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) IMultipartWebRequest(org.apache.wicket.protocol.http.IMultipartWebRequest) Request(org.apache.wicket.request.Request) IMultipartWebRequest(org.apache.wicket.protocol.http.IMultipartWebRequest)

Example 34 with FileItem

use of org.apache.commons.fileupload.FileItem in project wicket by apache.

the class MultiFileUploadField method getInputAsArray.

/**
 * @see org.apache.wicket.markup.html.form.FormComponent#getInputAsArray()
 */
@Override
public String[] getInputAsArray() {
    if (inputArrayCache == null) {
        // this array will aggregate all input names
        ArrayList<String> names = null;
        final Request request = getRequest();
        if (request instanceof IMultipartWebRequest) {
            // retrieve the filename->FileItem map from request
            final Map<String, List<FileItem>> itemNameToItem = ((IMultipartWebRequest) request).getFiles();
            for (Entry<String, List<FileItem>> entry : itemNameToItem.entrySet()) {
                // iterate over the map and build the list of filenames
                final String name = entry.getKey();
                final List<FileItem> fileItems = entry.getValue();
                if (!Strings.isEmpty(name) && name.startsWith(getInputName() + MAGIC_SEPARATOR) && !fileItems.isEmpty() && !Strings.isEmpty(fileItems.get(0).getName())) {
                    // make sure the fileitem belongs to this component and
                    // is not empty
                    names = (names != null) ? names : new ArrayList<String>();
                    names.add(name);
                }
            }
        }
        if (names != null) {
            inputArrayCache = names.toArray(new String[names.size()]);
        }
    }
    return inputArrayCache;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) IMultipartWebRequest(org.apache.wicket.protocol.http.IMultipartWebRequest) Request(org.apache.wicket.request.Request) IMultipartWebRequest(org.apache.wicket.protocol.http.IMultipartWebRequest) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with FileItem

use of org.apache.commons.fileupload.FileItem in project wicket by apache.

the class FileUploadTest method getInputStream.

/**
 * Test that when getting an input stream a new input stream is returned every time.
 *
 * Also test that the inputstream is saved internally for later closing.
 *
 * @throws Exception
 */
@Test
public void getInputStream() throws Exception {
    final IFileCleaner fileUploadCleaner = new FileCleaner();
    DiskFileItemFactory itemFactory = new DiskFileItemFactory() {

        @Override
        public FileCleaningTracker getFileCleaningTracker() {
            return new FileCleanerTrackerAdapter(fileUploadCleaner);
        }
    };
    FileItem fileItem = itemFactory.createItem("dummyFieldName", "text/java", false, "FileUploadTest.java");
    // Initialize the upload
    fileItem.getOutputStream();
    // Get the internal list out
    Field inputStreamsField = FileUpload.class.getDeclaredField("inputStreamsToClose");
    inputStreamsField.setAccessible(true);
    FileUpload fileUpload = new FileUpload(fileItem);
    List<?> inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertNull(inputStreams);
    InputStream is1 = fileUpload.getInputStream();
    inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertEquals(1, inputStreams.size());
    InputStream is2 = fileUpload.getInputStream();
    inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertEquals(2, inputStreams.size());
    assertNotSame(is1, is2);
    // Ok lets close all the streams
    try {
        fileUpload.closeStreams();
    } catch (Exception e) {
        fail();
    }
    inputStreams = (List<?>) inputStreamsField.get(fileUpload);
    assertNull(inputStreams);
    fileUploadCleaner.destroy();
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) Field(java.lang.reflect.Field) FileCleanerTrackerAdapter(org.apache.wicket.util.file.FileCleanerTrackerAdapter) InputStream(java.io.InputStream) List(java.util.List) IFileCleaner(org.apache.wicket.util.file.IFileCleaner) FileCleaner(org.apache.wicket.util.file.FileCleaner) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) IFileCleaner(org.apache.wicket.util.file.IFileCleaner) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

FileItem (org.apache.commons.fileupload.FileItem)165 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)78 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)72 FileUploadException (org.apache.commons.fileupload.FileUploadException)59 File (java.io.File)55 IOException (java.io.IOException)51 ArrayList (java.util.ArrayList)40 HashMap (java.util.HashMap)32 ServletException (javax.servlet.ServletException)30 List (java.util.List)27 InputStream (java.io.InputStream)24 FileItemFactory (org.apache.commons.fileupload.FileItemFactory)23 DiskFileItem (org.apache.commons.fileupload.disk.DiskFileItem)16 Map (java.util.Map)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 ServletRequestContext (org.apache.commons.fileupload.servlet.ServletRequestContext)10 Test (org.junit.Test)10 Iterator (java.util.Iterator)9 FileItemWrap (com.github.bordertech.wcomponents.file.FileItemWrap)8 Locale (java.util.Locale)8