Example usage for org.apache.commons.fileupload FileItemStream openStream

List of usage examples for org.apache.commons.fileupload FileItemStream openStream

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileItemStream openStream.

Prototype

InputStream openStream() throws IOException;

Source Link

Document

Creates an InputStream , which allows to read the items contents.

Usage

From source file:io.kloudwork.util.MultipartFormHandler.java

public static MultipartParamHolder handle(Request request) throws IOException, FileUploadException {
    if (!ServletFileUpload.isMultipartContent(request.raw())) {
        return parseContent(request);
    }/* ww w .  j  a  v a  2 s .  c  o  m*/
    ServletFileUpload servletFileUpload = new ServletFileUpload();
    FileItemIterator iterator = servletFileUpload.getItemIterator(request.raw());

    Map<String, String> postParameters = new HashMap<>();
    Map<String, FileItemStream> files = new HashMap<>();

    while (iterator.hasNext()) {
        FileItemStream fileItem = iterator.next();
        if (fileItem.isFormField()) {
            try (InputStream is = fileItem.openStream()) {
                postParameters.put(fileItem.getFieldName(), Streams.asString(is));
            }
        } else {
            files.put(fileItem.getFieldName(), fileItem);
        }
    }

    if (files.isEmpty()) {
        return new MultipartParamHolder(postParameters);
    }

    return new MultipartParamHolder(postParameters, files);
}

From source file:de.egore911.opengate.FileuploadServletRequest.java

public static FileuploadServletRequest wrap(HttpServletRequest req, User user)
        throws ServletException, IOException {
    if (req.getContentType().toLowerCase(Locale.ENGLISH).startsWith(FileUploadBase.MULTIPART)) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        Map<String, List<String>> parameterValues = new HashMap<String, List<String>>();
        ServletFileUpload upload = new ServletFileUpload();
        try {//ww  w .  j a v a  2s  . c o  m
            FileItemIterator iter = upload.getItemIterator(req);
            while (iter.hasNext()) {
                FileItemStream stream = iter.next();
                InputStream s = stream.openStream();
                try {
                    String fieldName = stream.getFieldName();
                    if (stream.isFormField()) {
                        if (parameters.containsKey(fieldName)) {
                            if (!parameterValues.containsKey(fieldName)) {
                                parameterValues.put(fieldName, new ArrayList<String>());
                                parameterValues.get(fieldName).add((String) parameters.get(fieldName));
                            }
                            parameterValues.get(fieldName).add(IOUtils.toString(s));
                        } else {
                            parameters.put(fieldName, IOUtils.toString(s));
                        }
                    } else {
                        byte[] byteArray = IOUtils.toByteArray(s);
                        if (byteArray.length > 0) {
                            BinaryData binaryData = new BinaryData();
                            binaryData.setData(new Blob(byteArray));
                            binaryData.setMimetype(stream.getContentType());
                            binaryData.setFilename(stream.getName());
                            binaryData.setSize(byteArray.length);
                            AbstractEntity.setCreated(binaryData, user);
                            parameters.put(fieldName, binaryData);
                        }
                    }
                } finally {
                    s.close();
                }
            }
        } catch (FileUploadException e) {
            throw new ServletException(e);
        }
        return new FileuploadServletRequest(parameters, parameterValues);
    } else {
        return new FileuploadServletRequest(req);
    }
}

From source file:com.mobiaware.util.UploadHelpers.java

public static File createUploadFile(final HttpServletRequest request) {
    DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
    fileItemFactory.setSizeThreshold(THRESHOLD_SIZE);
    fileItemFactory.setRepository(FileUtils.getTempDirectory());

    ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
    servletFileUpload.setFileSizeMax(MAX_FILE_SIZE);
    servletFileUpload.setSizeMax(REQUEST_SIZE);

    File file = null;/*  w ww .java2  s .com*/

    try {
        FileItemIterator fileItemIterator = servletFileUpload.getItemIterator(request);
        while (fileItemIterator.hasNext()) {
            FileItemStream fileItem = fileItemIterator.next();

            if (fileItem.isFormField()) {
                // ignore
            } else {
                file = File.createTempFile("liim_", null);

                InputStream is = new BufferedInputStream(fileItem.openStream());
                FileUtils.copyInputStreamToFile(is, file);
            }
        }
    } catch (IOException e) {
        LOG.error(Throwables.getStackTraceAsString(e));
    } catch (FileUploadException e) {
        LOG.error(Throwables.getStackTraceAsString(e));
    }

    return file;
}

From source file:com.elit2.app.model.FileUpload.java

