uploadify3.1 api 可参考:()
需要springmvc的jar包
1、upload.jsp(主要代码)
- <script type="text/javascript" charset="UTF-8"
- src="${pageContext.request.contextPath}/js/jquery1.11.js"></script>
- <script type="text/javascript" charset="UTF-8"
- src="${pageContext.request.contextPath}/static/llsfw/js/upload/upload.js"></script>
- <script type="text/javascript" charset="UTF-8"
- src="${pageContext.request.contextPath}/static/llsfw/common/uploadify/jquery.uploadify.min.js"></script>
- <div id="uploader">
- <p>
- <input type="file" name="file_upload" id="file_upload" />
- </p>
- <a href="javascript:$('#file_upload').uploadify('upload','*')">上传</a>
- <a href="javascript:$('#file_upload').uploadify('stop')">取消上传</a>
- <div id="uploader_queue"></div>
- <div id="uploader_msg"></div>
- <div id="uploader_view"></div>
- </div>
- $(function() {
- $("#file_upload").uploadify({
- 'auto' : false,
- 'method' : "get",
- 'formData' : { 'folder' : 'file'},
- 'height' : 30,
- 'swf' : basePath+'static/llsfw/common/uploadify/uploadify.swf', // flash
- 'uploader' : basePath+'uploadController/upload', // 数据处理url
- 'width' : 120,
- 'fileTypeDesc' : '只能是xls...',
- 'fileTypeExts' : '*.xls',
- 'fileSizeLimit' : '10500KB',
- 'buttonText' : '选择文件',
- 'uploadLimit' : 5,
- 'successTimeout' : 5,
- 'requeueErrors' : false,
- 'removeTimeout' : 10,
- 'removeCompleted' : false,
- 'queueSizeLimit' :10,
- 'queueID' : 'uploader_queue',
- 'progressData' : 'speed',
- 'onInit' : function (){},
- // 单个文件上传成功时的处理函数
- 'onUploadSuccess' : function(file, data, response){
- $("#uploader_view").append('<div height="60" >'+data+'</div>');
- },
- 'onQueueComplete' : function(queueData) {
- $('#uploader_msg').html(queueData.uploadsSuccessful + ' 个文件上传成功。');
- }
- });
- });
3、UploadController.java - package com.llsfw.core.controller.upload;
-
- import java.io.File;
- import java.io.IOException;
- import java.util.Iterator;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.multipart.MultipartFile;
- import org.springframework.web.multipart.MultipartHttpServletRequest;
- import org.springframework.web.multipart.commons.CommonsMultipartResolver;
-
- import com.llsfw.core.controller.base.BaseController;
-
- @Controller
- @RequestMapping("uploadController")
- public class UploadController extends BaseController {
- @RequestMapping("init")
- public String init() {
- return "llsfw/upload/upload";
- }
-
- @RequestMapping("upload")
- public void upload(HttpServletRequest request, HttpServletResponse response) {
- try {
- String savePath = "C://uploads/";
- File dirPath = new File(savePath);
- if (!dirPath.exists()) {
- dirPath.mkdirs();
- }
- // 解析器解析request的上下文
- CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
- request.getSession().getServletContext());
- // 先判断request中是否包涵multipart类型的数据,
- if (multipartResolver.isMultipart(request)) {
- // 再将request中的数据转化成multipart类型的数据
- MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
- @SuppressWarnings("rawtypes")
- Iterator iter = multiRequest.getFileNames();
-
- while (iter.hasNext()) {
- MultipartFile file = multiRequest.getFile((String) iter.next());
- if (file != null) {
- String fileName = file.getOriginalFilename();
- System.out.println(fileName);
- File localFile = new File(savePath + fileName);
- // 写文件到本地
- file.transferTo(localFile);
- response.getWriter().print(fileName+" 上传成功!");
- }
- }
- }
-
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }