整理文檔,搜刮出一個SpringMvc MultipartFile實現圖片文件上傳示例,稍微整理精簡一下做下分享。
spring-servlet.xml
1
2
3
4
5
6
7
|
<!-- SpringMVC上傳文件時,需要配置MultipartResolver處理器 --> < bean id = "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" > < property name = "defaultEncoding" value = "UTF-8" /> <!-- 指定所上傳文件的總大小,單位字節。注意maxUploadSize屬性的限制不是針對單個文件,而是所有文件的容量之和 --> < property name = "maxUploadSize" value = "10240000" /> </ bean > |
upload/index.jsp
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> <html> <head> <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > <title>單圖片上傳</title> </head> <body> <fieldset> <legend>圖片上傳</legend> <h2>只能上傳單張10M以下的 PNG、JPG、GIF 格式的圖片</h2> <form action= "/shop/auth/photoUpload" method= "post" enctype= "multipart/form-data" > 選擇文件:<input type= "file" name= "file" > <input type= "submit" value= "上傳" > </form> </fieldset> </body> </html> |
或者使用ExtJs
js/user/photoUpload.js
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
|
Ext.onReady( function (){ Ext.create( 'Ext.form.Panel' , { title: '圖片上傳' , width: 600, bodyPadding: 10, frame: true , renderTo: Ext.getBody(), items: [{ xtype: 'filefield' , name: 'file' , fieldLabel: 'Photo' , labelWidth: 50, msgTarget: 'side' , fileUpload: true , allowBlank: false , blankText: "Select an image" , emptyText: 'You can only upload a single PNG 10M or less, JPG, GIF format images' , anchor: '100%' , buttonText: '選擇圖片' }], buttons: [{ text: '上傳' , handler: function () { var form = this .up( 'form' ).getForm(); if (form.isValid()){ form.submit({ url: '/shop/auth/photoUpload' , waitMsg: '正在上傳圖片...' , success: function (fp, o) { Ext.Msg.alert( '提示' , o.result.msg); } }); } } }] }); }); |
pages/user/photoUpload.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >圖片上傳</ title > </ head > < link href = "../../ext-4.2.1.883/resources/css/ext-all.css" rel = "external nofollow" rel = "stylesheet" type = "text/css" /> < script type = "text/javascript" src = "../../ext-4.2.1.883/ext-all.js" ></ script > < script src = "../../js/user/photoUpload.js" type = "text/javascript" ></ script > < body > </ body > </ html > |
AuthController.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
|
/** * 圖片文件上傳 */ @ResponseBody @RequestMapping (value = "/photoUpload" ,method = RequestMethod.POST) public ResultData<Object> photoUpload(MultipartFile file,HttpServletRequest request,HttpServletResponse response,HttpSession session) throws IllegalStateException, IOException{ ResultData<Object> resultData= new ResultData<>(); // 判斷用戶是否登錄 /*User user=(User) session.getAttribute("user"); if (user==null) { resultData.setCode(40029); resultData.setMsg("用戶未登錄"); return resultData; }*/ if (file!=null) {// 判斷上傳的文件是否為空 String path=null;// 文件路徑 String type=null;// 文件類型 String fileName=file.getOriginalFilename();// 文件原名稱 System.out.println("上傳的文件原名稱:"+fileName); // 判斷文件類型 type=fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null; if (type!=null) {// 判斷文件類型是否為空 if ("GIF".equals(type.toUpperCase())||"PNG".equals(type.toUpperCase())||"JPG".equals(type.toUpperCase())) { // 項目在容器中實際發布運行的根路徑 String realPath=request.getSession().getServletContext().getRealPath("/"); // 自定義的文件名稱 String trueFileName=String.valueOf(System.currentTimeMillis())+fileName; // 設置存放圖片文件的路徑 path=realPath+/*System.getProperty("file.separator")+*/ trueFileName; System.out.println( "存放圖片文件的路徑:" +path); // 轉存文件到指定的路徑 file.transferTo( new File(path)); System.out.println( "文件成功上傳到指定目錄下" ); } else { System.out.println( "不是我們想要的文件類型,請按要求重新上傳" ); return null ; } } else { System.out.println( "文件類型為空" ); return null ; } } else { System.out.println( "沒有找到相對應的文件" ); return null ; } return resultData; } |
ResultData.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
|
public class ResultData<T> { private T data; private int code = 200 ; private String msg; private Boolean success = true ; public Boolean getSuccess() { return success; } public void setSuccess(Boolean success) { this .success = success; } public T getData() { return data; } public void setData(T data) { this .data = data; } public int getCode() { return code; } public void setCode( int code) { if ( 200 != code){ success = false ; } this .code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this .msg = msg; } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/LEARN4J/p/5426980.html