文件上傳頁面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>Insert title here</title> </head> <body> <h1>實現(xiàn)文件上傳</h1> <!--enctype= "開啟多媒體標(biāo)簽" -這一個屬性就開啟了多媒體文件的上傳!! --> <form action= "http://localhost:8091/file" method= "post" enctype= "multipart/form-data" > <input name= "fileImage" type= "file" /> <input type= "submit" value= "提交" /> </form> </body> </html> |
1 編輯FileController --只是引入文件上傳的示例
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
29
30
31
32
33
34
35
36
37
38
39
40
|
package com.jt.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.io.InputStream; @RestController public class FileController { /** * MultipartFile 接口作用 主要就是優(yōu)化了文件上傳 API集合 * 1. 文件上傳位置??? D:\JT-SOFT\images * 2. 判斷一下文件目錄是否存在 * 3. 利用API實現(xiàn)文件上傳. */ @RequestMapping ( "/file" ) public String file(MultipartFile fileImage){ String fileDir = "D:/JT-SOFT/images" ; File file = new File(fileDir); if (!file.exists()){ //文件不存在則創(chuàng)建文件 file.mkdirs(); //一次性創(chuàng)建多級目錄 } //文件信息 = 文件名+文件后綴 ----這里開始真正的利用多媒體文件MultipartFile類型的API-getOriginalFilename獲取多媒體文件的全稱 String fileName = fileImage.getOriginalFilename(); //將文件的整體封裝為對象 文件路徑/文件名稱 File imageFile = new File(fileDir+ "/" +fileName); //實現(xiàn)文件上傳,將文件字節(jié)數(shù)組傳輸?shù)街付ǖ奈恢? -另一個多媒體文件的API - transferTo上傳多媒體文件到本地磁盤位置并保存 try { fileImage.transferTo(imageFile); } catch (IOException e) { e.printStackTrace(); } return "文件上傳成功!!!!" ; } } |
真正項目上的文件上傳操作
1 封裝VO對象
{“error”:0,“url”:“圖片的保存路徑”,“width”:圖片的寬度,“height”:圖片的高度}
說明:
error: 代表文件上傳的錯誤. 0 文件上傳正確 1.文件上傳失敗.
url地址: 訪問圖片的網(wǎng)絡(luò)地址… 用戶通過url地址獲取圖片信息 --與真實物理地址之間經(jīng)過nginx反向代理實現(xiàn)
訪問圖片的物理地址… 真實存儲的地址 D:/a/a.jpg
width/height: 寬度和高度是圖片的特有屬性…判斷是否為圖片的依據(jù)
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
29
|
package com.jt.vo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.io.Serializable; @Data @Accessors (chain = true ) @NoArgsConstructor @AllArgsConstructor public class ImageVO implements Serializable { //{"error":0,"url":"圖片的保存路徑","width":圖片的寬度,"height":圖片的高度} private Integer error; private String url; //圖片虛擬訪問路徑 private Integer width; //寬度 private Integer height; //高度 //success fail public static ImageVO fail(){ return new ImageVO( 1 , null , null , null ); } public static ImageVO success(String url,Integer width,Integer height){ return new ImageVO( 0 , url, width, height); } } |
2 文件上傳頁面url分析
參數(shù)說明
jsp頁面需要/或者提供了哪個參數(shù)就后端開發(fā)就要提供/或者接收那個參數(shù),參數(shù)名稱必須一致,否則頁面解析不了/或后端參數(shù)/對象接收不到!!
3 編輯配置文件image.properties --為了將來實現(xiàn)項目的擴展性,將核心的配置寫入該配置文件中
1
2
3
4
|
#properties的作用就是封裝key=value 業(yè)務(wù)數(shù)據(jù) image.dirPath=D:/JT-SOFT/images image.urlPath=http: //image.jt.com #這里寫域名是為了客戶端訪問方便的,經(jīng)過nginx反向代理服務(wù)器后實際鏈接到服務(wù)器本地磁盤上的物理路徑 |
3.編輯FileController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.jt.controller; //導(dǎo)的包就不寫了,idea自動導(dǎo)去吧 @RestController public class FileController { /** * 業(yè)務(wù):實現(xiàn)商品的文件上傳操作 * url地址: http://localhost:8091/pic/upload?dir=image * 參數(shù): uploadFile 注意字母的大小寫 * 返回值結(jié)果: ImageVO對象. */ @Autowired private FileService fileService; @RequestMapping ( "/pic/upload" ) public ImageVO upload(MultipartFile uploadFile){ //將所有的業(yè)務(wù)操作,放到Service層中完成!!! return fileService.upload(uploadFile); } } |
4編輯FileService
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
package com.jt.service; @Service @PropertySource ( "classpath:/properties/image.properties" ) public class FileServiceImpl implements FileService{ @Value ( "${image.dirPath}" ) private String dirPath; @Value ( "${image.urlPath}" ) private String urlPath; //為了防止Set集合每次都要創(chuàng)建,則通過static代碼塊的形式負(fù)責(zé)封裝數(shù)據(jù) private static Set<String> imageSet = new HashSet<>(); static { imageSet.add( ".jpg" ); imageSet.add( ".png" ); imageSet.add( ".gif" ); //.... } /** * 文件上傳具體步驟: * 1.如何校驗用戶上傳的是圖片? jpg|png * 2.如何訪問用戶上傳惡意程序 木馬.exe.jpg 寬度*高度 * 3.應(yīng)該采用分目錄存儲的方式 保存數(shù)據(jù) * 4.上傳的文件名稱應(yīng)該盡量避免重名 自定義文件名稱... UUID.后綴... */ @Override public ImageVO upload(MultipartFile uploadFile) { //1.校驗圖片類型是否正確 jpg|png|gifxxxx 1.正則表達式判斷 2.準(zhǔn)備集合之后進行校驗Set<去重> //1.1 獲取上傳的圖片類型 ABC.JPG String fileName = uploadFile.getOriginalFilename(); //文件的全名 abc.jpg fileName = fileName.toLowerCase(); //將所有的字符轉(zhuǎn)化為小寫 int index = fileName.lastIndexOf( "." ); String fileType = fileName.substring(index); //含頭不含尾即從點處開始切割字符串,沒有聲明截止位置默認(rèn)為最后了. //1.2判斷是否為圖片類型 bug-圖片類型大小寫這里判斷不了一致的,所以獲取到多媒體文件的全稱后直接全部轉(zhuǎn)化為小寫!! if (!imageSet.contains(fileType)){ //用戶上傳的不是圖片 return ImageVO.fail(); } //2.上傳的數(shù)據(jù)是否為惡意程序. 高度和寬度是否為null. 利用圖片API //BufferedImage對象 專門負(fù)責(zé)封裝圖片 --利用圖片IO對象ImageIO的API-read方法獲取到該多媒體文件的字節(jié)流封裝為BufferedImage對象 --因為它上面有API獲取文件的寬和高 try { BufferedImage bufferedImage = ImageIO.read(uploadFile.getInputStream()); int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); if (width== 0 || height == 0 ){ return ImageVO.fail(); } //=======以上僅為判斷客戶端傳來的多媒體文件是否為圖片,下面才是真正的圖片存入本地磁盤操作========= //3.采用分目錄存儲的方式 --第三步只是為了創(chuàng)建存儲圖片的磁盤目錄 //String dirPath = "D:/JT-SOFT/images"; //動態(tài)獲取 //3.1 分目錄存儲方式1 hash方式 ACBBCDD //3.1 分目錄存儲方式2 時間方式存儲 yyyy/MM/dd --注意聲明時間格式時前后都要加斜杠,因為這是文件夾目錄 String dateDir = new SimpleDateFormat( "/yyyy/MM/dd/" ).format( new Date()); //3.2 準(zhǔn)備文件存儲的目錄 String imageDir = dirPath + dateDir; File imageFileDir = new File(imageDir); if (!imageFileDir.exists()){ imageFileDir.mkdirs(); } //4 實現(xiàn)文件上傳 --優(yōu)先防止存儲文件重名UUID生成文件名稱 //4.1 動態(tài)拼接文件名稱 f3aa1378-ece6-11ea-98c9-00d861eaf238 //UUID.randomUUID()生成的是一串帶有短橫杠的16進制數(shù)字,轉(zhuǎn)化為字符串是方便用字符串的API-replace替換掉(刪減)其中的短橫杠 String uuid = UUID.randomUUID().toString().replace( "-" , "" ); // uuid.后綴 String realFileName = uuid + fileType; //4.2 準(zhǔn)備文件上傳的全路徑 磁盤路徑地址+文件名稱 File imageFile = new File(imageDir+realFileName); //4.3 實現(xiàn)文件上傳 uploadFile.transferTo(imageFile); //=====以上已經(jīng)將多媒體文件存入服務(wù)器磁盤了!!下面是將本地磁盤路徑轉(zhuǎn)化為url路徑方便客戶端的訪問===== //5.動態(tài)生成URL地址 //請求協(xié)議: http:// https:// 帶證書的網(wǎng)址 安全性更高 公鑰私鑰進行加密解密. //向服務(wù)器運行商購買域名 com cn org hosts文件 //圖片存儲的虛擬地址的路徑 動態(tài)變化的路徑 //http://image.jt.com/2020/09/02/uuid.jpg String url = urlPath+dateDir+realFileName; return ImageVO.success(url,width,height); } catch (IOException e) { e.printStackTrace(); return ImageVO.fail(); } } } |
下一個博客專門寫nginx反向代理服務(wù)器的安裝及使用!!
到此這篇關(guān)于Java中多媒體文件上傳及頁面回顯的操作代碼的文章就介紹到這了,更多相關(guān)Java文件上傳頁面回顯內(nèi)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/BShu666/article/details/108570483