Example usage for org.apache.commons.fileupload DiskFileUpload setRepositoryPath

List of usage examples for org.apache.commons.fileupload DiskFileUpload setRepositoryPath

Introduction

In this page you can find the example usage for org.apache.commons.fileupload DiskFileUpload setRepositoryPath.

Prototype

public void setRepositoryPath(String repositoryPath) 

Source Link

Document

Sets the location used to temporarily store files that are larger than the configured size threshold.

Usage

From source file:org.liquidsite.core.web.MultiPartRequest.java

/**
 * Parses the incoming multi-part HTTP request.
 *
 * @param request        the HTTP request
 *
 * @throws FileUploadException if the request couldn't be parsed
 *             correctly/*www.  j a  v a 2s . co m*/
 */
private void parse(HttpServletRequest request) throws FileUploadException {

    DiskFileUpload parser = new DiskFileUpload();
    List list;
    FileItem item;
    String value;

    // Create multi-part parser
    parser.setRepositoryPath(uploadDir);
    parser.setSizeMax(uploadSize);
    parser.setSizeThreshold(4096);

    // Parse request
    list = parser.parseRequest(request);
    for (int i = 0; i < list.size(); i++) {
        item = (FileItem) list.get(i);
        if (item.isFormField()) {
            try {
                value = item.getString("UTF-8");
            } catch (UnsupportedEncodingException ignore) {
                value = item.getString();
            }
            parameters.put(item.getFieldName(), value);
        } else {
            files.put(item.getFieldName(), new MultiPartFile(item));
        }
    }
}

From source file:org.sakaiproject.tool.messageforums.FileUploadFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (!(request instanceof HttpServletRequest)) {
        chain.doFilter(request, response);
        return;/*from w  w  w .  j  av a2  s  .  c o m*/
    }

    HttpServletRequest httpRequest = (HttpServletRequest) request;

    boolean isMultipartContent = FileUpload.isMultipartContent(httpRequest);
    if (!isMultipartContent) {
        chain.doFilter(request, response);
        return;
    }

    DiskFileUpload upload = new DiskFileUpload();
    if (repositoryPath != null) {
        upload.setRepositoryPath(repositoryPath);
    }

    try {
        List list = upload.parseRequest(httpRequest);
        final Map map = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            FileItem item = (FileItem) list.get(i);
            String str = item.getString();
            if (item.isFormField()) {
                map.put(item.getFieldName(), new String[] { str });
            } else {
                httpRequest.setAttribute(item.getFieldName(), item);
            }
        }

        chain.doFilter(new HttpServletRequestWrapper(httpRequest) {
            public Map getParameterMap() {
                return map;
            }

            public String[] getParameterValues(String name) {
                Map map = getParameterMap();
                return (String[]) map.get(name);
            }

            public String getParameter(String name) {
                String[] params = getParameterValues(name);
                if (params == null) {
                    return null;
                }
                return params[0];
            }

            public Enumeration getParameterNames() {
                Map map = getParameterMap();
                return Collections.enumeration(map.keySet());
            }
        }, response);
    } catch (FileUploadException ex) {
        ServletException servletEx = new ServletException();
        servletEx.initCause(ex);
        throw servletEx;
    }
}

From source file:org.sakaiproject.tool.syllabus.FileUploadFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (!(request instanceof HttpServletRequest)) {
        chain.doFilter(request, response);
        return;//from  www  . ja v  a  2s .  co m
    }

    HttpServletRequest httpRequest = (HttpServletRequest) request;

    boolean isMultipartContent = FileUpload.isMultipartContent(httpRequest);
    if (!isMultipartContent) {
        chain.doFilter(request, response);
        return;
    }

    DiskFileUpload upload = new DiskFileUpload();
    if (repositoryPath != null)
        upload.setRepositoryPath(repositoryPath);

    try {
        List list = upload.parseRequest(httpRequest);
        final Map map = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            FileItem item = (FileItem) list.get(i);
            String str = item.getString();
            if (item.isFormField())
                map.put(item.getFieldName(), new String[] { str });
            else
                httpRequest.setAttribute(item.getFieldName(), item);
        }

        chain.doFilter(new HttpServletRequestWrapper(httpRequest) {
            public Map getParameterMap() {
                return map;
            }

            public String[] getParameterValues(String name) {
                Map map = getParameterMap();
                return (String[]) map.get(name);
            }

            public String getParameter(String name) {
                String[] params = getParameterValues(name);
                if (params == null)
                    return null;
                return params[0];
            }

            public Enumeration getParameterNames() {
                Map map = getParameterMap();
                return Collections.enumeration(map.keySet());
            }
        }, response);
    } catch (FileUploadException ex) {
        ServletException servletEx = new ServletException();
        servletEx.initCause(ex);
        throw servletEx;
    }
}

