java里使用uploadify来实现文件上传

java,jsp文件上传 我最后最后用的jquery的uploadify来结合spring

文件上传 最后用的jquery的uploadify来结合spring

1.新建一个bean

package com.xiaoma.xmapp.mo;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class UploadBean {
private CommonsMultipartFile filedata;
private String name;

public CommonsMultipartFile getFiledata() {
return filedata;
}

public void setFiledata(CommonsMultipartFile filedata) {
this.filedata = filedata;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

然后页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ String path = request.getContextPath(); String homePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort(); String basePath = homePath + path + "/"; %>

上传测试

<script type="text/javascript" src="<%=basePath%>js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="<%=basePath%>js/jquery.uploadify.v2.1.4.min.js"></script>
<script type="text/javascript" src="<%=basePath%>js/swfobject.js"></script>
<script type="text/javascript">
            $(document).ready(function() {
              $('#file_upload').uploadify({
                'uploader'  : '<%=basePath%>js/plugins/uploadify.swf',
                'script'    : '<%=basePath%>res/upload/',
                'cancelImg' : '<%=basePath%>images/uploadify/cancel.png',
                'multi': true,
                'folder'    : '/uploads',
                'auto'      : true
              });
            });
        </script>


<input id="file_upload" name="file_upload" type="file" />


然后controller

/**
* 上传界面
*
* @param request
* @return
*/
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public ModelAndView selectUploadFile(HttpServletRequest request) {
log.debug("go to the resource upload page");
ModelAndView mav = new ModelAndView("resource/upload");
return mav;
}

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ModelAndView doUpload(HttpServletRequest request,
UploadBean uploadbean, BindingResult result) {
log.debug("post the upload data");
String fileName = uploadbean.getFiledata().getOriginalFilename();
ModelAndView mav = new ModelAndView("resource/upload");
return mav;
}

在多文件上传的时候,每次上传都会进入一次doUpload方法