java中File與MultipartFile互轉(zhuǎn)代碼示例
1 概述
當(dāng)我們?cè)谔幚砦募蟼鞯墓δ軙r(shí),通常會(huì)使用MultipartFile對(duì)象來(lái)表示上傳的文件數(shù)據(jù)。然而,有時(shí)候我們可能已經(jīng)有了一個(gè)File對(duì)象,而不是MultipartFile對(duì)象,需要將File對(duì)象轉(zhuǎn)換為MultipartFile對(duì)象進(jìn)行進(jìn)一步處理。
在Java中,F(xiàn)ile對(duì)象表示文件在本地文件系統(tǒng)中的引用,而MultipartFile對(duì)象是Spring框架提供的用于處理文件上傳的接口。MultipartFile接口提供了許多有用的方法,例如獲取文件名、獲取文件內(nèi)容、獲取文件大小等。
2 代碼示例
2.1 引入依賴(lài)
<!--File轉(zhuǎn)MultipartFile需要test包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.9.RELEASE</version> <scope>compile</scope> </dependency>
2.2 MultipartFile轉(zhuǎn)File
public static File convert(MultipartFile file) throws IOException { File convFile = new File(file.getOriginalFilename()); convFile.createNewFile(); FileOutputStream fos = new FileOutputStream(convFile); fos.write(file.getBytes()); fos.close(); return convFile; }
2.3 File轉(zhuǎn)MultipartFile
//file 轉(zhuǎn)換為 MultipartFile private MultipartFile getMulFileByPath(String filePath) { FileItemFactory factory = new DiskFileItemFactory(16, null); String textFieldName = "textField"; int num = filePath.lastIndexOf("."); String extFile = filePath.substring(num); FileItem item = factory.createItem(textFieldName, "text/plain", true, "MyFileName" + extFile); File newfile = new File(filePath); int bytesRead = 0; byte[] buffer = new byte[8192]; try { FileInputStream fis = new FileInputStream(newfile); OutputStream os = item.getOutputStream(); while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } MultipartFile mfile = new CommonsMultipartFile(item); return mfile; }
總結(jié)
到此這篇關(guān)于java中File與MultipartFile互轉(zhuǎn)的文章就介紹到這了,更多相關(guān)java File與MultipartFile互轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot2.6.x默認(rèn)禁用循環(huán)依賴(lài)后的問(wèn)題解決
由于SpringBoot從底層逐漸引導(dǎo)開(kāi)發(fā)者書(shū)寫(xiě)規(guī)范的代碼,同時(shí)也是個(gè)憂傷的消息,循環(huán)依賴(lài)的應(yīng)用場(chǎng)景實(shí)在是太廣泛了,所以SpringBoot 2.6.x不推薦使用循環(huán)依賴(lài),本文給大家說(shuō)下SpringBoot2.6.x默認(rèn)禁用循環(huán)依賴(lài)后的應(yīng)對(duì)策略,感興趣的朋友一起看看吧2022-02-02java實(shí)現(xiàn)找出兩個(gè)文件中相同的單詞(兩種方法)
這篇文章主要介紹了java實(shí)現(xiàn)找出兩個(gè)文件中相同的單詞(兩種方法),需要的朋友可以參考下2020-08-08關(guān)于SpringCloud?Ribbon替換輪詢(xún)算法問(wèn)題
Spring?Cloud?Ribbon是基于Netlix?Ribbon實(shí)現(xiàn)的一套客戶(hù)端負(fù)載均衡的工具。接下來(lái)通過(guò)本文給大家介紹SpringCloud?Ribbon替換輪詢(xún)算法問(wèn)題,需要的朋友可以參考下2022-01-01spring的構(gòu)造函數(shù)注入屬性@ConstructorBinding用法
這篇文章主要介紹了關(guān)于spring的構(gòu)造函數(shù)注入屬性@ConstructorBinding用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java創(chuàng)建線程池的方式實(shí)現(xiàn)
本文主要介紹了創(chuàng)建線程池的方式,包括三種方式,第一種是使用Executors框架,第二種是使用ThreadPoolExecutor,第三種是使用ForkJoinPool,感興趣的可以了解一下2024-12-12Springsession nginx反向代理集成過(guò)程
這篇文章主要介紹了Springsession nginx反向代理集成過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04