Example usage for org.apache.commons.fileupload RequestContext getCharacterEncoding

List of usage examples for org.apache.commons.fileupload RequestContext getCharacterEncoding

Introduction

In this page you can find the example usage for org.apache.commons.fileupload RequestContext getCharacterEncoding.

Prototype

String getCharacterEncoding();

Source Link

Document

Retrieve the character encoding for the request.

Usage

From source file:fr.aliasource.webmail.server.UploadAttachmentsImpl.java

@SuppressWarnings("rawtypes")
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    RequestContext ctx = new ServletRequestContext(req);
    String enc = ctx.getCharacterEncoding();
    logger.warn("received encoding is " + enc);
    if (enc == null) {
        enc = "utf-8";
    }/*from  w w w  . j  a v a 2 s.  c  o m*/
    IAccount account = (IAccount) req.getSession().getAttribute("account");

    if (account == null) {
        resp.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    DiskFileItemFactory factory = new DiskFileItemFactory(100 * 1024,
            new File(System.getProperty("java.io.tmpdir")));
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax(20 * 1024 * 1024);

    List items = null;
    try {
        items = upload.parseRequest(req);
    } catch (FileUploadException e1) {
        logger.error("upload exception", e1);
        return;
    }

    // Process the uploaded items
    String id = null;
    Iterator iter = items.iterator();
    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();

        if (!item.isFormField()) {
            id = item.getFieldName();
            String fileName = removePathElementsFromFilename(item.getName());
            logger.warn("FileItem: " + item);
            long size = item.getSize();
            logger.warn("pushing upload of " + fileName + " to backend for " + account.getLogin() + "@"
                    + account.getDomain() + " size: " + size + ").");
            AttachmentMetadata meta = new AttachmentMetadata();
            meta.setFileName(fileName);
            meta.setSize(size);
            meta.setMime(item.getContentType());
            try {
                account.uploadAttachement(id, meta, item.getInputStream());
            } catch (Exception e) {
                logger.error("Cannot write uploaded file to disk");
            }
        }
    }
}

From source file:com.alibaba.citrus.service.upload.impl.cfu.ServletFileUpload.java

@Override
public List<FileItem> parseRequest(RequestContext ctx) throws FileUploadException {
    List<FileItem> items = super.parseRequest(ctx);
    String charset = ctx.getCharacterEncoding();

    for (FileItem fileItem : items) {
        if (fileItem instanceof AbstractFileItem) {
            ((AbstractFileItem) fileItem).setCharset(charset);
        }/*from w  ww . j av  a 2 s .  c  o  m*/
    }

    return items;
}

From source file:org.seasar.cubby.controller.impl.MultipartRequestParser.java

@SuppressWarnings("unchecked")
Map<String, Object[]> getMultipartParameterMap(final FileUpload fileUpload,
        final RequestContext requestContext) {
    try {/*  w w w  . j ava 2s .c o  m*/
        final String encoding = requestContext.getCharacterEncoding();
        fileUpload.setHeaderEncoding(encoding);
        final List<FileItem> items = fileUpload.parseRequest(requestContext);

        // Field????
        final Map<String, Object[]> parameterMap = toParameterMap(encoding, items);

        return parameterMap;
    } catch (final FileUploadException e) {
        final String messageCode;
        final Object[] args;
        if (e instanceof SizeLimitExceededException) {
            final SizeLimitExceededException sle = (SizeLimitExceededException) e;
            messageCode = "ECUB0202";
            args = new Object[] { sle.getPermittedSize(), sle.getActualSize() };
        } else {
            messageCode = "ECUB0201";
            args = new Object[] { e };
        }
        throw new RequestParseException(format(messageCode, args), e);
    } catch (final IOException e) {
        throw new RequestParseException(e);
    }
}

From source file:org.tinygroup.weblayer.webcontext.parser.impl.ServletFileUpload.java

public List<?/* FileItem */> parseRequest(RequestContext ctx) throws FileUploadException {
    FileItemFactory itemFactory = getFileItemFactory();
    FileItemFactoryWrapper wrapper = (FileItemFactoryWrapper) itemFactory;
    FileItemFactory itemWrapperFactory = wrapper.getFileItemFactory();
    if (itemWrapperFactory instanceof DiskFileItemFactory) {//
        DiskFileItemFactory factory = (DiskFileItemFactory) itemWrapperFactory;
        factory.setRequest(((ServletRequestContext) ctx).getRequest());
    }/*w  ww  . j a  va  2 s .co m*/
    @SuppressWarnings("unchecked")
    List<FileItem> items = super.parseRequest(ctx);
    String charset = ctx.getCharacterEncoding();
    for (FileItem fileItem : items) {
        if (fileItem instanceof AbstractFileItem) {
            ((AbstractFileItem) fileItem).setCharset(charset);
        }
        if (fileItem instanceof TinyFileItem) {
            TinyFileItem tinyFileItem = (TinyFileItem) fileItem;
            tinyFileItem.setCharset(charset);
            tinyFileItem.storage();//
        }
    }
    return items;
}