public static boolean proccessFile(String path, FileItemStream item) {
    try {/*from  w  ww .j  ava 2s  .  c  o  m*/
        File f = new File(path + File.separator + "images");
        if (!f.exists()) {
            f.mkdir();
        }
        File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();

        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read()) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

From source file:in.co.sneh.model.CargaExcelRuralModel.java

public static boolean processFile(String path, FileItemStream item) {
    try {/*  www  . jav  a2s . c o m*/
        File f = new File(path + File.separator + "exceles" + File.separator);
        if (!f.exists()) {
            f.mkdir();
        }
        File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();
        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read(b)) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();
        return true;
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return false;
}

From source file:helpers.FileUpload.java

public static boolean processFile(String path, FileItemStream item) {
    try {/*from w w  w.  java 2 s  .c o m*/

        File f = new File(path + File.separator + "assets" + File.separator + "uploads");
        if (!f.exists()) {
            f.mkdir();
        }
        File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();
        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read(b)) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();
        return true;
    } catch (Exception e) {
        System.out.println("FileUpload 35: " + e.getMessage());
    }
    return false;
}

From source file:com.google.enterprise.adaptor.experimental.Sim.java

static Map<String, byte[]> splitMultipartRequest(RequestContext req) throws IOException {
    Map<String, byte[]> parts = new HashMap<String, byte[]>();
    try {//from   w w  w .  ja v  a  2 s.  c o m
        FileUpload upload = new FileUpload();
        FileItemIterator iterator = upload.getItemIterator(req);
        while (iterator.hasNext()) {
            FileItemStream item = iterator.next();
            String field = item.getFieldName();
            byte value[] = IOHelper.readInputStreamToByteArray(item.openStream());
            parts.put(field, value);
        }
        return parts;
    } catch (FileUploadException e) {
        throw new IOException("caught FileUploadException", e);
    }
}

From source file:DBMS.UpdateFileUpload.java

public static boolean processFile(String path, FileItemStream item, int id) {
    try {//from  w w w  .ja  v  a  2  s  .com
        String check = item.getName();
        if (check.endsWith(".jpg") || check.endsWith(".JPG")) {
            String imstring = "images/" + Integer.toString(id);
            File f = new File(path + File.separator + imstring);
            if (!f.exists())
                f.mkdir();
            File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
            FileOutputStream fos = new FileOutputStream(savedFile);
            InputStream is = item.openStream();
            int x = 0;
            byte[] b = new byte[1024];
            while ((x = is.read(b)) != -1) {
                fos.write(b, 0, x);
            }
            fos.flush();
            fos.close();
            String dbimage = imstring + "/a.jpg";
            //dc.enterImage(dbimage);
            //im =dbimage;
            //System.out.println("Resizing!");
            //Resize rz = new Resize();
            //rz.resize(dbimage);
            BufferedImage originalImage = ImageIO.read(savedFile);
            int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
            BufferedImage resizeImageJpg = resizeImage(originalImage, type);
            ImageIO.write(resizeImageJpg, "jpg", savedFile);
            File rFile = new File(f.getAbsolutePath() + "/a.jpg");
            savedFile.renameTo(rFile);
            ProfileEditDB dc = new ProfileEditDB();
            dc.enterImage(id, dbimage);
            System.out.println("Link Entered to Database!");
            return true;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:elw.web.ControllerElw.java

protected static InputSupplier<InputStream> supplierForFileItem(final FileItemStream item) {
    return new InputSupplier<InputStream>() {
        public InputStream getInput() throws IOException {
            return item.openStream();
        }//www . j av  a2  s. c  o  m
    };
}

From source file:uploadProcess.java

public static boolean encrypt(String path, FileItemStream item, String patientID) {
    try {/*from   w  w w  .ja  v  a 2s  . c o m*/
        File mainFolder = new File(path + File.separator + "Encrypt");
        if (!mainFolder.exists()) {
            mainFolder.mkdir();
        }
        File patientFolder = new File(mainFolder + File.separator + patientID);
        if (!patientFolder.exists()) {
            patientFolder.mkdir();
        }
        String ukey = GetKey.getPatientKey(patientID);
        InputStream fis = item.openStream();

        FileOutputStream fos = new FileOutputStream(
                patientFolder.getAbsolutePath() + File.separator + item.getName());
        byte[] k = ukey.getBytes();
        SecretKeySpec key = new SecretKeySpec(k, "AES");
        System.out.println(key);
        Cipher enc = Cipher.getInstance("AES");
        enc.init(Cipher.ENCRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(fos, enc);
        byte[] buf = new byte[1024];
        int read;
        while ((read = fis.read(buf)) != -1) {
            cos.write(buf, 0, read);
        }
        fis.close();
        fos.flush();
        cos.close();

        //Upload File to cloud
        DropboxUpload upload = new DropboxUpload();
        upload.uploadFile(patientFolder, item.getName(), StoragePath.getDropboxDir() + patientID);
        DeleteDirectory.delete(patientFolder);

        return true;
    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
    return false;
}