1 16 17 package org.springframework.core.io; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.net.URL ; 24 25 import org.springframework.util.Assert; 26 import org.springframework.util.ResourceUtils; 27 import org.springframework.util.StringUtils; 28 29 37 public class FileSystemResource extends AbstractResource { 38 39 private final File file; 40 41 private final String path; 42 43 44 48 public FileSystemResource(File file) { 49 Assert.notNull(file, "File must not be null"); 50 this.file = file; 51 this.path = StringUtils.cleanPath(file.getPath()); 52 } 53 54 58 public FileSystemResource(String path) { 59 Assert.notNull(path, "Path must not be null"); 60 this.file = new File (path); 61 this.path = StringUtils.cleanPath(path); 62 } 63 64 67 public final String getPath() { 68 return this.path; 69 } 70 71 72 76 public boolean exists() { 77 return this.file.exists(); 78 } 79 80 84 public InputStream getInputStream() throws IOException { 85 return new FileInputStream (this.file); 86 } 87 88 92 public URL getURL() throws IOException { 93 return new URL (ResourceUtils.FILE_URL_PREFIX + this.file.getAbsolutePath()); 94 } 95 96 99 public File getFile() { 100 return file; 101 } 102 103 108 public Resource createRelative(String relativePath) { 109 String pathToUse = StringUtils.applyRelativePath(this.path, relativePath); 110 return new FileSystemResource(pathToUse); 111 } 112 113 117 public String getFilename() { 118 return this.file.getName(); 119 } 120 121 126 public String getDescription() { 127 return "file [" + this.file.getAbsolutePath() + "]"; 128 } 129 130 131 134 public boolean equals(Object obj) { 135 return (obj == this || 136 (obj instanceof FileSystemResource && this.path.equals(((FileSystemResource) obj).path))); 137 } 138 139 142 public int hashCode() { 143 return this.path.hashCode(); 144 } 145 146 } 147 | Popular Tags |