包: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);
複製代碼