Java BufferedImage轉(zhuǎn)換為MultipartFile方式
BufferedImage轉(zhuǎn)換為MultipartFile
Java里讀取圖片或調(diào)整圖片大小可使用BufferedImage進(jìn)行操作(參考我另一篇文章Java修改圖片大小尺寸),但有時(shí)候我們需要將BufferedImage轉(zhuǎn)為MultipartFile進(jìn)行其他操作可如下轉(zhuǎn)換:
方法一
1.新建ConvertToMultipartFile類(lèi)實(shí)現(xiàn)MultipartFile接口
import org.springframework.web.multipart.MultipartFile; import java.io.*; public class ConvertToMultipartFile implements MultipartFile { private byte[] fileBytes; String name; String originalFilename; String contentType; boolean isEmpty; long size; public ConvertToMultipartFile(byte[] fileBytes, String name, String originalFilename, String contentType, long size) { this.fileBytes = fileBytes; this.name = name; this.originalFilename = originalFilename; this.contentType = contentType; this.size = size; this.isEmpty = false; } @Override public String getName() { return name; } @Override public String getOriginalFilename() { return originalFilename; } @Override public String getContentType() { return contentType; } @Override public boolean isEmpty() { return isEmpty; } @Override public long getSize() { return size; } @Override public byte[] getBytes() throws IOException { return fileBytes; } @Override public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(fileBytes); } @Override public void transferTo(File dest) throws IOException, IllegalStateException { new FileOutputStream(dest).write(fileBytes); } }
2.BufferedImage 轉(zhuǎn)換為 MultipartFile
BufferedImage 先轉(zhuǎn)為byte[ ],再通過(guò)上面的ConvertToMultipartFile類(lèi)轉(zhuǎn)為MultipartFile
try { //讀取圖片轉(zhuǎn)換為 BufferedImage BufferedImage image = ImageIO.read(new FileInputStream("F:/test/pic1.jpg")); //BufferedImage 轉(zhuǎn)化為 ByteArrayOutputStream ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", out); //ByteArrayOutputStream 轉(zhuǎn)化為 byte[] byte[] imageByte = out.toByteArray(); //將 byte[] 轉(zhuǎn)為 MultipartFile MultipartFile multipartFile = new ConvertToMultipartFile(imageByte, "newNamepic", "pic1", "jpg", imageByte.length); } catch (IOException e) { e.printStackTrace(); }
方法二
引入依賴:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.2</version> <scope>compile</scope> </dependency>
try { //讀取圖片轉(zhuǎn)換為 BufferedImage BufferedImage image = ImageIO.read(new FileInputStream("F:/test/pic1.jpg")); //調(diào)整圖片大小后的BufferedImage。resizeImage方法是調(diào)整圖片大小的可參考文章開(kāi)頭我上一篇文章 BufferedImage newImage = ImageUtils.resizeImage(image, 200, 200); //將newImage寫(xiě)入字節(jié)數(shù)組輸出流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write( newImage, "jpg", baos ); //轉(zhuǎn)換為MultipartFile MultipartFile multipartFile = new MockMultipartFile("pic1.jpg", baos.toByteArray()); } catch (IOException e) { e.printStackTrace(); }
Java數(shù)據(jù)轉(zhuǎn)存的中MultipartFile轉(zhuǎn)File
錯(cuò)誤背景
由于文件儲(chǔ)存在第三方的服務(wù)器上,所有需要講將接收到MultipartFile文件 轉(zhuǎn)換為File 然后傳輸。(Spring MVC)
通過(guò)搜索引擎 找到了以下兩種方法
均要在先spring xml中聲明。如下:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
如需添加最大,最小等范圍控制,請(qǐng)自行百度參考。
方法一:強(qiáng)轉(zhuǎn)
方法二:
CommonsMultipartFile cf = (CommonsMultipartFile)multfile; DiskFileItem fi = (DiskFileItem) cf.getFileItem(); File file = fi.getStoreLocation();
親測(cè)有效。但是后期發(fā)現(xiàn)設(shè)置的問(wèn)題 導(dǎo)致文件轉(zhuǎn)換中錯(cuò)誤,文件不可讀從而導(dǎo)致 程序拋出 is not a normal file 異常。
因?yàn)殄e(cuò)誤出現(xiàn)的隨機(jī)性很大,所以選擇用緩沖區(qū)來(lái)實(shí)現(xiàn)這個(gè)轉(zhuǎn)換即使用java 創(chuàng)建的臨時(shí)文件 使用 MultipartFile.transferto()方法 。
代碼如下:
File f = null; try { f=File.createTempFile("tmp", null); file.transferTo(f);<br> f.deleteOnExit(); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
親測(cè)有效。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中Calendar類(lèi)的一些常用方法小結(jié)
項(xiàng)目當(dāng)中,我們經(jīng)常會(huì)涉及到對(duì)時(shí)間的處理,Date類(lèi)最主要的作用就是獲得當(dāng)前時(shí)間,同時(shí)這個(gè)類(lèi)里面也具有設(shè)置時(shí)間以及一些其他的功能,但更推薦使用 Calendar 類(lèi)進(jìn)行時(shí)間和日期的處理,這篇文章主要給大家介紹了關(guān)于Java中Calendar類(lèi)的一些常用方法,需要的朋友可以參考下2021-11-11Java并發(fā)編程volatile關(guān)鍵字的作用
這篇文章主要介紹了Java并發(fā)編程volatile關(guān)鍵字的作用,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07SpringBoot 并發(fā)登錄人數(shù)控制的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot 并發(fā)登錄人數(shù)控制的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05java 多態(tài)性詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了java 多態(tài)性詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02springboot CompletableFuture并行計(jì)算及使用方法
CompletableFuture基于 Future 和 CompletionStage 接口,利用線程池、回調(diào)函數(shù)、異常處理、組合操作等機(jī)制,提供了強(qiáng)大而靈活的異步編程功能,這篇文章主要介紹了springboot CompletableFuture并行計(jì)算及使用方法,需要的朋友可以參考下2024-05-05MybatisPlus開(kāi)啟、關(guān)閉二級(jí)緩存方法
本文主要介紹了MybatisPlus開(kāi)啟、關(guān)閉二級(jí)緩存方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09java.lang.UnsupportedClassVersionError錯(cuò)誤的解決辦法(附圖文)
這篇文章主要給大家介紹了關(guān)于java.lang.UnsupportedClassVersionError錯(cuò)誤的解決辦法,"java.lang.UnsupportedClassVersionError"意味著您正在運(yùn)行的Java版本與編譯該類(lèi)時(shí)使用的Java版本不兼容,需要的朋友可以參考下2023-10-10