Example usage for org.apache.commons.fileupload ProgressListener update

List of usage examples for org.apache.commons.fileupload ProgressListener update

Introduction

In this page you can find the example usage for org.apache.commons.fileupload ProgressListener update.

Prototype

void update(long pBytesRead, long pContentLength, int pItems);

Source Link

Document

Updates the listeners status information.

Usage

From source file:com.vmware.appfactory.datastore.DsDatastoreCifs.java

@Override
public void copy(MultipartFile source, String destination, ProgressListener pl) throws IOException {
    /* Create destination file */
    File destFile = new File(destination);

    /* Open input and output, for copying */
    InputStream is = source.getInputStream();
    OutputStream os = new FileOutputStream(destFile);

    try {/*from  www.j av  a 2 s  .co  m*/
        /* We could use IOUtils.copy(), but this approach gives us progress: */
        long total = source.getSize();
        long soFar = 0;

        /* Transfer using a buffer */
        byte[] buffer = new byte[COPY_BUFFER_SIZE];
        int numRead;
        while ((numRead = is.read(buffer, 0, COPY_BUFFER_SIZE)) != -1) {
            os.write(buffer, 0, numRead);
            soFar += numRead;
            if (pl != null) {
                pl.update(soFar, total, 1);
            }
        }

        os.flush();
    } finally {
        os.close();
        is.close();
    }
}

From source file:eu.webtoolkit.jwt.servlet.WebRequest.java

@SuppressWarnings({ "unchecked", "deprecation" })
private void parse(final ProgressListener progressUpdate) throws IOException {
    if (FileUploadBase.isMultipartContent(this)) {
        try {/*from   w w w . j  a  v  a2s  .c  o m*/
            // Create a factory for disk-based file items
            DiskFileItemFactory factory = new DiskFileItemFactory();

            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);

            if (progressUpdate != null) {
                upload.setProgressListener(new org.apache.commons.fileupload.ProgressListener() {
                    public void update(long pBytesRead, long pContentLength, int pItems) {
                        progressUpdate.update(WebRequest.this, pBytesRead, pContentLength);
                    }
                });
            }

            // Parse the request
            List items = upload.parseRequest(this);

            parseParameters();

            Iterator itr = items.iterator();

            FileItem fi;
            File f = null;
            while (itr.hasNext()) {
                fi = (FileItem) itr.next();

                // Check if not form field so as to only handle the file inputs
                // else condition handles the submit button input
                if (!fi.isFormField()) {
                    try {
                        f = File.createTempFile("jwt", "jwt");
                        fi.write(f);
                        fi.delete();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    List<UploadedFile> files = files_.get(fi.getFieldName());
                    if (files == null) {
                        files = new ArrayList<UploadedFile>();
                        files_.put(fi.getFieldName(), files);
                    }
                    files.add(new UploadedFile(f.getAbsolutePath(), fi.getName(), fi.getContentType()));
                } else {
                    String[] v = parameters_.get(fi.getFieldName());
                    if (v == null)
                        v = new String[1];
                    else {
                        String[] newv = new String[v.length + 1];
                        for (int i = 0; i < v.length; ++i)
                            newv[i] = v[i];
                        v = newv;
                    }
                    v[v.length - 1] = fi.getString();
                    parameters_.put(fi.getFieldName(), v);
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
    } else
        parseParameters();
}

From source file:org.jason.mapmaker.server.util.UploadProgressInputStream.java

private void updateListeners(final long bytesRead, final long totalBytes) {

    for (ProgressListener listener : listeners) {

        listener.update(bytesRead, totalBytes, listeners.size());
    }//w ww. j a  va 2  s . com
}