Search in sources :

Example 1 with FileUploadException

use of net.jforum.util.legacy.commons.fileupload.FileUploadException 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)

Example 2 with FileUploadException

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

the class DiskFileItem method write.

/**
     * A convenience method to write an uploaded item to disk. The client code
     * is not concerned with whether or not the item is stored in memory, or on
     * disk in a temporary location. They just want to write the uploaded item
     * to a file.
     * <p>
     * This implementation first attempts to rename the uploaded item to the
     * specified destination file, if the item was originally written to disk.
     * Otherwise, the data will be copied to the specified file.
     * <p>
     * This method is only guaranteed to work <em>once</em>, the first time it
     * is invoked for a particular item. This is because, in the event that the
     * method renames a temporary file, that file will no longer be available
     * to copy or rename again at a later time.
     *
     * @param file The <code>File</code> into which the uploaded item should
     *             be stored.
     *
     * @exception Exception if an error occurs.
     */
public void write(File file) throws Exception {
    if (isInMemory()) {
        FileOutputStream fout = null;
        try {
            fout = new FileOutputStream(file);
            fout.write(get());
        } finally {
            if (fout != null) {
                fout.close();
            }
        }
    } else {
        File outputFile = getStoreLocation();
        if (outputFile != null) {
            /*
                 * The uploaded file is being stored on disk
                 * in a temporary location so move it to the
                 * desired file.
                 */
            if (!outputFile.renameTo(file)) {
                BufferedInputStream in = null;
                BufferedOutputStream out = null;
                try {
                    in = new BufferedInputStream(new FileInputStream(outputFile));
                    out = new BufferedOutputStream(new FileOutputStream(file));
                    byte[] bytes = new byte[WRITE_BUFFER_SIZE];
                    int s = 0;
                    while ((s = in.read(bytes)) != -1) {
                        out.write(bytes, 0, s);
                    }
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                        // ignore
                        }
                    }
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException e) {
                        // ignore
                        }
                    }
                }
            }
        } else {
            /*
                 * For whatever reason we cannot write the
                 * file to disk.
                 */
            throw new FileUploadException("Cannot write uploaded file to disk!");
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) DeferredFileOutputStream(org.apache.commons.io.output.DeferredFileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream) FileUploadException(net.jforum.util.legacy.commons.fileupload.FileUploadException)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 FileUploadException (net.jforum.util.legacy.commons.fileupload.FileUploadException)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)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 DiskFileItemFactory (net.jforum.util.legacy.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (net.jforum.util.legacy.commons.fileupload.servlet.ServletFileUpload)1 DeferredFileOutputStream (org.apache.commons.io.output.DeferredFileOutputStream)1