包:org.springframework.web.multipartInterface MultipartFile
官方介绍:public interface MultipartFile A representation of an uploaded file received in a multipart request.The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storages will be cleared at the end of request processing.
译: 公共接口MultipartFile在多部分请求中接收的上传文件的表示形式。文件内容要么存储在内存中,要么临时存储在磁盘上。在这两种状况下,若是须要,用户负责将文件内容复制到会话级或持久性存储中。在请求处理结束时,将清除临时存储。
经常使用方法
boolean isEmpty() 返回上传的文件是否为空 void transferTo(File dest) 将接收到的文件传输到给定的目标文件。 复制代码
基本方法
System.out.println("以字节数组的形式返回文件的内容。");
System.out.println(file.getBytes());
System.out.println("返回文件的内容类型。");
System.out.println(file.getContentType());
System.out.println("返回一个InputStream来读取文件的内容。");
System.out.println(file.getInputStream());
System.out.println("以多部分形式返回参数的名称。");
System.out.println(file.getName());
System.out.println("返回客户机文件系统中的原始文件名。");
System.out.println(file.getOriginalFilename());
System.out.println("以字节为单位返回文件的大小");
System.out.println(file.getSize());
System.out.println("返回上传的文件是否为空,也就是说,在多部分表单中没有选择任何文件,或者选择的文件没有内容。");
System.out.println(file.isEmpty());
以字节数组的形式返回文件的内容。
[B@5a38fbe9
返回文件的内容类型。
text/plain
返回一个InputStream来读取文件的内容。
java.io.ByteArrayInputStream@5f971779
以多部分形式返回参数的名称。
file
返回客户机文件系统中的原始文件名。
新建文本文档.txt
以字节为单位返回文件的大小
331
返回上传的文件是否为空,也就是说,在多部分表单中没有选择任何文件,或者选择的文件没有内容。
false
复制代码
核心方法 void transferTo(File dest) 将接收到的文件传输到给定的目标文件。java
把上传文件中的内容,输出给指定的文件web
System.out.println("将接收到的文件传输到给定的目标文件。");
//上传文件
File file1=new File("D:\\白居易工做文件夹/tupian.png");
// 建立文件,若是父级目录没有一样建立
file1.mkdirs();
//文件写入
file.transferTo(file1);
复制代码