| 1 | |
package org.webslinger.commons.fileupload; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.InputStream; |
| 5 | |
import java.io.IOException; |
| 6 | |
import java.io.OutputStream; |
| 7 | |
import java.util.ArrayList; |
| 8 | |
import java.util.Enumeration; |
| 9 | |
import java.util.HashMap; |
| 10 | |
import java.util.List; |
| 11 | |
import java.util.Map; |
| 12 | |
import javax.servlet.http.HttpServletRequest; |
| 13 | |
|
| 14 | |
import org.apache.commons.fileupload.FileItem; |
| 15 | |
import org.apache.commons.fileupload.FileItemFactory; |
| 16 | |
import org.apache.commons.fileupload.FileUploadException; |
| 17 | |
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
| 18 | |
import org.apache.commons.fileupload.servlet.ServletFileUpload; |
| 19 | |
|
| 20 | |
import org.apache.commons.vfs.FileObject; |
| 21 | |
|
| 22 | |
import org.webslinger.commons.vfs.VFSUtil; |
| 23 | |
import org.webslinger.io.Charsets; |
| 24 | |
import org.webslinger.io.IOUtil; |
| 25 | |
|
| 26 | |
public abstract class AbstractCommonsVfsFileItem implements FileItem { |
| 27 | |
private final String name; |
| 28 | |
|
| 29 | |
private boolean formField; |
| 30 | |
private String fieldName; |
| 31 | |
|
| 32 | 0 | protected AbstractCommonsVfsFileItem(String name) { |
| 33 | 0 | this.name = name; |
| 34 | 0 | } |
| 35 | |
|
| 36 | |
public abstract FileObject getFile() throws IOException; |
| 37 | |
protected void finalize() { |
| 38 | 0 | delete(); |
| 39 | 0 | } |
| 40 | |
|
| 41 | |
public byte[] get() { |
| 42 | |
try { |
| 43 | 0 | return VFSUtil.getBytes(getFile()); |
| 44 | 0 | } catch (IOException e) { |
| 45 | 0 | throw (IllegalArgumentException) new IllegalArgumentException(e.getMessage()).initCause(e); |
| 46 | |
} |
| 47 | |
} |
| 48 | |
|
| 49 | |
public String getFieldName() { |
| 50 | 0 | return fieldName; |
| 51 | |
} |
| 52 | |
|
| 53 | |
public InputStream getInputStream() throws IOException { |
| 54 | 0 | return getFile().getContent().getInputStream(); |
| 55 | |
} |
| 56 | |
|
| 57 | |
public String getName() { |
| 58 | 0 | return name; |
| 59 | |
} |
| 60 | |
|
| 61 | |
public OutputStream getOutputStream() throws IOException { |
| 62 | 0 | OutputStream out = getFile().getContent().getOutputStream(); |
| 63 | 0 | getFile().getContent().setAttribute("content-type", getContentType()); |
| 64 | 0 | return out; |
| 65 | |
} |
| 66 | |
|
| 67 | |
public long getSize() { |
| 68 | |
try { |
| 69 | 0 | return getFile().getContent().getSize(); |
| 70 | 0 | } catch (IOException e) { |
| 71 | 0 | throw (IllegalArgumentException) new IllegalArgumentException(e.getMessage()).initCause(e); |
| 72 | |
} |
| 73 | |
} |
| 74 | |
|
| 75 | |
public String getString() { |
| 76 | |
try { |
| 77 | 0 | return VFSUtil.getString(getFile()); |
| 78 | 0 | } catch (IOException e) { |
| 79 | 0 | throw (IllegalArgumentException) new IllegalArgumentException(e.getMessage()).initCause(e); |
| 80 | |
} |
| 81 | |
} |
| 82 | |
|
| 83 | |
public String getString(String encoding) { |
| 84 | |
try { |
| 85 | 0 | return VFSUtil.getString(getFile(), encoding); |
| 86 | 0 | } catch (IOException e) { |
| 87 | 0 | throw (IllegalArgumentException) new IllegalArgumentException(e.getMessage()).initCause(e); |
| 88 | |
} |
| 89 | |
} |
| 90 | |
|
| 91 | |
public boolean isFormField() { |
| 92 | 0 | return formField; |
| 93 | |
} |
| 94 | |
|
| 95 | |
public boolean isInMemory() { |
| 96 | 0 | return false; |
| 97 | |
} |
| 98 | |
|
| 99 | |
public void setFieldName(String fieldName) { |
| 100 | 0 | this.fieldName = fieldName; |
| 101 | 0 | } |
| 102 | |
|
| 103 | |
public void setFormField(boolean formField) { |
| 104 | 0 | this.formField = formField; |
| 105 | 0 | } |
| 106 | |
|
| 107 | |
public void write(File file) throws IOException { |
| 108 | 0 | IOUtil.copy(getFile().getContent().getInputStream(), true, file); |
| 109 | 0 | } |
| 110 | |
|
| 111 | |
public String toString() { |
| 112 | 0 | StringBuilder builder = new StringBuilder(); |
| 113 | 0 | builder.append("{field=") |
| 114 | |
.append(getFieldName()) |
| 115 | |
.append(", isFormField=") |
| 116 | |
.append(isFormField()) |
| 117 | |
.append(", name=") |
| 118 | |
.append(getName()) |
| 119 | |
.append(", size=") |
| 120 | |
.append(getSize()) |
| 121 | |
.append("};"); |
| 122 | 0 | return builder.toString(); |
| 123 | |
} |
| 124 | |
} |