springmvc多文件上传

来源:互联网 发布:c语言合法用户标识符 编辑:程序博客网 时间:2024/05/22 09:07

jar包:倒jar不用多说


springmvc配置:

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
<property name="maxUploadSize">
<value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>




js:

<script type="text/javascript">

function submitMe() { //提交表单

     if (check()) {
        document.getElementById('myForm').submit();
        document.getElementById('saveinfo').disabled=true; 
     }
}


//提交之前检查
function check()
{
var errCode = true ;

return errCode;

}


//检测文件大小和类型 
function fileChange(target){ 
//检测上传文件的类型 
if(!(/(?:jpg|gif|png|jpeg)$/i.test(target.value))) { 
alert("只允许上传jpg|gif|png|jpeg格式的图片"); 
if(window.ActiveXObject) {//for IE 
target.select();//select the file ,and clear selection 
document.selection.clear(); 
} else if(window.opera) {//for opera 
target.type="text";target.type="file"; 
} else target.value="";//for FF,Chrome,Safari 
return; 
} else { 
return; //alert("ok");//or you can do nothing here. 


//检测上传文件的大小 
var isIE = /msie/i.test(navigator.userAgent) && !window.opera; 
var fileSize = 0; 
if (isIE && !target.files){ 
var filePath = target.value; 
var fileSystem = new ActiveXObject("Scripting.FileSystemObject"); 
var file = fileSystem.GetFile (filePath); 
fileSize = file.Size; 
} else { 
fileSize = target.files[0].size; 

var size = fileSize / 1024; 
if(size>(500)){ 
alert("文件大小不能超过500KB"); 
if(window.ActiveXObject) {//for IE 
target.select();//select the file ,and clear selection 
document.selection.clear(); 
} else if(window.opera) {//for opera 
target.type="text";target.type="file"; 
} else { 
target.value="";//for FF,Chrome,Safari 

return; 
}else{ 
return; 


</script>





html:

<form action="<%=basePath%>/image/update2.action"  id="myForm" method="post"  enctype="multipart/form-data" >

<input type="file" name ="upLoadFile"  id ="upLoadFile"  onchange="fileChange(this)"/>

<input type="file" name ="upLoadFile"  id ="upLoadFile2"  onchange="fileChange(this)"/>

<input type="button" id="saveinfo" value="保存" onclick="javascript:submitMe();" />
<input type="button" value="返回" onclick="javascript:window.history.back();" />

<form>





后台:

package com.dm.controller;



import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;


import com.dm.model.Image;
import com.dm.service.UpLoadService;






@Controller
@RequestMapping("/image")
public class ImageController {
@Autowired
private UpLoadService upLoadService;


@RequestMapping("/update2")
public  String  updatePig(@RequestParam(value = "upLoadFile", required = false) MultipartFile file[],Image image, HttpServletRequest request){
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();  
   ServletContext servletContext = webApplicationContext.getServletContext();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
String path = servletContext.getRealPath("/")+"upload/"+image.getImagestyle()+"/";

for(int i=0;i<file.length;i++){
UUID uuid = UUID.randomUUID();
       String fileName =uuid.toString()+file[i].getOriginalFilename();
       File targetFile = new File(path, fileName);
       if(!targetFile.exists()){
           targetFile.mkdirs();
       }


       try {
file[i].transferTo(targetFile);

} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
image.setImagename(fileName);
image.setImageurl("upload/"+image.getImagestyle()+"/"+fileName);
image.setImagetime(new Date());
image.setImagestatu(0);
       upLoadService.updatePig(image);
}
return "success";
}
}
0 0
原创粉丝点击