2008-04-22
jquery和springmvc多文件上传
关键字: jquery springmvc 多文件上传
jquery的插件使用的是jQuery Multiple File Upload Plugin v1.27 地址 http://jquery.com/plugins/project/MultiFile/
需要修改源代码,在js的282行
去掉注释,让input name就生成不同名称了,不然post上来的文件数据都是没有名字的.
页面端代码:
spring mvc端:
UploadFormBean:
需要修改源代码,在js的282行
slave.name = (slave.name || $(MASTER).attr('name') || 'file');// + (slave.i>0?slave.i:''); // same name as master element
去掉注释,让input name就生成不同名称了,不然post上来的文件数据都是没有名字的.
页面端代码:
<script type="text/javascript">
<!--
$().ready(function() {
$('#houseMaps').MultiFile({
accept:'gif|jpg|png|jpeg|bmp', max:6, STRING: {
remove:'删除',
selected:'Selecionado: $file',
denied:'不支持上传该文件类型 $ext!'
}
});
});
//-->
</script>
<form method="post" action="xxx.do?method=uploader" enctype="multipart/form-data">
<input type="file" id="houseMaps" name="houseMaps"/>
</form>
spring mvc端:
public ModelAndView uploader(HttpServletRequest request,HttpServletResponse response,UploadFormBean ufb) throws ServletRequestBindingException, HouseImgUploadException, IOException{
MultipartFile styleMap = ufb.getStyleMap();
Set<MultipartFile> mfs = getFileSet(request);
for(MultipartFile mf : mfs){
String imgPath = uploadFileAndCallback(mf);
//拿到的imgPath就是图片的相对于contextPath的存储路径了
}
return new ModelAndView("xxx");
}
public Set<MultipartFile> getFileSet(HttpServletRequest request) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Set<MultipartFile> fileset = new LinkedHashSet<MultipartFile>();
for (Iterator it = multipartRequest.getFileNames(); it.hasNext();) {
String key = (String)it.next();
MultipartFile file = multipartRequest.getFile(key);
if (file.getOriginalFilename().length() > 0) {
fileset.add(file);
}
}
return fileset;
}
private String uploadFileAndCallback(MultipartFile file) throws HouseImgUploadException, IOException {
if(validateFile(file)){
String filename = file.getOriginalFilename();
String extName = filename.substring(filename.lastIndexOf(".")).toLowerCase();
String lastFileName = System.currentTimeMillis()+CalendarUtil.getRadomValue()+extName;
//图片存储的相对路径
String path = sp+resource_subpath_name+sp+houseId+sp;
String fileFullPath = getServletContext().getRealPath(resource_name)+path+lastFileName;
FileCopyUtils.copy(file.getBytes(),new File(fileFullPath));
return "/"+resource_name+"/"+resource_subpath_name+"/"+lastFileName;
}else{
throw new HouseImgUploadException("上传文件不符合规格");
}
}
private boolean validateFile(MultipartFile file) {
if(file.getSize()<0 || file.getSize() > 2000000)
return false;
String filename = file.getOriginalFilename();
String extName = filename.substring(filename.lastIndexOf(".")).toLowerCase();
if(extName.equals(".jpg") || extName.equals(".gif") ||
extName.equals(".png") || extName.equals(".bmp") || extName.equals(".jpeg")){
return true;
}else{
return false;
}
}
UploadFormBean:
public class UploadFormBean {
private MultipartFile styleMap;
public MultipartFile getStyleMap() {
return styleMap;
}
public void setStyleMap(MultipartFile styleMap) {
this.styleMap = styleMap;
}
}
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 12395 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
我的相册
001
共 3 张
共 3 张
最近加入圈子
最新评论
-
cas-client-3.1.1的客户端 ...
我按照你的配置怎么都跑不起来,现在很急啊,你实地配成功过没有。另外3.1.1的单 ...
-- by luojunlt -
在frameset中的登陆界面se ...
没看明白什么意思!!o(∩_∩)o...能力问题?
-- by guojingxf -
cas-client-3.1.1的客户端 ...
不是,就是一个spring容器上下文管理的xml文件
-- by kj2ff -
cas-client-3.1.1的客户端 ...
请问下面的casContent.xml是什么文件?acegi的配置文件么?
-- by yangeou -
linux 中文显示乱码解决办 ...
好东西啊,正在找这个,呵呵
-- by flysky9820000






评论排行榜