Search in sources :

Example 1 with DiskFileItemFactory

use of net.jforum.util.legacy.commons.fileupload.disk.DiskFileItemFactory in project jforum2 by rafaelsteil.

the class WebRequestContext method handleMultipart.

/**
	 * @param superRequest HttpServletRequest
	 * @param encoding String
	 * @throws UnsupportedEncodingException
	 */
private void handleMultipart(HttpServletRequest superRequest, String encoding) throws UnsupportedEncodingException {
    String tmpPath = new StringBuffer(256).append(SystemGlobals.getApplicationPath()).append('/').append(SystemGlobals.getValue(ConfigKeys.TMP_DIR)).toString();
    File tmpDir = new File(tmpPath);
    boolean success = false;
    try {
        if (!tmpDir.exists()) {
            tmpDir.mkdirs();
            success = true;
        }
    } catch (Exception e) {
    // We won't log it because the directory
    // creation failed for some reason - a SecurityException
    // or something else. We don't care about it, as the
    // code below tries to use java.io.tmpdir
    }
    if (!success) {
        tmpPath = System.getProperty("java.io.tmpdir");
        tmpDir = new File(tmpPath);
    }
    ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory(100 * 1024, tmpDir));
    upload.setHeaderEncoding(encoding);
    try {
        List items = upload.parseRequest(superRequest);
        for (Iterator iter = items.iterator(); iter.hasNext(); ) {
            FileItem item = (FileItem) iter.next();
            if (item.isFormField()) {
                this.addParameter(item.getFieldName(), item.getString(encoding));
            } else {
                if (item.getSize() > 0) {
                    // We really don't want to call addParameter(), as 
                    // there should not be possible to have multiple
                    // values for a InputStream data
                    this.query.put(item.getFieldName(), item);
                }
            }
        }
    } catch (FileUploadException e) {
        throw new MultipartHandlingException("Error while processing multipart content: " + e);
    }
}
Also used : FileItem(net.jforum.util.legacy.commons.fileupload.FileItem) ServletFileUpload(net.jforum.util.legacy.commons.fileupload.servlet.ServletFileUpload) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) DiskFileItemFactory(net.jforum.util.legacy.commons.fileupload.disk.DiskFileItemFactory) FileUploadException(net.jforum.util.legacy.commons.fileupload.FileUploadException) IOException(java.io.IOException) MultipartHandlingException(net.jforum.exceptions.MultipartHandlingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FileUploadException(net.jforum.util.legacy.commons.fileupload.FileUploadException) MultipartHandlingException(net.jforum.exceptions.MultipartHandlingException)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 MultipartHandlingException (net.jforum.exceptions.MultipartHandlingException)1 FileItem (net.jforum.util.legacy.commons.fileupload.FileItem)1 FileUploadException (net.jforum.util.legacy.commons.fileupload.FileUploadException)1 DiskFileItemFactory (net.jforum.util.legacy.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (net.jforum.util.legacy.commons.fileupload.servlet.ServletFileUpload)1