01: // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
02:
03: package jodd.servlet.upload.impl;
04:
05: import jodd.util.SystemUtil;
06: import jodd.servlet.upload.FileUpload;
07: import jodd.servlet.upload.MultipartRequestInputStream;
08: import jodd.servlet.upload.FileUploadFactory;
09:
10: import java.io.IOException;
11: import java.io.File;
12: import java.io.FileOutputStream;
13: import java.io.BufferedOutputStream;
14:
15: /**
16: * {@link FileUpload} that saves uploaded files directly to destination folder.
17: */
18: public class DiskFileUpload extends FileUpload {
19:
20: protected String destFolder;
21:
22: public DiskFileUpload(MultipartRequestInputStream input) {
23: super (input);
24: this .destFolder = SystemUtil.getTempDir();
25: }
26:
27: public DiskFileUpload(MultipartRequestInputStream input,
28: String destinationFolder) {
29: super (input);
30: this .destFolder = destinationFolder;
31: }
32:
33: /**
34: * Returns destination folder.
35: */
36: public String getDestinationFolder() {
37: return destFolder;
38: }
39:
40: /**
41: * Returns uploaded and saved file.
42: */
43: public File getFile() {
44: return file;
45: }
46:
47: protected File file;
48:
49: @Override
50: protected void processStream() throws IOException {
51: file = new File(destFolder, header.getFileName());
52: FileOutputStream fos = new FileOutputStream(file);
53: size = -1;
54: try {
55: size = input.copyAll(new BufferedOutputStream(fos));
56: } finally {
57: try {
58: fos.close();
59: } catch (Exception ex) {
60: // ignore
61: }
62: }
63: }
64:
65: // ---------------------------------------------------------------- factory
66:
67: public static class Factory implements FileUploadFactory {
68:
69: public FileUpload create(MultipartRequestInputStream input) {
70: return new DiskFileUpload(input);
71: }
72: }
73: }
|