码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC上传文件的MultipartFile源码

时间:2017-11-08 17:39:59      阅读:401      评论:0      收藏:0      [点我收藏+]

标签:ora   stream   awd   esc   content   multipart   char   als   font   

0.MultipartFile上传文件的具体实例如下:

http://blog.csdn.net/swingpyzf/article/details/20230865

1.MultipartFile接口源码如下:

public interface MultipartFile {
    String getName();

//获取文件原名(不包含路径) String getOriginalFilename();
//获取上传文件的类型 String getContentType();
boolean isEmpty(); long getSize(); byte[] getBytes() throws IOException; InputStream getInputStream() throws IOException;
//保存到一个目标文件中
void transferTo(File var1) throws IOException, IllegalStateException; }

2.MultipartFile接口的实现类CommonsMultipartFile源码如下:

public class CommonsMultipartFile implements MultipartFile, Serializable {
    protected static final Log logger = LogFactory.getLog(CommonsMultipartFile.class);
    private final FileItem fileItem;
    private final long size;

    public CommonsMultipartFile(FileItem fileItem) {
        this.fileItem = fileItem;
        this.size = this.fileItem.getSize();
    }

    public final FileItem getFileItem() {
        return this.fileItem;
    }

    public String getName() {
        return this.fileItem.getFieldName();
    }

    public String getOriginalFilename() {
        String filename = this.fileItem.getName();
        if(filename == null) {
            return "";
        } else {
            int pos = filename.lastIndexOf("/");
            if(pos == -1) {
                pos = filename.lastIndexOf("\\");
            }

            return pos != -1?filename.substring(pos + 1):filename;
        }
    }

    public String getContentType() {
        return this.fileItem.getContentType();
    }

    public boolean isEmpty() {
        return this.size == 0L;
    }

    public long getSize() {
        return this.size;
    }

    public byte[] getBytes() {
        if(!this.isAvailable()) {
            throw new IllegalStateException("File has been moved - cannot be read again");
        } else {
            byte[] bytes = this.fileItem.get();
            return bytes != null?bytes:new byte[0];
        }
    }

    public InputStream getInputStream() throws IOException {
        if(!this.isAvailable()) {
            throw new IllegalStateException("File has been moved - cannot be read again");
        } else {
            InputStream inputStream = this.fileItem.getInputStream();
            return (InputStream)(inputStream != null?inputStream:new ByteArrayInputStream(new byte[0]));
        }
    }

    public void transferTo(File dest) throws IOException, IllegalStateException {
        if(!this.isAvailable()) {
            throw new IllegalStateException("File has already been moved - cannot be transferred again");
        } else if(dest.exists() && !dest.delete()) {
            throw new IOException("Destination file [" + dest.getAbsolutePath() + "] already exists and could not be deleted");
        } else {
            try {
                this.fileItem.write(dest);
                if(logger.isDebugEnabled()) {
                    String ex = "transferred";
                    if(!this.fileItem.isInMemory()) {
                        ex = this.isAvailable()?"copied":"moved";
                    }

                    logger.debug("Multipart file \‘" + this.getName() + "\‘ with original filename [" + this.getOriginalFilename() + "], stored " + this.getStorageDescription() + ": " + ex + " to [" + dest.getAbsolutePath() + "]");
                }

            } catch (FileUploadException var3) {
                throw new IllegalStateException(var3.getMessage());
            } catch (IOException var4) {
                throw var4;
            } catch (Exception var5) {
                logger.error("Could not transfer to file", var5);
                throw new IOException("Could not transfer to file: " + var5.getMessage());
            }
        }
    }

    protected boolean isAvailable() {
        return this.fileItem.isInMemory()?true:(this.fileItem instanceof DiskFileItem?((DiskFileItem)this.fileItem).getStoreLocation().exists():this.fileItem.getSize() == this.size);
    }

