Example usage for org.apache.commons.fileupload FileUploadException toString

List of usage examples for org.apache.commons.fileupload FileUploadException toString

Introduction

In this page you can find the example usage for org.apache.commons.fileupload FileUploadException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:podd.resources.services.TabImportService.java

/**
 * Creates a Tab transfer object from the Restlet Representation
 * @param entity//from ww w . ja v a2  s  .com
 * @return
 */
private Tab createTab(Representation entity) {
    Tab tab = new Tab();
    DiskFileItemFactory factory = new DiskFileItemFactory();
    factory.setSizeThreshold(0); // always write to disk
    RestletFileUpload upload = new RestletFileUpload(factory);
    try {
        List<FileItem> list = upload.parseRepresentation(entity);
        for (FileItem item : list) {
            if (item.getFieldName().equals(PARAM_TAB) && !item.isFormField()) {
                tab.setTabStream(item.getInputStream());
            }
            if (item.getFieldName().equals(PARAM_ATTACHMENT) && !item.isFormField()) {
                DiskFileItem diskFileItem = (DiskFileItem) item;

                if (_DEBUG) {
                    LOGGER.debug("isInMemory = " + diskFileItem.isInMemory() + " diskFileItem exists = "
                            + diskFileItem.getStoreLocation().exists());
                }

                // If the file is not on disk, force to disk
                File copiedFile = FileUploadHelper.writeToDisk(diskFileItem, authenticatedUser);
                if (_DEBUG) {
                    LOGGER.debug("copiedFile = " + copiedFile);
                }

                if (copiedFile != null)
                    tab.addAttachment(copiedFile.getName(), copiedFile);
            }
        }
    } catch (FileUploadException e) {
        getResponse().setStatus(SERVER_ERROR_INTERNAL);
        final String msg = "Error reading form data. ";
        errorHandler.handleException(GENERAL_MESSAGE_ID, "File Initialisation Error", msg, e);
        auditLogHelper.auditAction(ERROR, authenticatedUser, "Tab import Service: " + msg, e.toString());
    } catch (IOException e) {
        getResponse().setStatus(SERVER_ERROR_INTERNAL);
        final String msg = "Error reading file. ";
        errorHandler.handleException(GENERAL_MESSAGE_ID, "File Initalisation Error", msg, e);
        auditLogHelper.auditAction(ERROR, authenticatedUser, "Tab import Service: " + msg, e.toString());

    }
    return tab;
}

From source file:Servlet.ServImportacaoMovVisual.java

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request//from w w  w .j  a v  a2s.  c o  m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    HttpSession sessao = request.getSession();
    String expira = (String) sessao.getAttribute("empresa");
    if (expira == null) {
        response.sendRedirect("/index.jsp?msgLog=3");
    } else {
        try {

            String nomeBD = (String) sessao.getAttribute("empresa");
            boolean isMultiPart = FileUpload.isMultipartContent(request);

            if (isMultiPart) {

                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                upload.setSizeMax(1024 * 1024 * 2);

                String vData1 = "", vData2 = "";
                String vCaminho = "";
                int idUsuario = 0;
                Date data1 = null, data2 = null;

                List items = upload.parseRequest(request);

                Iterator iter = items.iterator();

                FileItem itemImg = (FileItem) iter.next();

                while (iter.hasNext()) {

                    FileItem item = (FileItem) iter.next();

                    if (item.isFormField()) {
                        if (item.getFieldName().equals("data")) {
                            vData1 = item.getString();
                            data1 = FormatarData.formataRetornaDate(vData1);
                        } else if (item.getFieldName().equals("data2")) {
                            vData2 = item.getString();
                            data2 = FormatarData.formataRetornaDate(vData2);
                        } else if (item.getFieldName().equals("idUsuario")) {
                            idUsuario = Integer.parseInt(item.getString());
                        }

                    }
                    if (!item.isFormField()) {
                        if (item.getName().length() > 0) {
                            itemImg = item;
                        }
                    }
                }

                vCaminho = "";//inserirDiretorio(itemImg);

                if (vCaminho.equals("")) {
                    sessao.setAttribute("msg", "Escolha um arquivo para importacao!");
                } else {
                    if (data1 != null && data2 != null && (data1.before(data2) || data1.equals(data2))
                            && Util.SomaData.diferencaEmDias(data1, data2) <= 31) {
                        String mensagem = importaMovimento(itemImg, data1, nomeBD, idUsuario);
                        sessao.setAttribute("msg", mensagem);
                    } else {
                        sessao.setAttribute("msg", "Data incorreta, ou periodo maior que 1 mes!");
                    }
                }
            }

        } catch (FileUploadBase.SizeLimitExceededException e) {
            sessao.setAttribute("msg", "Tamanho Mximo do Arquivo Excedido!");
        } catch (FileUploadException ex) {
            int idErro = ContrErroLog.inserir("HOITO - ServImportacaoMov", "FileUploadException", null,
                    ex.toString());
            sessao.setAttribute("msg", "SYSTEM ERROR N: " + idErro + "; Ocorreu um erro inesperado!");
        } catch (Exception ex) {
            int idErro = ContrErroLog.inserir("HOITO - ServImportacaoMov", "Exception", null, ex.toString());
            sessao.setAttribute("msg", "SYSTEM ERROR N: " + idErro + "; Ocorreu um erro inesperado!");
        }

        //response.sendRedirect("./Agencia/Importacao/imp_movimento_visual.jsp");
        response.sendRedirect(request.getHeader("referer"));
    }
}