1 31 32 package org.apache.commons.httpclient.methods.multipart; 33 34 import java.io.ByteArrayInputStream ; 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 import java.io.FileNotFoundException ; 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 41 50 public class FilePartSource implements PartSource { 51 52 53 private File file = null; 54 55 56 private String fileName = null; 57 58 66 public FilePartSource(File file) throws FileNotFoundException { 67 this.file = file; 68 if (file != null) { 69 if (!file.isFile()) { 70 throw new FileNotFoundException ("File is not a normal file."); 71 } 72 if (!file.canRead()) { 73 throw new FileNotFoundException ("File is not readable."); 74 } 75 this.fileName = file.getName(); 76 } 77 } 78 79 88 public FilePartSource(String fileName, File file) 89 throws FileNotFoundException { 90 this(file); 91 if (fileName != null) { 92 this.fileName = fileName; 93 } 94 } 95 96 101 public long getLength() { 102 if (this.file != null) { 103 return this.file.length(); 104 } else { 105 return 0; 106 } 107 } 108 109 114 public String getFileName() { 115 return (fileName == null) ? "noname" : fileName; 116 } 117 118 124 public InputStream createInputStream() throws IOException { 125 if (this.file != null) { 126 return new FileInputStream (this.file); 127 } else { 128 return new ByteArrayInputStream (new byte[] {}); 129 } 130 } 131 132 } 133 | Popular Tags |