Package org.apache.commons.fileupload.disk

Examples of org.apache.commons.fileupload.disk.DiskFileItem


            return null;
        }

        List<String> fileNames = new ArrayList<String>(items.size());
        for (int i = 0; i < items.size(); i++) {
            DiskFileItem fileItem = (DiskFileItem) items.get(i);
            fileNames.add(fileItem.getStoreLocation().getName());
        }

        return (String[]) fileNames.toArray(new String[fileNames.size()]);
    }
View Full Code Here


            log.debug("Please upload 1 file per request");
            removeUploadResource(uploadId);
            return;
        }

        DiskFileItem fileItem = (DiskFileItem) itemList.get(0);
        String fileName = fileItem.getName();
        if (fileName == null)
            fileName = uploadId;
        fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);
        String storeLocation = uploadLocation_ + "/" + uploadId + "." + fileName;

        // commons-fileupload will store the temp file with name *.tmp
        // we need to rename it to our desired name
        fileItem.getStoreLocation().renameTo(new File(storeLocation));
        File fileStore = new File(storeLocation);
        if (!fileStore.exists())
            try {
                fileStore.createNewFile();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        upResource.setFileName(fileName);
        upResource.setMimeType(fileItem.getContentType());
        if (plugins != null)
            for (MimeTypeUploadPlugin plugin : plugins) {
                String mimeType = plugin.getMimeType(fileName);
                if (mimeType != null)
                    upResource.setMimeType(mimeType);
View Full Code Here

         log.debug("Please upload 1 file per request");
         removeUploadResource(uploadId);
         return;
      }

      DiskFileItem fileItem = (DiskFileItem)itemList.get(0);
      String fileName = fileItem.getName();
      if (fileName == null)
         fileName = uploadId;
      fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);
      String storeLocation = uploadLocation_ + "/" + uploadId + "." + fileName;

     
      // commons-fileupload will store the temp file with name *.tmp
      // we need to rename it to our desired name
      fileItem.getStoreLocation().renameTo(new File(storeLocation));
      File fileStore = new File(storeLocation);
      if (!fileStore.exists())
         try
         {
            fileStore.createNewFile();
         }
         catch (IOException e)
         {
            throw new RuntimeException(e);
         }
     

      upResource.setFileName(fileName);
      upResource.setMimeType(fileItem.getContentType());
      if(plugins != null)
         for(MimeTypeUploadPlugin plugin : plugins)
         {
            String mimeType = plugin.getMimeType(fileName) ;
            if(mimeType != null) upResource.setMimeType(mimeType) ;
View Full Code Here

    // FIXME imageFileItem does not get any data
    File image = new File("testImage_tooWide.jpg");

    if (image.length() > 0) {

      FileItem imageFileItem = new DiskFileItem("image", "image/JPEG",
          true, "file", 50000, null);
      // imageFileItem = image.toFileItem();

      ProcessCreateResponse testResponse = this
          .processTestRequest(
View Full Code Here

    // FIXME imageFileItem does not get any data
    File image = new File("testImage_tooHigh.jpg");

    if (image.length() > 0) {

      FileItem imageFileItem = new DiskFileItem("image", "image/JPEG",
          true, "file", 50000, null);
      // imageFileItem = image.toFileItem();

      ProcessCreateResponse testResponse = this
          .processTestRequest(
View Full Code Here

    // FIXME imageFileItem does not get any data
    File image = new File("testImage_wrongType.png");

    if (image.length() > 0) {

      FileItem imageFileItem = new DiskFileItem("image", "image/JPEG",
          true, "file", 50000, null);
      // imageFileItem = image.toFileItem();

      ProcessCreateResponse testResponse = this
          .processTestRequest(
View Full Code Here

         log.debug("Please upload 1 file per request");
         removeUploadResource(uploadId);
         return;
      }

      DiskFileItem fileItem = (DiskFileItem)itemList.get(0);
      String fileName = fileItem.getName();
      if (fileName == null)
         fileName = uploadId;
      fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);
      String storeLocation = uploadLocation_ + "/" + uploadId + "." + fileName;

     
      // commons-fileupload will store the temp file with name *.tmp
      // we need to rename it to our desired name
      fileItem.getStoreLocation().renameTo(new File(storeLocation));
      File fileStore = new File(storeLocation);
      if (!fileStore.exists())
         try
         {
            fileStore.createNewFile();
         }
         catch (IOException e)
         {
            throw new RuntimeException(e);
         }
     

      upResource.setFileName(fileName);
      upResource.setMimeType(fileItem.getContentType());
      if(plugins != null)
         for(MimeTypeUploadPlugin plugin : plugins)
         {
            String mimeType = plugin.getMimeType(fileName) ;
            if(mimeType != null) upResource.setMimeType(mimeType) ;
View Full Code Here

    }

    BSONObject result = new BasicBSONObject("_id", uploadId);
    BSONObject uploads = new BasicBSONObject();
    for (FileItem file : files) {
      DiskFileItem f = (DiskFileItem) file;
      // inline until 10KB
      if (f.isInMemory()) {
        uploads.put(f.getFieldName(), storage
            .inlineOrInsertContentsAndBackRefs(10240, f.get(),
                uploadId, f.getName(), f.getContentType()));
      } else {
        uploads.put(f.getFieldName(), storage
            .inlineOrInsertContentsAndBackRefs(10240, f
                .getStoreLocation(), uploadId, f.getName(), f
                .getContentType()));
      }
      file.delete();
    }
    result.put("files", uploads);
View Full Code Here

             
              Iterator i = fileItems.iterator();
            while(i.hasNext())
            {
                Object o = i.next();
                DiskFileItem dfi = (DiskFileItem)o;
                logger.info("dfi:" + dfi.getFieldName());
                logger.info("dfi:" + dfi);
               
                if (!dfi.isFormField()) {
                    String fieldName = dfi.getFieldName();
                    String fileName = dfi.getName();
                    String contentType = dfi.getContentType();
                    boolean isInMemory = dfi.isInMemory();
                    long sizeInBytes = dfi.getSize();
                   
                    logger.info("fieldName:" + fieldName);
                    logger.info("fileName:" + fileName);
                    logger.info("contentType:" + contentType);
                    logger.info("isInMemory:" + isInMemory);
                    logger.info("sizeInBytes:" + sizeInBytes);
                    File uploadedFile = new File("c:/temp/uploads/" + fileName);
                    dfi.write(uploadedFile);
 
                  req.setAttribute(dfi.getFieldName(), uploadedFile.getAbsolutePath());
                }
               
            }
          }
      }
View Full Code Here

            }

            // Copy the stream of file data to a temporary file. NOTE: This is necessary even if the
            // current field is a simple form-field because the call below to diskFileItem.getString()
            // will fail otherwise.
            DiskFileItem diskFileItem = (DiskFileItem) diskFileItemFactory.createItem(fieldName,
                contentType, formField, fileName);
            Streams.copy(fieldStream.openStream(), diskFileItem.getOutputStream(), true);

            // If the current field is a simple form-field, then save the form field value in the map.
            if (diskFileItem.isFormField()) {
              String characterEncoding = clientDataRequest.getCharacterEncoding();
              String requestParameterValue = null;

              if (characterEncoding == null) {
                requestParameterValue = diskFileItem.getString();
              }
              else {
                requestParameterValue = diskFileItem.getString(characterEncoding);
              }

              facesRequestParameterMap.addValue(fieldName, requestParameterValue);
            }
            else {

              File tempFile = diskFileItem.getStoreLocation();

              // If the copy was successful, then
              if (tempFile.exists()) {

                // Copy the commons-fileupload temporary file to a file in the same temporary
                // location, but with the filename provided by the user in the upload. This has two
                // benefits: 1) The temporary file will have a nice meaningful name. 2) By copying
                // the file, the developer can have access to a semi-permanent file, because the
                // commmons-fileupload DiskFileItem.finalize() method automatically deletes the
                // temporary one.
                String tempFileName = tempFile.getName();
                String tempFileAbsolutePath = tempFile.getAbsolutePath();

                String copiedFileName = stripIllegalCharacters(fileName);

                String copiedFileAbsolutePath = tempFileAbsolutePath.replace(tempFileName,
                    copiedFileName);
                File copiedFile = new File(copiedFileAbsolutePath);
                FileUtils.copyFile(tempFile, copiedFile);

                // If present, build up a map of headers.
                Map<String, List<String>> headersMap = new HashMap<String, List<String>>();
                FileItemHeaders fileItemHeaders = fieldStream.getHeaders();

                if (fileItemHeaders != null) {
                  Iterator<String> headerNameItr = fileItemHeaders.getHeaderNames();

                  if (headerNameItr != null) {

                    while (headerNameItr.hasNext()) {
                      String headerName = headerNameItr.next();
                      Iterator<String> headerValuesItr = fileItemHeaders.getHeaders(headerName);
                      List<String> headerValues = new ArrayList<String>();

                      if (headerValuesItr != null) {

                        while (headerValuesItr.hasNext()) {
                          String headerValue = headerValuesItr.next();
                          headerValues.add(headerValue);
                        }
                      }

                      headersMap.put(headerName, headerValues);
                    }
                  }
                }

                // Put a valid UploadedFile instance into the map that contains all of the
                // uploaded file's attributes, along with a successful status.
                Map<String, Object> attributeMap = new HashMap<String, Object>();
                String id = Long.toString(((long) hashCode()) + System.currentTimeMillis());
                String message = null;
                UploadedFile uploadedFile = uploadedFileFactory.getUploadedFile(copiedFileAbsolutePath,
                    attributeMap, diskFileItem.getCharSet(), diskFileItem.getContentType(),
                    headersMap, id, message, fileName, diskFileItem.getSize(),
                    UploadedFile.Status.FILE_SAVED);

                facesRequestParameterMap.addValue(fieldName, copiedFileAbsolutePath);
                addUploadedFile(uploadedFileMap, fieldName, uploadedFile);
                logger.debug("Received uploaded file fieldName=[{0}] fileName=[{1}]", fieldName,
View Full Code Here

TOP

Related Classes of org.apache.commons.fileupload.disk.DiskFileItem

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.