1 14 package org.wings.resource; 15 16 import org.wings.StaticResource; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 23 27 public class FileResource extends StaticResource { 28 29 private final File file; 30 31 public FileResource(String name) 32 throws IOException { 33 this(new File (name)); 34 } 35 36 public FileResource(File file) { 37 this(file, null, "unknown"); 38 } 39 40 public FileResource(File file, String ext, String mt) { 41 super(ext, mt); 42 this.file = file; 43 if (extension == null) { 44 int dotIndex = file.getName().lastIndexOf('.'); 45 if (dotIndex > -1) { 46 extension = file.getName().substring(dotIndex + 1); 47 } 48 } 49 try { 50 size = (int) file.length(); 51 } catch (SecurityException ignore) { 52 } 53 } 54 55 public String toString() { 56 return getId() + (file != null ? " " + file.getName() : ""); 57 } 58 59 public final File getFile() { 60 return file; 61 } 62 63 protected final InputStream getResourceStream() throws IOException { 64 return new FileInputStream (file); 65 } 66 } 67 | Popular Tags |