之前在使用springboot進行文件上傳時,遇到了很多問題。于是在翻閱了很多的博文之后,總算將上傳功能進行了相應的完善,便在這里記錄下來,供自己以后查閱。
首先,是建立一個標準的springboot 的工程,這里使用的ide是intellij idea,為了方便配置,將默認的配置文件替換為了application.yml。
1.在index.html中進行文件上傳功能,這里使用的文件上傳方式是ajax,當然也可以按照自己的具體要求使用傳統的表單文件上傳。
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
|
<!doctype html> <html lang= "en" > <head> <meta charset= "utf-8" > <title>上傳測試</title> <script type= "text/javascript" src= "js/jquery-3.2.1.min.js" ></script> </head> <body> <input id= "file" type= "file" name= "file" /> <br/> <button id= "upload" onclick= "doupload()" >上傳</button> <progress id= "progressbar" value= "0" max= "100" ></progress> <script> function doupload() { var fileobj = document.getelementbyid( "file" ).files[ 0 ]; // js 獲取文件對象 var filecontroller = "/upload" ; // 接收上傳文件的后臺地址 // formdata 對象 var form = new formdata(); form.append( "file" ,fileobj); // xmlhttprequest 對象 var xhr = new xmlhttprequest(); //為請求添加返回處理函數 xhr.onreadystatechange=function () { if ( this .readystate == 4 && this .status == 200 ){ var b = this .responsetext; if (b == "success" ){ alert( "上傳成功!" ); } else { alert( "上傳失??!" ); } } }; xhr.open( "post" , filecontroller, true ); //使用進度條記錄上傳進度 xhr.upload.addeventlistener( "progress" , progressfunction, false ); xhr.send(form); } function progressfunction(evt) { var progressbar = document.getelementbyid( "progressbar" ); var percentagediv = document.getelementbyid( "percentage" ); if (evt.lengthcomputable) { progressbar.max = evt.total; progressbar.value = evt.loaded; percentagediv.innerhtml = math.round(evt.loaded / evt.total * 100 ) + "%" ; } } </script> </body> </html> |
2.在maincontroller添加文件上傳的api,并返回上傳結果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@postmapping ( "/upload" ) @responsebody public string upload(httpservletrequest request, @requestparam ( "file" ) multipartfile file) { string path = "e://upload//" ; string filename = file.getoriginalfilename(); system.out.println(filename); file targetfile = new file(path); if (!targetfile.exists()) { targetfile.mkdirs(); } file savefile= new file(path+filename); // 保存 try { file.transferto(savefile); return "success" ; } catch (exception e) { e.printstacktrace(); return "fail" ; } } |
這時,我們進行測試,就可以發現,文件上傳已經完成了。
很多時候,我們在進行文件上傳時,特別是向普通用戶開放文件上傳功能時,需要對上傳文件的格式進行控制,以防止黑客將病毒腳本上傳。單純的將文件名的類型進行截取的方式非常容易遭到破解,上傳者只需要將病毒改換文件名便可以完成上傳。
這時候我們可以讀取文件的十六進制的文件頭,來判斷文件真正的格式。
因為我們發現,在我們讀取文件的二進制數據并將其轉換為十六進制時,同類型文件的文件頭數據是相同的,即使改變了其后綴,這個數據也不會改變,例如,png文件的文件頭為“89504e47”。
首先,我們將文件的數據進行讀取
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
|
public class fileutil { public static string getfileheader( multipartfile file) { inputstream is = null ; string value = null ; try { is = file.getinputstream(); byte [] b = new byte [ 4 ]; is.read(b, 0 , b.length); value = bytestohexstring(b); } catch (exception e) { } finally { if ( null != is) { try { is.close(); } catch (ioexception e) { } } } return value; } private static string bytestohexstring( byte [] src) { stringbuilder builder = new stringbuilder(); if (src == null || src.length <= 0 ) { return null ; } string hv; for ( int i = 0 ; i < src.length; i++) { hv = integer.tohexstring(src[i] & 0xff ).touppercase(); if (hv.length() < 2 ) { builder.append( 0 ); } builder.append(hv); } system.out.println(builder.tostring()); return builder.tostring(); } } |
然后在文件上傳的api中進行調用
1
|
fileutil.getfileheader(file) |
這時候,我們只需要進行簡單的字符串比對,判斷調用的返回值是否為“89504e47”,就可以知道上傳的是否為png文件。
下面看下 java 獲取和判斷文件頭信息
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
import java.io.fileinputstream; import java.io.ioexception; import java.util.hashmap; /** * 獲取和判斷文件頭信息 * * @author sud * */ public class gettypebyhead { // 緩存文件頭信息-文件頭信息 public static final hashmap<string, string> mfiletypes = new hashmap<string, string>(); static { // images mfiletypes.put( "ffd8ff" , "jpg" ); mfiletypes.put( "89504e47" , "png" ); mfiletypes.put( "47494638" , "gif" ); mfiletypes.put( "49492a00" , "tif" ); mfiletypes.put( "424d" , "bmp" ); // mfiletypes.put( "41433130" , "dwg" ); // cad mfiletypes.put( "38425053" , "psd" ); mfiletypes.put( "7b5c727466" , "rtf" ); // 日記本 mfiletypes.put( "3c3f786d6c" , "xml" ); mfiletypes.put( "68746d6c3e" , "html" ); mfiletypes.put( "44656c69766572792d646174653a" , "eml" ); // 郵件 mfiletypes.put( "d0cf11e0" , "doc" ); mfiletypes.put( "5374616e64617264204a" , "mdb" ); mfiletypes.put( "252150532d41646f6265" , "ps" ); mfiletypes.put( "255044462d312e" , "pdf" ); mfiletypes.put( "504b0304" , "docx" ); mfiletypes.put( "52617221" , "rar" ); mfiletypes.put( "57415645" , "wav" ); mfiletypes.put( "41564920" , "avi" ); mfiletypes.put( "2e524d46" , "rm" ); mfiletypes.put( "000001ba" , "mpg" ); mfiletypes.put( "000001b3" , "mpg" ); mfiletypes.put( "6d6f6f76" , "mov" ); mfiletypes.put( "3026b2758e66cf11" , "asf" ); mfiletypes.put( "4d546864" , "mid" ); mfiletypes.put( "1f8b08" , "gz" ); mfiletypes.put( "4d5a9000" , "exe/dll" ); mfiletypes.put( "75736167" , "txt" ); } /** * 根據文件路徑獲取文件頭信息 * * @param filepath * 文件路徑 * @return 文件頭信息 */ public static string getfiletype(string filepath) { system.out.println(getfileheader(filepath)); system.out.println(mfiletypes.get(getfileheader(filepath))); return mfiletypes.get(getfileheader(filepath)); } /** * 根據文件路徑獲取文件頭信息 * * @param filepath * 文件路徑 * @return 文件頭信息 */ public static string getfileheader(string filepath) { fileinputstream is = null ; string value = null ; try { is = new fileinputstream(filepath); byte [] b = new byte [ 4 ]; /* * int read() 從此輸入流中讀取一個數據字節。 int read(byte[] b) 從此輸入流中將最多 b.length * 個字節的數據讀入一個 byte 數組中。 int read(byte[] b, int off, int len) * 從此輸入流中將最多 len 個字節的數據讀入一個 byte 數組中。 */ is.read(b, 0, b.length); value = bytestohexstring(b); } catch (exception e) { } finally { if (null != is) { try { is.close(); } catch (ioexception e) { } } } return value; } /** * 將要讀取文件頭信息的文件的byte數組轉換成string類型表示 * * @param src * 要讀取文件頭信息的文件的byte數組 * @return 文件頭信息 */ private static string bytestohexstring( byte [] src) { stringbuilder builder = new stringbuilder(); if (src == null || src.length <= 0 ) { return null ; } string hv; for ( int i = 0 ; i < src.length; i++) { // 以十六進制(基數 16)無符號整數形式返回一個整數參數的字符串表示形式,并轉換為大寫 hv = integer.tohexstring(src[i] & 0xff ).touppercase(); if (hv.length() < 2 ) { builder.append( 0 ); } builder.append(hv); } system.out.println(builder.tostring()); return builder.tostring(); } public static void main(string[] args) throws exception { final string filetype = getfiletype( "d:\\ry4s_java.dll" ); system.out.println(filetype); } } |
總結
以上所述是小編給大家介紹的springboot文件上傳控制及java 獲取和判斷文件頭信息,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/lcdxlh/article/details/78777675