views:

30

answers:

0

Hi everyone, I want to save the uploaded file with Spring WebFlow in a specific folder, and I made the following.

Here is the model:

public class FileUploadHandler implements Action {

private static final long serialVersionUID = -8801901733860111693L;

static Logger LOG = Logger.getLogger(UserServiceImpl.class);

private transient MultipartFile file;

final int maxSmall = 32; // Maximale Gr��e des Benutzerfotos f�r Tabellensicht
final int maxBig = 140; // Maximale Gr��e des Benutzerfotos f�r Benutzerinfo

public void processFile(RequestContext context, String userId) throws IllegalStateException, IOException {

    ServletExternalContext externalContext = (ServletExternalContext) context.getExternalContext();

    final String destinationDir =  externalContext.getContextPath() + ("/style/img/userpics/");
    String strUserId = userId;

    File destinationBig = new File(destinationDir + "/" + strUserId + "b.jpg");
    File destinationSmall = new File(destinationDir + "/" + strUserId + "s.jpg");

    // �bertragenes Bild nach "destinationSmall" speichern
    file.transferTo(destinationSmall);

    // Bild kopieren ("destinationSmall" nach "destinationBig")
    FileChannel inChannel = new FileInputStream(destinationSmall).getChannel();
    FileChannel outChannel = new FileOutputStream(destinationBig).getChannel();
    try {
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null)
            inChannel.close();
        if (outChannel != null)
            outChannel.close();
    }

    // Gr��e der beiden Bilder �ndern und speichern
    resizeUserPic(destinationBig, maxBig);
    resizeUserPic(destinationSmall, maxSmall);

    return;

}

private void resizeUserPic(File picture, int maxPixel) throws IOException {

    int newWidth, newHeight = 0;

    try {
        Image img = new ImageIcon(ImageIO.read(picture)).getImage();
        int picHeight = img.getHeight(null);
        int picWidth = img.getWidth(null);
        float ratio = (float) picHeight / (float) picWidth;
        LOG.debug("B x H (alt) = " + picWidth + " x " + picHeight + "(Ratio: " + ratio + ")");

        if (ratio > 1) {
            newHeight = maxPixel;
            newWidth = (int) (maxPixel / ratio);
        } else {
            newHeight = (int) (maxPixel * ratio);
            newWidth = maxPixel;
        }
        LOG.debug("B x H (neu) = " + newWidth + " x " + newHeight);

        Image scaledImage = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
        BufferedImage outImg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
        Graphics g = outImg.getGraphics();
        g.drawImage(scaledImage, 0, 0, null);
        g.dispose();

        Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();

        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(0.5f);
        // Quality: integer between 0 and 1, 1 specifies minimum compression
        // and maximum quality

        File file = new File(picture.getAbsolutePath());
        FileImageOutputStream output = new FileImageOutputStream(file);
        writer.setOutput(output);
        IIOImage image = new IIOImage(outImg, null, null);
        writer.write(null, image, iwp); // iwp statt , , null

    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void setFile(MultipartFile file) {
    this.file = file;
    }

@Override
public Event execute(RequestContext arg0) throws Exception {
    // TODO Auto-generated method stub
    return null;
}

}

Here the action:

public class FileUploadAction extends AbstractAction implements Serializable{

private static final long serialVersionUID = -8801901733860111694L;

public Event doExecute(RequestContext context) throws Exception {
    MultipartFile file = context.getRequestParameters().getRequiredMultipartFile("file");
    if (file.getSize() > 0) {
        // data was uploaded
        context.getFlashScope().put("file", new String(file.getOriginalFilename()));
        context.getFlashScope().put("size", (long)(file.getSize()));
        //context.getFlashScope().put("file", new String(file.getBytes()));
        return success();
    } else {
        return error();
    } 
}

}

But the uploaded file can not be saved in the specified path(/style/img/userpics/).

What am I doing wrong?

Thanks for your help.