Example usage for org.apache.commons.fileupload FileItem getInputStream

List of usage examples for org.apache.commons.fileupload FileItem getInputStream

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileItem getInputStream.

Prototype

InputStream getInputStream() throws IOException;

Source Link

Document

Returns an java.io.InputStream InputStream that can be used to retrieve the contents of the file.

Usage

From source file:Algorithm.RequestParser.java

public static InputStream getStream(HttpServletRequest request) throws FileUploadException, IOException {
    if (ServletFileUpload.isMultipartContent(request)) {
        List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        for (FileItem item : multiparts) {
            if (!item.isFormField()) {
                InputStream img = item.getInputStream();
                return img;
            }/*ww  w  . j  a  v  a  2  s .  c o m*/
        }
    }
    return null;
}

From source file:fileserver.Utils.java

/**
 * Check exist file./*from  ww w.  jav a  2 s .c  o  m*/
 * @param fi - FileItem
 * @return true - exist, else - false
 */
public static boolean isFileExist(FileItem fi) {
    try {
        String id = calcID(fi.getInputStream());
        return isFileExist(id);
    } catch (IOException ex) {
        logger.error("is file exist", ex);
        return false;
    }
}

From source file:fileserver.Utils.java

/**
 * Compute unique identificator uploaded file
 * @param fi - FileItem//from  ww w  . j ava  2 s. co m
 * @return SHA1 Hex string
 */
public static String calcID(FileItem fi) {
    try {
        return DigestUtils.sha1Hex(fi.getInputStream());
    } catch (IOException ex) {
        logger.error("calcId", ex);
        return "";
    }
}

From source file:com.silverpeas.util.web.servlet.FileUploadUtil.java

