加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

(阅读笔记)FileSystemResource

(2010-11-21 16:50:00)
标签:

杂谈

分类: spring

package org.springframework.core.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;


public class FileSystemResource extends AbstractResource {

    private final File file;

    private final String path;


    //Create a new FileSystemResource from a File handle.
    public FileSystemResource(File file) {
        Assert.notNull(file, "File must not be null");
        this.file = file;
        this.path = StringUtils.cleanPath(file.getPath());
    }

    // Create a new FileSystemResource from a file path.
    public FileSystemResource(String path) {
        Assert.notNull(path, "Path must not be null");
        this.file = new File(path);
        this.path = StringUtils.cleanPath(path);
    }

    // Return the file path for this resource.
    public final String getPath() {
        return this.path;
    }


    // This implementation returns whether the underlying file exists.
    public boolean exists() {
        return this.file.exists();
    }

    // This implementation checks whether the underlying file is marked as readable
     * (and corresponds to an actual file with content, not to a directory).
    public boolean isReadable() {
        return (this.file.canRead() && !this.file.isDirectory());
    }

    //This implementation opens a FileInputStream for the underlying file.
    public InputStream getInputStream() throws IOException {
        return new FileInputStream(this.file);
    }

    // This implementation returns a URL for the underlying file.
    public URL getURL() throws IOException {
        return this.file.toURI().toURL();
    }

    //This implementation returns a URI for the underlying file.
    public URI getURI() throws IOException {
        return this.file.toURI();
    }

    // This implementation returns the underlying File reference.
    public File getFile() {
        return this.file;
    }

    // This implementation creates a FileSystemResource, applying the given path
     * relative to the path of the underlying file of this resource descriptor.
    public Resource createRelative(String relativePath) {
        String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
        return new FileSystemResource(pathToUse);
    }

    // This implementation returns the name of the file.
    public String getFilename() {
        return this.file.getName();
    }

    / This implementation returns a description that includes the absolute
     * path of the file.
    public String getDescription() {
        return "file [" + this.file.getAbsolutePath() + "]";
    }


    
    public boolean equals(Object obj) {
        return (obj == this ||
            (obj instanceof FileSystemResource && this.path.equals(((FileSystemResource) obj).path)));
    }

    
    public int hashCode() {
        return this.path.hashCode();
    }

}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有