首页 > 代码库 > springMVC+uploadify3.1 文件上传 demo
springMVC+uploadify3.1 文件上传 demo
uploadify3.1 api 可参考:(点击打开链接)
需要springmvc的jar包
1、upload.jsp(主要代码)
[html] view plain copy
print?
- <script type="text/javascript" charset="UTF-8"
- src=http://www.mamicode.com/"${pageContext.request.contextPath}/js/jquery1.11.js"></script>
- <script type="text/javascript" charset="UTF-8"
- src=http://www.mamicode.com/"${pageContext.request.contextPath}/static/llsfw/js/upload/upload.js"></script>
- <script type="text/javascript" charset="UTF-8"
- src=http://www.mamicode.com/"${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=http://www.mamicode.com/"javascript:$(‘#file_upload‘).uploadify(‘upload‘,‘*‘)">上传</a>
- <a href=http://www.mamicode.com/"javascript:$(‘#file_upload‘).uploadify(‘stop‘)">取消上传</a>
- <div id="uploader_queue"></div>
- <div id="uploader_msg"></div>
- <div id="uploader_view"></div>
- </div>
2、upload.js
[javascript] view plain copy
print?
- $(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
[java] view plain copy
print?
- 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();
- }
- }
- }
springMVC+uploadify3.1 文件上传 demo
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。