    public String getStorageDescription() {
        return this.fileItem.isInMemory()?"in memory":(this.fileItem instanceof DiskFileItem?"at [" + ((DiskFileItem)this.fileItem).getStoreLocation().getAbsolutePath() + "]":"on disk");
    }
}

 3.FileItem接口,具体的api介绍如以下链接:

http://www.jb51.net/article/90331.htm

4.FileItem的实现类DiskFileItem源码如下:

public class DiskFileItem implements FileItem {
    private static final long serialVersionUID = 2237570099615271025L;
    public static final String DEFAULT_CHARSET = "ISO-8859-1";
    private static final String UID = UUID.randomUUID().toString().replace(‘-‘, ‘_‘);
    private static final AtomicInteger COUNTER = new AtomicInteger(0);
    private String fieldName;
    private final String contentType;
    private boolean isFormField;
    private final String fileName;
    private long size = -1L;
    private final int sizeThreshold;
    private final File repository;
    private byte[] cachedContent;
    private transient DeferredFileOutputStream dfos;
    private transient File tempFile;
    private File dfosFile;
    private FileItemHeaders headers;

    public DiskFileItem(String fieldName, String contentType, boolean isFormField, String fileName, int sizeThreshold, File repository) {
        this.fieldName = fieldName;
        this.contentType = contentType;
        this.isFormField = isFormField;
        this.fileName = fileName;
        this.sizeThreshold = sizeThreshold;
        this.repository = repository;
    }

    public InputStream getInputStream() throws IOException {
        if(!this.isInMemory()) {
            return new FileInputStream(this.dfos.getFile());
        } else {
            if(this.cachedContent == null) {
                this.cachedContent = this.dfos.getData();
            }

            return new ByteArrayInputStream(this.cachedContent);
        }
    }

    public String getContentType() {
        return this.contentType;
    }

    public String getCharSet() {
        ParameterParser parser = new ParameterParser();
        parser.setLowerCaseNames(true);
        Map params = parser.parse(this.getContentType(), ‘;‘);
        return (String)params.get("charset");
    }

    public String getName() {
        return Streams.checkFileName(this.fileName);
    }

    public boolean isInMemory() {
        return this.cachedContent != null?true:this.dfos.isInMemory();
    }

    public long getSize() {
        return this.size >= 0L?this.size:(this.cachedContent != null?(long)this.cachedContent.length:(this.dfos.isInMemory()?(long)this.dfos.getData().length:this.dfos.getFile().length()));
    }

    public byte[] get() {
        if(this.isInMemory()) {
            if(this.cachedContent == null) {
                this.cachedContent = this.dfos.getData();
            }

            return this.cachedContent;
        } else {
            byte[] fileData = new byte[(int)this.getSize()];
            BufferedInputStream fis = null;

            try {
                fis = new BufferedInputStream(new FileInputStream(this.dfos.getFile()));
                fis.read(fileData);
            } catch (IOException var12) {
                fileData = null;
            } finally {
                if(fis != null) {
                    try {
                        fis.close();
                    } catch (IOException var11) {
                        ;
                    }
                }

            }

            return fileData;
        }
    }

    public String getString(String charset) throws UnsupportedEncodingException {
        return new String(this.get(), charset);
    }

    public String getString() {
        byte[] rawdata = this.get();
        String charset = this.getCharSet();
        if(charset == null) {
            charset = "ISO-8859-1";
        }

        try {
            return new String(rawdata, charset);
        } catch (UnsupportedEncodingException var4) {
            return new String(rawdata);
        }
    }

