1:MultipartFile 轉成File
public static File multipartFileToFile(MultipartFile file ) {
File toFile = null;
try{
if(file==null||file.equals("")||file.getSize()<=0){
file = null;
}else {
InputStream ins = file.getInputStream(); //獲取MultipartFile的輸入流
String filePath = file.getOriginalFilename();
filePath = filePath.substring(filePath.lastIndexOf("/")+1);
toFile = new File(filePath); //根據老檔名建立一個File物件
OutputStream os = new FileOutputStream(file);//根據File建立一個輸出流
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead); //將輸入流寫到輸出流中
}
ins.close();
os.close();
}catch(Exception e){
//這裡進行異常處理
return toFile;
理解:首先以MultipartFile的名字建立一個File物件,然後以MultipartFile為輸入流的源頭,以新File物件為輸出流的目的地,最後進行流的讀寫即可。
2:File 轉成 MultipartFile
(1):
public static MultipartFile fileToMultipartFile( File file ) {
FileInputStream fileInput = new FileInputStream(file);
MultipartFile toMultipartFile = new MockMultipartFile("file",file.getName(),"text/plain", IOUtils.toByteArray(fileInput));
return toMultipartFile;
理解:以File為輸入流的源頭,然後將其轉成位元組陣列,最終以位元組陣列建立MultipartFile。
(2):
FileItem fileItem = createFileItem(file); //根據File建立FileItem
MultipartFile mfile = new CommonsMultipartFile(fileItem);//根據FileItem建立MultipartFile
return mfile;
private static FileItem createFileItem(File file){
FileItemFactory factory = (FileItemFactory) new DiskFileItemFactory();
String textFieldName = "textField";
int num = file.getName().lastIndexOf(".");
String extFile = file.getName().substring(num);
FileItem item = factory.createItem(textFieldName, "text/plain", true,"MyFileName" + extFile);//根據File名建立一個FileItem
try {
FileInputStream fis = new FileInputStream(file); //以File為源頭建立輸入流
OutputStream os = item.getOutputStream(); //以FileItem為目的地建立輸出流
while ((bytesRead = fis.read(buffer, 0, 8192)) != -1){
os.write(buffer, 0, bytesRead); //將輸入流寫入輸出流
fis.close();
}catch (IOException e){
//異常處理
return item;
理解:將File轉換成FileItem,最後以FileItem建立MultipartFile
1:MultipartFile 轉成File
public static File multipartFileToFile(MultipartFile file ) {
File toFile = null;
try{
if(file==null||file.equals("")||file.getSize()<=0){
file = null;
}else {
InputStream ins = file.getInputStream(); //獲取MultipartFile的輸入流
String filePath = file.getOriginalFilename();
filePath = filePath.substring(filePath.lastIndexOf("/")+1);
toFile = new File(filePath); //根據老檔名建立一個File物件
OutputStream os = new FileOutputStream(file);//根據File建立一個輸出流
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead); //將輸入流寫到輸出流中
}
ins.close();
os.close();
}
}catch(Exception e){
//這裡進行異常處理
}
return toFile;
}
理解:首先以MultipartFile的名字建立一個File物件,然後以MultipartFile為輸入流的源頭,以新File物件為輸出流的目的地,最後進行流的讀寫即可。
2:File 轉成 MultipartFile
(1):
public static MultipartFile fileToMultipartFile( File file ) {
FileInputStream fileInput = new FileInputStream(file);
MultipartFile toMultipartFile = new MockMultipartFile("file",file.getName(),"text/plain", IOUtils.toByteArray(fileInput));
return toMultipartFile;
}
理解:以File為輸入流的源頭,然後將其轉成位元組陣列,最終以位元組陣列建立MultipartFile。
(2):
public static MultipartFile fileToMultipartFile( File file ) {
FileItem fileItem = createFileItem(file); //根據File建立FileItem
MultipartFile mfile = new CommonsMultipartFile(fileItem);//根據FileItem建立MultipartFile
return mfile;
}
private static FileItem createFileItem(File file){
FileItemFactory factory = (FileItemFactory) new DiskFileItemFactory();
String textFieldName = "textField";
int num = file.getName().lastIndexOf(".");
String extFile = file.getName().substring(num);
FileItem item = factory.createItem(textFieldName, "text/plain", true,"MyFileName" + extFile);//根據File名建立一個FileItem
int bytesRead = 0;
byte[] buffer = new byte[8192];
try {
FileInputStream fis = new FileInputStream(file); //以File為源頭建立輸入流
OutputStream os = item.getOutputStream(); //以FileItem為目的地建立輸出流
while ((bytesRead = fis.read(buffer, 0, 8192)) != -1){
os.write(buffer, 0, bytesRead); //將輸入流寫入輸出流
}
os.close();
fis.close();
}catch (IOException e){
//異常處理
}
return item;
}
理解:將File轉換成FileItem,最後以FileItem建立MultipartFile