springMVC+uploadify实现文件异步上传

springMVC+uplodify实现异步上传

最近在做课程设计的时候准备实现一个异步上传的功能
在网上查找jQuery的上传插件,找到了这个uploadify插件
简单的看了下上传时候的demo感觉样式不错,于是决定拿来用一下。

首先来说目前uploadfiy插件分为HTML5版本(收费)和flash版本
此处谈论flash版本

下面是插件的简单用法

$(‘#file_upload’).uploadify({
    ‘swf’:’resources/js/uploadify.swf’,
    ‘uploader’:’picUpload’
});

以上两个是必须的参数
第一个是指定的动画文件,后面为动画的路径
第二个是指定的上传的路径(服务器接收路径)
如果需要更详尽的参数可以去官网查找。官网doc地址

在使用springMVC接收参数时有问题,request接收到的参数,始终在转换为MultipartHttpServletRequest的时候有问题,查原因,一般在同步接收的时候,都是提交form表单的,需要在表单中红增加ENCTYPE=”multipart/form-data “这么个东西。然后查了下uploadify文档没有找到相应的参数设置,于是放弃,使用传统的servlet的方式。

在此贴出解决后的代码,所需jar(commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar)

@RequestMapping(value=”/{picUpload}”, method=RequestMethod.POST)  
    public String picUpload(HttpServletRequest request,ModelMap model, HttpSession session) throws IOException {
    DiskFileItemFactory fac = new DiskFileItemFactory();  
    ServletFileUpload upload = new ServletFileUpload(fac);

    upload.setHeaderEncoding(“utf-8”);  
    List fileList = null;  
    try {  
        fileList = upload.parseRequest(request);  
    } catch (FileUploadException ex) {

    }

    Iterator<FileItem> it = fileList.iterator();
    String name = “”;  
    String extName = “”;  
    while (it.hasNext()) {  
        FileItem item = it.next();
        if (!item.isFormField()) {  
            name = item.getName();  
            long size = item.getSize();  
            String type = item.getContentType();  
            //System.out.println(size + ” ” + type);  
            if (name == null || name.trim().equals(“”)) {  
                continue;  
            }  
            // 扩展名格式:  
            if (name.lastIndexOf(“.”) >= 0) {  
                extName = name.substring(name.lastIndexOf(“.”));  
            }  
            File file = null;  
            do {  
                // 生成文件名:  
                name = UUID.randomUUID().toString();  
                file = new File(“D://” + name + extName);  
                //System.out.println(savePath + name + extName);  
            } while (file.exists());  
            File saveFile = new File(“D://” + name + extName);  
            try {  
                item.write(saveFile);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }
        }  
    }


    return “”;
}

如果有哪位高手,使用uploadify插件能够用spring的MultipartHttpServletRequest来接收的,请不吝赐教。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值