1.在jsp页面<head>标签内引入script文件(注意顺序)
1
2
|
<script type= "text/javascript" src= "resources/js/jquery-3.3.1.min.js" ></script> <script type= "text/javascript" src= "resources/js/ajaxfileupload.js" ></script> |
2.springmvc.xml配置文件中(必须进行配置)
1
2
3
4
|
<!--使用springmvc上传图片 ajaxfileupload--> <bean id= "multipartresolver" class = "org.springframework.web.multipart.commons.commonsmultipartresolver" > <property name= "maxuploadsize" value= "10485760" /> </bean> |
3.<input>标签(id属性,<script>中 ajaxfileupload的fileelementid会用到)
1
|
<input type= "file" id= "headimg" name= "headimg" /> |
4.在<script>标签中,我将ajaxfileupload写在一个函数里,在需要用到上传图片的位置,调用该函数
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
|
function headimgupload(){ //图片上传函数 var results = "" ; var account = $( "#account" ).val(); //account、identity 这两个变量,根据我的需求会在后面用到,阅读者可以根据自己的需求删掉或者修改这两个变量 var identity = $( "input[name='identity']:checked" ).val(); $.ajaxfileupload({ url: "register/headimgupload?account=" +account+ "&identity=" +identity, //根据url访问controller层中的方法 secureuri: false , fileelementid: "headimg" , //<input>标签中的id属性 type: "post" , datatype: "text" , //服务器返回的数据类型 success:function (result) { result = result.replace(/<pre.*?>/g, '' ); //ajaxfileupload会对服务器响应回来的text内容加上<pre style="....">text</pre>前后缀 result = result.replace(/<pre.*?>/g, '' ); result = result.replace( "<pre>" , '' ); result = result.replace( "</pre>" , '' ); result = result.replace( "<pre>" , '' ); result = result.replace( "</pre>" , '' ); result = json.parse(result); //转换为json格式 results = result.result; alert(result); }, error:function (data,status,error) { alert( "失败!!!" +error); } }); } |
5.controller层
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
|
@controller @requestmapping (value = "register" ) public class registercontroller { //用户头像上传 @requestmapping (value = "/headimgupload" ,method = requestmethod.post) @responsebody public map<string,object> headimgupload( @requestparam multipartfile headimg, string account, string identity, httpservletrequest request){ //上传的结果 string result = "fail" ; //头像上传到的位置 string imgrealpath = "" ; try { //确保上传的图片不为空 if (headimg != null && !headimg.isempty()){ //判断注册用户的身份,商户还是会员 if (identity.equals( "member" )){ //会员 imgrealpath = request.getsession().getservletcontext().getrealpath( "/resources/image/member" ); } else if (identity.equals( "businessman" )){ //商户 imgrealpath = request.getsession().getservletcontext().getrealpath( "/resources/image/businessman" ); } system.out.println(imgrealpath); //在控制台打印一下路径 //上传完成后保存的文件名 string filename= account + ".jpg" ; //文件夹不存在的话,新建一个 file filefolder = new file(imgrealpath); if (!filefolder.exists()){ filefolder.mkdirs(); } file file = new file(filefolder,filename); //transferto(),springmvc的方法,用于图片上传时,将内存中的图片写入磁盘 headimg.transferto(file); //会报io异常 result = "success" ; } } catch (ioexception e){ e.printstacktrace(); } map<string,object> resultmap = new hashmap<string,object>(); resultmap.put( "result" ,result); return resultmap; } } |
总结
以上所述是小编给大家介绍的springmvc使用ajaxfailupload上传图片,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://blog.csdn.net/Tdh5258/article/details/80533997