public static void saveToFile(File file, FileItem item) throws IOException {
    OutputStream out = FileUtils.openOutputStream(file);
    InputStream in = item.getInputStream();
    try {//  w w  w. j  a v a 2  s.  c om
        IOUtils.copy(in, out);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:com.dien.upload.server.UploadAction.java

/**
 * Returns the content of a file as an InputStream if it is found in the 
 * FileItem vector.  /*www  . ja  v a 2 s.  c o m*/
 * 
 * @param sessionFiles collection of files sent by the client 
 * @param parameter field name or file name of the desired file 
 * @return an ImputString 
 */
public static InputStream getFileStream(Vector<FileItem> sessionFiles, String parameter) throws IOException {
    FileItem item = findFileItem(sessionFiles, parameter);
    return item == null ? null : item.getInputStream();
}

From source file:com.uniquesoft.uidl.servlet.UploadAction.java

/**
 * Returns the content of a file as an InputStream if it is found in the 
 * FileItem vector.  //from w  w  w .  java  2s.c o m
 * 
 * @param sessionFiles collection of files sent by the client 
 * @param parameter field name or file name of the desired file 
 * @return an ImputString 
 */
public static InputStream getFileStream(List<FileItem> sessionFiles, String parameter) throws IOException {
    FileItem item = findFileItem(sessionFiles, parameter);
    return item == null ? null : item.getInputStream();
}

From source file:com.openmeap.util.ServletUtils.java

static final public File tempFileFromFileItem(String temporaryStoragePath, FileItem item) throws IOException {
    File destinationFile = File.createTempFile("uploadArchive", "", new File(temporaryStoragePath));
    InputStream is = item.getInputStream();
    OutputStream os = new FileOutputStream(destinationFile);
    try {/*  w w  w .j  a va 2s  . c  o  m*/
        Utils.pipeInputStreamIntoOutputStream(is, os);
    } finally {
        is.close();
        os.close();
    }
    return destinationFile;
}

From source file:it.swim.util.ConvertitoreFotoInBlob.java

/**
 * Metodo per convertire un fileItem in blob, per poterlo salvare sul database
 * @param item : FileItem ottenuta nella servlet
 * @param lunghezza : int che rappresenta la lunghezza che dovra' avere l'immagine sul database
 * @param altezza : int che rappresenta l'altezza che dovra' avere l'immagine sul database
 * @param dimensioneMaxInMB : int che rappresenta la dimensione massima del file caricato
 * @return <b>blob</b> che rappresenta l'oggetto gia' ridimensionato, pronto per essere salvato sul database
 * @throws IOException errore durante il ridimensionamento oppure nella conversione in Blob
 * @throws SerialException errore nella conversione in Blob
 * @throws SQLException errore nella conversione in Blob
 * @throws FotoException errore con cause:
 *    FILETROPPOGRANDE: se la dimensione del file supera quella prevista
 *    NONRICONOSCIUTACOMEFOTO: dovuto all'upload di un file che non e' una foto, oppure lo e' ma il sistema non la riconosce come tale
 *///from   w  w w  .ja  va2s .com
public static Blob getBlobFromFileItem(FileItem item, int lunghezza, int altezza, int dimensioneMaxInMB)
        throws IOException, SerialException, SQLException, FotoException {
    Blob blob = null;
    InputStream filecontent = item.getInputStream();
    byte[] b = new byte[filecontent.available()];
    log.debug("inputstream blob: " + filecontent.available());
    if (filecontent.available() > 0) {
        filecontent.read(b); //metto i dati nell'array b che uso nei metodi successivi

        log.debug("       ------------            " + b.length);

        if (b.length > dimensioneMaxInMB * 1024 * 1024) { //se file piu' grande di dimensioneMaxInMB lancia eccezione
            throw new FotoException(FotoException.Causa.FILETROPPOGRANDE);
        }

        //per poter ridimensionare l'immagine ho bisogno dia vere un BufferedImage, allora converto il byte[] in BufferedImage
        BufferedImage bufferedImage = ImageResizer.convertiByteArrayInBufferedImage(b);

        //attenzione se non e' una immagine o ci sono altri problemi nel capire che il file e' una immagine, la variabile bufferedImage==null
        if (bufferedImage == null) {
            throw new FotoException(FotoException.Causa.NONRICONOSCIUTACOMEFOTO);
        } else {
            //ridimensiono l'immagine e riottengo un riferimento BufferedImage
            BufferedImage ridimensionata = ImageResizer.ridimensionaImmagine(bufferedImage, lunghezza, altezza);

            //ora riconverto la bufferedImage in byte[]
            byte[] ridottaConvertita = ImageResizer.convertiBufferedImageInByteArray(ridimensionata);

            //infine converto l'immagine in byte[] in un Blob per salvarlo sul database, non in questo metodo pero'
            blob = new SerialBlob(ridottaConvertita);
        }
    }
    filecontent.close();
    return blob;
}

From source file:com.liferay.apio.architect.internal.body.MultipartToBodyConverter.java

private static void _storeFileItem(FileItem fileItem, Consumer<String> valueConsumer,
        Consumer<BinaryFile> fileConsumer) {

    try {/*  www .  ja v  a 2  s .c  o m*/
        if (fileItem.isFormField()) {
            InputStream stream = fileItem.getInputStream();

            valueConsumer.accept(Streams.asString(stream));
        } else {
            BinaryFile binaryFile = new BinaryFile(fileItem.getInputStream(), fileItem.getSize(),
                    fileItem.getContentType(), fileItem.getName());

            fileConsumer.accept(binaryFile);
        }
    } catch (IOException ioe) {
        throw new BadRequestException("Invalid body", ioe);
    }
}

From source file:com.afis.jx.ckfinder.connector.utils.ImageUtils.java

/**
 * checks if image file is image.//from w  w  w . ja  v  a2 s.c  om
 *
 * @param item file upload item
 * @return true if file is image.
 */
public static boolean checkImageFile(final FileItem item) {
    BufferedImage bi;
    InputStream is = null;
    try {
        is = item.getInputStream();
        bi = ImageIO.read(is);
    } catch (IOException e) {
        return false;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }
    return (bi != null);
}