MultipartFile轉(zhuǎn)File
公司業(yè)務(wù)遇到需要接收前臺(tái)提交過(guò)來(lái)的圖片或文件(multipart/form-data)類(lèi)型的(ps:不知道有沒(méi)有拼錯(cuò)嘻嘻)
后臺(tái)接收的需要轉(zhuǎn)換為一個(gè)File類(lèi)型的
那么這里涉及到了類(lèi)型轉(zhuǎn)換的問(wèn)題:
先列下我的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
@PostMapping ( "upload" ) @ResponseBody public void upload(HttpServletResponse response,MultipartFile file) throws IOException{ //MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file"); PrintWriter writer = response.getWriter(); File f = null ; if (file.equals( "" )||file.getSize()<= 0 ){ file = null ; } else { InputStream ins = file.getInputStream(); f= new File(file.getOriginalFilename()); FileUtil.inputStreamToFile(ins, f); } JSONObject jsonObject= weChatMaterialService.getMediaId( "image" ,f); writer.print(jsonObject); writer.flush(); writer.close(); File del = new File(f.toURI()); del.delete(); System.out.println(jsonObject); } |
現(xiàn)在來(lái)剖析下:就是判斷下我接收的文件是不是空的,如果不是空的就將它轉(zhuǎn)換成為流的形式
后面那個(gè)FileUtil就是將流轉(zhuǎn)換成File,最后一步就是因?yàn)檫@樣做法會(huì)在項(xiàng)目的根目錄下(好像是根目錄,我也不確定,也是現(xiàn)學(xué)現(xiàn)用的嘻嘻)生成一個(gè) 文件,這個(gè)是我們不需要的對(duì)嘛,然后將其刪除就行啦
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
File f = null ; if (file.equals( "" )||file.getSize()<= 0 ){ file = null ; } else { InputStream ins = file.getInputStream(); f= new File(file.getOriginalFilename()); FileUtil.inputStreamToFile(ins, f); } public class FileUtil { public static void inputStreamToFile(InputStream ins, File file) { try { OutputStream os = new FileOutputStream(file); int bytesRead = 0 ; byte [] buffer = new byte [ 8192 ]; while ((bytesRead = ins.read(buffer, 0 , 8192 )) != - 1 ) { os.write(buffer, 0 , bytesRead); } os.close(); ins.close(); } catch (Exception e) { e.printStackTrace(); } } } File del = new File(f.toURI()); del.delete(); |
File轉(zhuǎn)MultipartFile
添加依賴
1
2
3
4
5
6
|
<!-- https://mvnrepository.com/artifact/org.springframework/spring-mock --> < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-mock</ artifactId > < version >2.0.8</ version > </ dependency > |
代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * @param * @return * @author:liuyu * @創(chuàng)建日期:2020年3月5日 * @功能說(shuō)明:File轉(zhuǎn)MultipartFile */ private static MultipartFile fileToMultipartFile(File file) throws Exception { InputStream inputStream = new FileInputStream(file); MultipartFile multipartFile = new MockMultipartFile(file.getName(), inputStream); return multipartFile; } |
PS:file轉(zhuǎn)base64字符串
① java之文件轉(zhuǎn)為base64字符
1
2
3
4
5
6
|
FileInputStream inputFile = new FileInputStream(f); String base 64 = null ; byte [] buffer = new byte [( int ) f.length()]; inputFile.read(buffer); inputFile.close(); base64= new BASE64Encoder().encode(buffer); |
②注意:java中在使用BASE64Enconder().encode(buffer)會(huì)出現(xiàn)字符串換行問(wèn)題,這是因?yàn)镽FC 822中規(guī)定,每72個(gè)字符中加一個(gè)換行符號(hào),這樣會(huì)造成在使用base64字符串時(shí)出現(xiàn)問(wèn)題,所以我們?cè)谑褂脮r(shí)要先解決換行的問(wèn)題
1
|
String encoded = base64.replaceAll( "[\\s*\t\n\r]" , "" ); |
到此這篇關(guān)于java中MultipartFile互轉(zhuǎn)File的方法的文章就介紹到這了,更多相關(guān)java MultipartFile互轉(zhuǎn)File內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/lp15203883326/article/details/82879973