plupload上传插件在SpringMVC中的整合
前言:近期在给学院的站点做一个加入附件的功能,首先到了某某邮箱看了一下。简单有用。可是是flash做的,无法拷贝。就仅仅好上网找插件了。经过筛选。最终找到plupload这款插件(其实有的编辑器自带加入附件功能)。官网仅仅有PHP版本号。后来各种百度谷歌。找到的资料都用点小问题,拼搏一天最终实现了功能,以下就把遇到的问题和重点部分写出来。希望遇到相同问题的同学能够參考。
首先你须要下载plupload插件,下载地址:http://www.plupload.com/download/
JSP页面配置例如以下:
- <!-- 配置界面上的css -->
- <link rel="stylesheet" type="text/css" href="<%=basePath%>plupload/js/jquery.plupload.queue/css/jquery.plupload.queue.css">
- <script type="text/javascript" src="<%=basePath%>js/jquery-1.9.1.min.js"></script>
- <script type="text/javascript" src="<%=basePath%>plupload/js/plupload.full.min.js"></script>
- <script type="text/javascript" src="<%=basePath%>plupload/js/jquery.plupload.queue/jquery.plupload.queue.js"></script>
- <!-- 国际化中文支持 -->
- <script type="text/javascript" src="<%=basePath%>plupload/js/i18n/zh_CN.js"></script>
- <script type="text/javascript">
- // Initialize the widget when the DOM is ready
- $(function() {
- // Setup html5 version
- function plupload(){
- $("#uploader").pluploadQueue({
- // General settings
- runtimes : 'flash,html5,gears,browserplus,silverlight,html4',
- url : "<%=basePath%>upload",
- //unique_names: true,
- chunk_size : '1mb',
- //rename : true,
- dragdrop: true,
- filters : {
- // Maximum file size
- max_file_size : '10mb',
- // Specify what files to browse for
- mime_types: [
- {title : "Image files", extensions : "jpg,gif,png"},
- {title : "Zip files", extensions : "zip"}
- ]
- },
- // Resize images on clientside if we can
- resize: {
- width : 200,
- height : 200,
- quality : 90,
- crop: true
- // crop to exact dimensions
- }, // Flash settings
- flash_swf_url : '<%=basePath%>plupload/js/Moxie.swf',
- // Silverlight settings
- silverlight_xap_url : '<%=basePath%>plupload/js/Moxie.xap' ,
- // 參数
- multipart_params: {'user': 'Rocky', 'time': '2012-06-12'}
- });
- }
- plupload();
- $('#Reload').click(function(){
- plupload();
- });
- });
- </script>
- <div style="width:750px; margin:0px auto;">
- <div id="uploader">
- <p>您的浏览器未安装 Flash, Silverlight, Gears, BrowserPlus 或者支持 HTML5 .</p>
- </div>
- <input value="继续上传" id="Reload" type="button">
- </div>
* 1024;
false : true);
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.web.multipart.MultipartFile;
- /**
- * Plupload是一个上传插件。
- * 这是一个bean类,主要存储Plupload插件上传时须要的參数。
- * 属性名不可任意修改.
- * 这里主要使用MultipartFile文件上传方法
- */
- public class Plupload {
- /**文件暂时名(打文件被分解时)或原名*/
- private String name;
- /**总的块数*/
- private int chunks = -1;
- /**当前块数(从0開始计数)*/
- private int chunk = -1;
- /**HttpServletRequest对象,不能直接传入进来,须要手动传入*/
- private HttpServletRequest request;
- /**保存文件上传信息,不能直接传入进来。须要手动传入*/
- private MultipartFile multipartFile;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getChunks() {
- return chunks;
- }
- public void setChunks(int chunks) {
- this.chunks = chunks;
- }
- public int getChunk() {
- return chunk;
- }
- public void setChunk(int chunk) {
- this.chunk = chunk;
- }
- public HttpServletRequest getRequest() {
- return request;
- }
- public void setRequest(HttpServletRequest request) {
- this.request = request;
- }
- public MultipartFile getMultipartFile() {
- return multipartFile;
- }
- public void setMultipartFile(MultipartFile multipartFile) {
- this.multipartFile = multipartFile;
- }
- }
最后就是測试类了,这部分不是重点。比較粗糙
- import java.io.File;
- import java.io.IOException;
- 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.bind.annotation.RequestMethod;
- @Controller
- public class uploadAction {
- public static final String FileDir = "uploadfile/";
- /**上传界面*/
- @RequestMapping("/uploadui")
- public String uploadUI() {
- return "login.upload";
- }
- /**上传处理方法*/
- @RequestMapping(value="/upload", method = RequestMethod.POST)
- public String upload(Plupload plupload,HttpServletRequest request, HttpServletResponse response) {
- //System.out.println(plupload.getChunk() + "===" + plupload.getName() + "---" + plupload.getChunks());
- plupload.setRequest(request);
- //文件存储路径
- File dir = new File(plupload.getRequest().getSession().getServletContext().getRealPath("/") + FileDir);
- System.out.println(dir.getPath());
- try {
- //上传文件
- PluploadUtil.upload(plupload, dir);
- //推断文件是否上传成功(被分成块的文件是否所有上传完毕)
- if (PluploadUtil.isUploadFinish(plupload)) {
- System.out.println(plupload.getName() + "----");
- }
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return "login.upload";
- }
- }
好了,到此重点部分的工作算是完毕了,须要提醒一下的就是,在前台JS部分有一个rename參数,假设设置成true,后台就获取不到文件的真实名称了。后台获取真实名称也不在是MultipartFile的getOriginalFilename()方法了,由于被分块的文件名称称会改变,而须要使用Plupload.getName()获取。
附上上传成功界面图(这里把设置的文件是没有限制):
至于后面的怎样将状态码以JOSN形式返回与前台进行交互和文件大小一次性上传大小的限制。就在往后用到了在详述吧。
以上观点均为个人意见,欢迎指正与批评。
plupload上传插件在SpringMVC中的整合的更多相关文章
- Plupload上传插件中文文档
Plupload上传插件中文帮助文档 如有疑问,加群交流:646104701 下载地址:https://www.plupload.com/download/ 配置参数 实例化一个plupload对象时 ...
- Plupload上传插件简单整理
Plupload Plupload是有TinyMCE的开发者开发的,为您的内容管理系统或是类似上传程序提供一个高度可用的上传插件.Plupload 目前分为一个核心API 和一个jQuery上传队列部 ...
- Plupload上传插件中文帮助文档
Plupload上传插件中文帮助文档 配置参数 实例化一个plupload对象时,也就是 new plupload.Uploader(),需要传入一个对象作为配置参数.后面内容中出现的plupload ...
- Plupload上传插件自定义图片的修改
若自定义的一个上传图片效果,代码(可能不全),当用户再次点击所有或任意一个上传图片的input时,uploader.files已经多了客户再次上传的图片,但是你就想要最后的两张图片,这就可以使用到up ...
- jquery plupload上传插件
http://www.jianshu.com/p/047349275cd4 http://www.cnblogs.com/2050/p/3913184.html demo地址: http://chap ...
- Plupload上传实例《模仿微云上传实例》,带源码
Plupload上传实例<模仿微云上传实例>,带源码,作者:鱼塘总裁 如有疑问,加群交流:646104701 一.实例截图 1.上传过程 2.上传成功 3.上传失败 4.最小化 二.所需文 ...
- 关于Plupload结合上传插件jquery.plupload.queue的使用
之前使用过很多的上传组件,但对各种浏览器的兼容性太差,不得不放弃!! plupload 是款很强大的上传组件,不得不推荐.plupload 前端根据浏览器不同选择使用Html5. Gears, Sil ...
- plupload上传视频插件jQuery+php
我在网上找到一个很好的视频上传插件,经过我的一些整理.补充,在这里分享给大家. 这个视频插件是新浪微博plupload上传视频插件,支持格式有mpg,m4v,mp4,flv,3gp,mov,avi,r ...
- 文件上传插件Uploadify在Struts2中的应用,完整详细实例
—>最近由于项目需要使用到一个上传插件,在网上发现uploadify挺不错,所以决定使用它,但是官网文档和例子是php的,而项目是SSI框架的,所以自己对uploadify在struts2中的使 ...
随机推荐
- effective c++:引用传递与值传递,成员函数与非成员函数
以pass-by-reference-to-const 替换pass-by-value 考虑以下class继承体系 class Person { public: Person(); // parame ...
- 为网页设计师准备的30个使用的HTML5框架
原文地址:http://www.goodfav.com/zh/html5-framework-8189.html 网页设计师在开始使用一些应用程序之前需要考虑几个事实,以确保在应用Web程序框架时,这 ...
- .net调用java webservice基于JBOSS服务器 学习笔记(一)
1.遇到数组类型或List等复杂数据类型是,需要对其进行包装,就是将复杂数据类型放到一个类里面: public class VOCargoJTWS { /** JT列表 */ private List ...
- bzoj3717: [PA2014]Pakowanie
Time Limit: 90 Sec Memory Limit: 256 MBSubmit: 128 Solved: 43[Submit][Status][Discuss]Description 你有 ...
- error while loading shared libraries: lib******: cannot open shared object file: No such file or directory
程序编译成功后,运行时错误: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object fi ...
- 原生JS默认设置默认值的写法
json=json||{};json.type=json.type||'get';json.data=json.data||{};json.time=json.time||2000;
- C#.Net 导出Excel 之单元格 相关设置
range.NumberFormatLocal = "@"; //设置单元格格式为文本range = (Range)worksheet.get_Range("A1 ...
- Extjs4.2纯前台导出Excel总结
前段时间做了两个项目,用到了Extjs4.2纯前台导出Excel,遇到很多的问题,从网上也找了很多资料,在这里总结一下,做一个记录: 使用方法: 1.下载extexcel文件包,这里可以下载http: ...
- java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
http://blog.csdn.net/chenghui0317/article/details/9531171 —————————————————————————————————————————— ...
- to_number,Extract oracle的关键字
to_number(Extract(year from 字段名)) 简介:获取时间字段的年份后转换为数字