    public void write(File file) throws Exception {
        if(this.isInMemory()) {
            FileOutputStream outputFile = null;

            try {
                outputFile = new FileOutputStream(file);
                outputFile.write(this.get());
            } finally {
                if(outputFile != null) {
                    outputFile.close();
                }

            }
        } else {
            File outputFile1 = this.getStoreLocation();
            if(outputFile1 == null) {
                throw new FileUploadException("Cannot write uploaded file to disk!");
            }

            this.size = outputFile1.length();
            if(!outputFile1.renameTo(file)) {
                BufferedInputStream in = null;
                BufferedOutputStream out = null;

                try {
                    in = new BufferedInputStream(new FileInputStream(outputFile1));
                    out = new BufferedOutputStream(new FileOutputStream(file));
                    IOUtils.copy(in, out);
                } finally {
                    if(in != null) {
                        try {
                            in.close();
                        } catch (IOException var19) {
                            ;
                        }
                    }

                    if(out != null) {
                        try {
                            out.close();
                        } catch (IOException var18) {
                            ;
                        }
                    }

                }
            }
        }

    }

    public void delete() {
        this.cachedContent = null;
        File outputFile = this.getStoreLocation();
        if(outputFile != null && outputFile.exists()) {
            outputFile.delete();
        }

    }

    public String getFieldName() {
        return this.fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public boolean isFormField() {
        return this.isFormField;
    }

    public void setFormField(boolean state) {
        this.isFormField = state;
    }

    public OutputStream getOutputStream() throws IOException {
        if(this.dfos == null) {
            File outputFile = this.getTempFile();
            this.dfos = new DeferredFileOutputStream(this.sizeThreshold, outputFile);
        }

        return this.dfos;
    }

    public File getStoreLocation() {
        return this.dfos == null?null:this.dfos.getFile();
    }

    protected void finalize() {
        File outputFile = this.dfos.getFile();
        if(outputFile != null && outputFile.exists()) {
            outputFile.delete();
        }

    }

    protected File getTempFile() {
        if(this.tempFile == null) {
            File tempDir = this.repository;
            if(tempDir == null) {
                tempDir = new File(System.getProperty("java.io.tmpdir"));
            }

            String tempFileName = String.format("upload_%s_%s.tmp", new Object[]{UID, getUniqueId()});
            this.tempFile = new File(tempDir, tempFileName);
        }

        return this.tempFile;
    }

    private static String getUniqueId() {
        int limit = 100000000;
        int current = COUNTER.getAndIncrement();
        String id = Integer.toString(current);
        if(current < 100000000) {
            id = ("00000000" + id).substring(id.length());
        }

        return id;
    }

    public String toString() {
        return String.format("name=%s, StoreLocation=%s, size=%s bytes, isFormField=%s, FieldName=%s", new Object[]{this.getName(), this.getStoreLocation(), Long.valueOf(this.getSize()), Boolean.valueOf(this.isFormField()), this.getFieldName()});
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        if(this.dfos.isInMemory()) {
            this.cachedContent = this.get();
        } else {
            this.cachedContent = null;
            this.dfosFile = this.dfos.getFile();
        }

        out.defaultWriteObject();
    }

    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        in.defaultReadObject();
        if(this.repository != null) {
            if(!this.repository.isDirectory()) {
                throw new IOException(String.format("The repository [%s] is not a directory", new Object[]{this.repository.getAbsolutePath()}));
            }

            if(this.repository.getPath().contains("\u0000")) {
                throw new IOException(String.format("The repository [%s] contains a null character", new Object[]{this.repository.getPath()}));
            }
        }

        OutputStream output = this.getOutputStream();
        if(this.cachedContent != null) {
            output.write(this.cachedContent);
        } else {
            FileInputStream input = new FileInputStream(this.dfosFile);
            IOUtils.copy(input, output);
            this.dfosFile.delete();
            this.dfosFile = null;
        }

        output.close();
        this.cachedContent = null;
    }

    public FileItemHeaders getHeaders() {
        return this.headers;
    }

    public void setHeaders(FileItemHeaders pHeaders) {
        this.headers = pHeaders;
    }
}

 

SpringMVC上传文件的MultipartFile源码

标签:ora   stream   awd   esc   content   multipart   char   als   font   

原文地址:http://www.cnblogs.com/expiator/p/7804336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!