From source file:ro.finsiel.eunis.admin.EUNISUploadServlet.java

/**
 * Overrides public method doPost of javax.servlet.http.HttpServlet.
 *
 * @param request//w  ww .  j  a v a2  s  .  c  o m
 *            Request object
 * @param response
 *            Response object.
 */
public void doPost(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession(false);

    sessionManager = (SessionManager) session.getAttribute("SessionManager");

    // Initialise the default settings
    try {
        BASE_DIR = getServletContext().getInitParameter(Constants.APP_HOME_INIT_PARAM);
        TEMP_DIR = BASE_DIR + getServletContext().getInitParameter("TEMP_DIR");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    List items = new ArrayList();
    boolean isMultipart = FileUpload.isMultipartContent(request);
    DiskFileUpload upload = new DiskFileUpload();

    upload.setSizeThreshold(MAX_MEM_TRESHOLD);
    upload.setSizeMax(MAX_FILE_SIZE);
    upload.setRepositoryPath(TEMP_DIR);
    try {
        items = upload.parseRequest(request);
    } catch (FileUploadException ex) {
        ex.printStackTrace();
        try {
            response.sendRedirect("related-reports-error.jsp?status=Error while interpreting request");
        } catch (IOException _ex) {
            _ex.printStackTrace();
        }
    }
    // If it's a multi-part content then it's an upload. So we process it.
    if (isMultipart) {
        int uploadType = -1;
        String description = ""; // Description of the uploaded document (used only for UPLOAD_TYPE_FILE)

        // Process the uploaded items
        for (int i = 0; i < items.size(); i++) {
            FileItem item = (FileItem) items.get(i);

            if (item.isFormField()) {
                // FORM FIELD
                String fieldName = item.getFieldName();
                String fieldValue = item.getString();

                if (null != fieldName && fieldName.equals("uploadType")) {
                    if (null != fieldValue && fieldValue.equalsIgnoreCase("file")) {
                        uploadType = UPLOAD_TYPE_FILE;
                    }
                    if (null != fieldValue && fieldValue.equalsIgnoreCase("picture")) {
                        uploadType = UPLOAD_TYPE_PICTURE;
                    }
                }
                // Id object
                if (null != fieldName && fieldName.equalsIgnoreCase("idobject")) {
                    natureObjectInfo.idObject = fieldValue;
                }
                // Description
                if (null != fieldName && fieldName.equalsIgnoreCase("description")) {
                    natureObjectInfo.description = fieldValue;
                    description = fieldValue;
                }
                // Nature object type
                if (null != fieldName && fieldName.equalsIgnoreCase("natureobjecttype")) {
                    natureObjectInfo.natureObjectType = fieldValue;
                }
            }
        }
        if (uploadType == UPLOAD_TYPE_FILE) {
            String message = "";

            if (sessionManager.isAuthenticated() && sessionManager.isUpload_reports_RIGHT()) {
                try {
                    uploadDocument(items, message, sessionManager.getUsername(), description);
                    response.sendRedirect("related-reports-upload.jsp?message=" + message);
                } catch (IOException ex) { // Thrown by sendRedirect
                    ex.printStackTrace();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    try {
                        String errorURL = "related-reports-error.jsp?status=" + ex.getMessage();

                        response.sendRedirect(errorURL); // location is a dummy param
                    } catch (IOException ioex) {
                        ioex.printStackTrace();
                    }
                }
            } else {
                message = "You must be logged in and have the 'upload files' ";
                message += "right in order to use this feature. Upload is not possible.";
                try {
                    response.sendRedirect("related-reports-error.jsp?status=" + message);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        if (uploadType == UPLOAD_TYPE_PICTURE) {
            if (sessionManager.isAuthenticated() && sessionManager.isUpload_pictures_RIGHT()) {
                try {
                    uploadPicture(items);
                    String redirectStr = "pictures-upload.jsp?operation=upload";

                    redirectStr += "&idobject=" + natureObjectInfo.idObject;
                    redirectStr += "&natureobjecttype=" + natureObjectInfo.natureObjectType;
                    redirectStr += "&filename=" + natureObjectInfo.filename;
                    redirectStr += "&message=Picture successfully loaded.";
                    response.sendRedirect(redirectStr);
                } catch (IOException ex) { // Thrown by sendRedirect
                    ex.printStackTrace();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    try {
                        response.sendRedirect(
                                "related-reports-error.jsp?status=An error ocurred during picture upload. "
                                        + ex.getMessage());
                    } catch (IOException ioex) {
                        ioex.printStackTrace();
                    }
                }
            } else {
                try {
                    response.sendRedirect(
                            "related-reports-error.jsp?status=You do not have the proper rights. Upload is not possible.");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

From source file:thinwire.render.web.WebServlet.java

private void handleUserUpload(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    HttpSession httpSession = request.getSession();
    ApplicationHolder holder = (ApplicationHolder) httpSession.getAttribute("instance");

    if (holder.app != null) {
        try {/*from   w  w w.j a  va  2  s .c om*/
            DiskFileUpload upload = new DiskFileUpload();
            upload.setSizeThreshold(1000000);
            upload.setSizeMax(-1);
            upload.setRepositoryPath("C:\\");
            List<FileItem> items = upload.parseRequest(request);

            if (items.size() > 0) {
                FileChooser.FileInfo f = null;

                synchronized (holder.app.fileList) {
                    for (FileItem fi : items) {
                        if (!fi.isFormField()) {
                            f = new FileChooser.FileInfo(fi.getName(), fi.getInputStream());
                            holder.app.fileList[0] = f;
                        }
                    }

                    holder.app.fileList.notify();
                }
            }
        } catch (FileUploadException e) {
            log.log(Level.SEVERE, null, e);
        }
    }

    response.sendRedirect("?_twr_=FileUploadPage.html");
}

From source file:uk.ac.lancs.e_science.fileUpload.UploadFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    if (!(request instanceof HttpServletRequest)) {
        chain.doFilter(request, response);
        return;/* w w w . j av a2 s .  co  m*/
    }

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String contentLength = httpRequest.getHeader("Content-Length");
    try {
        if (sizeMax != -1 && contentLength != null && Long.parseLong(contentLength) > sizeMax) {
            ServletException servletEx = new ServletException("Uploaded file size excess maximun legal");
            throw servletEx;
        }
    } catch (NumberFormatException e) {
        e.printStackTrace();
        //nothing
    }

    boolean isMultipartContent = FileUpload.isMultipartContent(httpRequest);
    if (!isMultipartContent) {
        chain.doFilter(request, response);
        return;
    }

    DiskFileUpload upload = new DiskFileUpload();
    if (repositoryPath != null)
        upload.setRepositoryPath(repositoryPath);

    try {

        // SAK-13408 - Websphere cannot properly read the request if it has already been parsed and
        // marked by the Apache Commons FileUpload library. The request needs to be buffered so that 
        // it can be reset for subsequent processing
        if ("websphere".equals(ServerConfigurationService.getString("servlet.container"))) {
            HttpServletRequest bufferedInputRequest = new BufferedHttpServletRequestWrapper(httpRequest);
            httpRequest = bufferedInputRequest;
        }

        List list = upload.parseRequest(httpRequest);

        if ("websphere".equals(ServerConfigurationService.getString("servlet.container"))) {
            httpRequest.getInputStream().reset();
        }

        final Map map = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            FileItem item = (FileItem) list.get(i);
            String str = item.getString("UTF-8");
            if (item.isFormField())
                map.put(item.getFieldName(), new String[] { str });
            else
                httpRequest.setAttribute(item.getFieldName(), item);
        }

        chain.doFilter(new HttpServletRequestWrapper(httpRequest) {
            public Map getParameterMap() {
                return map;
            }

            public String[] getParameterValues(String name) {
                Map map = getParameterMap();
                return (String[]) map.get(name);
            }

            public String getParameter(String name) {
                String[] params = getParameterValues(name);
                if (params == null)
                    return null;
                return params[0];
            }

            public Enumeration getParameterNames() {
                Map map = getParameterMap();
                return Collections.enumeration(map.keySet());
            }
        }, response);
    } catch (FileUploadException ex) {
        ServletException servletEx = new ServletException();
        servletEx.initCause(ex);
        throw servletEx;
    }
}