com.mobiaware.util.UploadHelpers.java Source code

Java tutorial

Introduction

Here is the source code for com.mobiaware.util.UploadHelpers.java

Source

/*
 * Copyright (c) 2010 mobiaware.com.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.mobiaware.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Throwables;

public final class UploadHelpers {
    private static final int THRESHOLD_SIZE = 1024 * 1024 * 1; // 1MB
    private static final int MAX_FILE_SIZE = 1024 * 1024 * 1; // 1MB
    private static final int REQUEST_SIZE = 1024 * 1024 * 1; // 1MB

    private static final String NAME = UploadHelpers.class.getSimpleName();
    private static final Logger LOG = LoggerFactory.getLogger(NAME);

    private UploadHelpers() {
        // static only
    }

    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;

        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;
    }
}