Example usage for org.apache.commons.fileupload DefaultFileItem write

List of usage examples for org.apache.commons.fileupload DefaultFileItem write

Introduction

In this page you can find the example usage for org.apache.commons.fileupload DefaultFileItem write.

Prototype

public void write(File file) throws Exception 

Source Link

Document

A convenience method to write an uploaded item to disk.

Usage

From source file:ca.myewb.frame.PostParamWrapper.java

private File safeFileReplace(File file, DefaultFileItem item) throws Exception {

    File temp = new File(file.getPath() + file.getName() + ".tmp");

    //If the file exists, rename it .tmp
    if (file.exists()) {
        file.renameTo(temp);//  w  w w . j  a  v a 2  s  .  com
        log.info("Old file renamed to " + file.getPath());
    }

    item.write(file); //write the file

    log.info("New file saved as " + file.getPath());

    //delete the temporary file
    if (temp.exists()) {
        temp.delete();
        log.info("Temporary file deleted from " + temp.getPath());
    }

    return file;
}

From source file:ca.myewb.frame.PostParamWrapper.java

public boolean saveJpeg(String name, int userid) throws Exception {
    DefaultFileItem item = ((DefaultFileItem) fileParams.get(name));

    if (item.getName().equals("")) {
        return false;
    }//  w  ww. j  a  v  a 2 s .c  om

    String userIdStr = Integer.toString(userid);
    String userPicturesDir = Helpers.getUserPicturesDir();
    String fullPath = userPicturesDir + "/fullsize/" + userIdStr + ".jpg";
    String thumbPath = userPicturesDir + "/thumbs/" + userIdStr + ".jpg";
    String minithumbPath = userPicturesDir + "/minithumbs/" + userIdStr + ".jpg";
    String screenPath = userPicturesDir + "/screensize/" + userIdStr + ".jpg";

    File file = new File(fullPath);
    File thumbnail = new File(thumbPath);
    File minithumbnail = new File(minithumbPath);
    File screensize = new File(screenPath);

    try {
        file.delete();
        item.write(file);

        String command = "";

        thumbnail.delete();
        command = "convert -thumbnail 200x200 -quality 100 " + fullPath + " " + thumbPath;
        Runtime.getRuntime().exec(command).waitFor();
        if (!thumbnail.exists()) {
            throw new Exception("no file: " + thumbPath);
        }

        minithumbnail.delete();
        command = "convert -thumbnail 75x -quality 100 " + fullPath + " " + minithumbPath;
        Runtime.getRuntime().exec(command).waitFor();
        if (!minithumbnail.exists()) {
            throw new Exception("no file: " + minithumbPath);
        }

        screensize.delete();
        command = "convert -thumbnail 800x600 -quality 100 " + fullPath + " " + screenPath;
        Runtime.getRuntime().exec(command).waitFor();
        if (!screensize.exists()) {
            throw new Exception("no file: " + screenPath);
        }

        return true;
    } catch (Exception e) {
        Logger.getLogger(this.getClass()).error("jpeg error: " + e, e);

        file.delete();
        thumbnail.delete();
        minithumbnail.delete();

        return false;
    }
}