Search in sources :

Example 1 with FileItemFactory

use of org.apache.commons.fileupload.FileItemFactory in project libresonic by Libresonic.

the class ImportPlaylistController method handlePost.

@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, format, item.getInputStream(), null);
                    map.put("playlist", playlist);
                }
            }
        }
    } catch (Exception e) {
        map.put("error", e.getMessage());
    }
    redirectAttributes.addFlashAttribute("model", map);
    return "redirect:importPlaylist";
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) Playlist(org.libresonic.player.domain.Playlist) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) HashMap(java.util.HashMap) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with FileItemFactory

use of org.apache.commons.fileupload.FileItemFactory in project Lucee by lucee.

the class FormImpl method initializeMultiPart.

private void initializeMultiPart(PageContext pc, boolean scriptProteced) {
    // get temp directory
    Resource tempDir = ((ConfigImpl) pc.getConfig()).getTempDirectory();
    Resource tempFile;
    // Create a new file upload handler
    final String encoding = getEncoding();
    FileItemFactory factory = tempDir instanceof File ? new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, (File) tempDir) : new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setHeaderEncoding(encoding);
    // ServletRequestContext c = new ServletRequestContext(pc.getHttpServletRequest());
    HttpServletRequest req = pc.getHttpServletRequest();
    ServletRequestContext context = new ServletRequestContext(req) {

        @Override
        public String getCharacterEncoding() {
            return encoding;
        }
    };
    // Parse the request
    try {
        FileItemIterator iter = upload.getItemIterator(context);
        // byte[] value;
        InputStream is;
        ArrayList<URLItem> list = new ArrayList<URLItem>();
        String fileName;
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            is = IOUtil.toBufferedInputStream(item.openStream());
            if (item.getContentType() == null || StringUtil.isEmpty(item.getName())) {
                list.add(new URLItem(item.getFieldName(), new String(IOUtil.toBytes(is), encoding), false));
            } else {
                fileName = getFileName();
                tempFile = tempDir.getRealResource(fileName);
                _fileItems.put(fileName, new Item(tempFile, item.getContentType(), item.getName(), item.getFieldName()));
                String value = tempFile.toString();
                IOUtil.copy(is, tempFile, true);
                list.add(new URLItem(item.getFieldName(), value, false));
            }
        }
        raw = list.toArray(new URLItem[list.size()]);
        fillDecoded(raw, encoding, scriptProteced, pc.getApplicationContext().getSameFieldAsArray(SCOPE_FORM));
    } catch (Exception e) {
        SystemOut.printDate(e);
        // throw new PageRuntimeException(Caster.toPageException(e));
        fillDecodedEL(new URLItem[0], encoding, scriptProteced, pc.getApplicationContext().getSameFieldAsArray(SCOPE_FORM));
        initException = e;
    }
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) Resource(lucee.commons.io.res.Resource) ArrayList(java.util.ArrayList) ServletRequestContext(org.apache.commons.fileupload.servlet.ServletRequestContext) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) PageException(lucee.runtime.exp.PageException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpServletRequest(javax.servlet.http.HttpServletRequest) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) URLItem(lucee.commons.net.URLItem) URLItem(lucee.commons.net.URLItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) File(java.io.File) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) ConfigImpl(lucee.runtime.config.ConfigImpl)

Example 3 with FileItemFactory

use of org.apache.commons.fileupload.FileItemFactory in project mica2 by obiba.

the class DraftStudiesImportResource method getUploadedFile.

/**
 * Returns the first {@code FileItem} that is represents a file upload field. If no such field exists, this method
 * returns null
 *
 * @param request
 * @return
 * @throws FileUploadException
 */
FileItem getUploadedFile(HttpServletRequest request) throws FileUploadException {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    for (FileItem fileItem : upload.parseRequest(request)) {
        if (!fileItem.isFormField()) {
            return fileItem;
        }
    }
    return null;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory)

Example 4 with FileItemFactory

use of org.apache.commons.fileupload.FileItemFactory in project pentaho-platform by pentaho.

the class PluggableUploadFileServlet method getFileItem.

@SuppressWarnings("unchecked")
private FileItem getFileItem(HttpServletRequest request, long maxFileSize) throws FileUploadException {
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setFileSizeMax(maxFileSize);
    List items = upload.parseRequest(request);
    Iterator it = items.iterator();
    while (it.hasNext()) {
        FileItem item = (FileItem) it.next();
        if (!item.isFormField()) {
            return item;
        }
    }
    return null;
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) Iterator(java.util.Iterator) List(java.util.List) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory)

Example 5 with FileItemFactory

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

the class GcsBlobServlet method getFileItems.

private List<FileItem> getFileItems(HttpServletRequest request) throws FileUploadException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        FileItemFactory factory = new FileItemFactory() {

            @Override
            public FileItem createItem(String fieldName, String contentType, boolean isFormField, String fileName) {
                return new AppEngineAttachmentService.InMemoryFileItem(fieldName, contentType, isFormField, fileName);
            }
        };
        ServletFileUpload upload = new ServletFileUpload(factory);
        return upload.parseRequest(request);
    }
    throw new RuntimeException("No upload provided");
}
Also used : ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemFactory(org.apache.commons.fileupload.FileItemFactory)

Aggregations

FileItemFactory (org.apache.commons.fileupload.FileItemFactory)27 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)27 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)24 FileItem (org.apache.commons.fileupload.FileItem)23 FileUploadException (org.apache.commons.fileupload.FileUploadException)16 HashMap (java.util.HashMap)9 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 JSONObject (org.json.JSONObject)6 ApplicationContext (org.springframework.context.ApplicationContext)6 File (java.io.File)5 InputStream (java.io.InputStream)5 ServletException (javax.servlet.ServletException)5 ILogEventService (org.cerberus.crud.service.ILogEventService)5 MessageEvent (org.cerberus.engine.entity.MessageEvent)5 Answer (org.cerberus.util.answer.Answer)5 Iterator (java.util.Iterator)4 AnswerItem (org.cerberus.util.answer.AnswerItem)4 BufferedReader (java.io.BufferedReader)3