此文是jQuery + SWFUpload + COS的上传插件源代码,使用了jquery1.4、SWFUpload2.2和COS来制作jquery上传插件。
1、引入必须的js文件和css文件:
代码如下 | 复制代码 |
<!--引入jquery插件,其它版本也可以--> <script type="text/javascript" src="jquery-1.4.2.min.js"></script> <!--jquery插件vinSwfUpload的js--> <script type="text/javascript" src="vinSWFUpload-1.2.js"></script> <!--SwfUpload核心js--> <script type="text/javascript" src="core/SWFUpload-2.2.js"></script> <!--jquery插件vinSwfUpload的核心css--> <link href="css/vinSwfUpload-1.2.css" rel="stylesheet" type="text/css"/> <!--jquery插件vinSwfUpload的皮肤css--> <link href="css/vinSwfUpload-1.2-skin.css" rel="stylesheet" type="text/css"/> |
2、在body中添加组件显示目标:
代码如下 | 复制代码 |
<div id="vinEdit"></div> |
3、初始化插件:
代码如下 | 复制代码 |
<script type="text/javascript"> $(function() { var path = "<%=path%>/vinSwfUpload-1.2/"; var upload = $("#vinEdit").vinSWFUpload( { uploadURL:'<%=path%>/swf/upload', width : 500, heigth : 300, sizeUnit:"M", skin:"green", autoRemoveStoped:false, flashURL : path+"core/SWFUpload.swf", addImgURl:path+"images/add.gif", uploadImgURl:path+"/images/upload.png", stopImgURl:path+"images/stop.png", deleteImgURl:path+"images/trash.gif", tipImgURl:path+"images/tip.gif" }); }); </script> |
4、所需的上传类:
代码如下 | 复制代码 |
DefaultFileRenamePolicy rename = generateFileRenamePolicy(); MultipartRequest multi = null; try{ multi = new MultipartRequest(request, parameters.getSaveDirectory(), parameters.getMaxPostSize(),parameters.getEncoding(), rename); renderJson(response,Constant.UPLOAD_SUCCESS); } catch (IOException e) { if(!BlankUtil.isBlank(e.getMessage())){ int index = e.getMessage().indexOf("Posted content length of"); if(index>-1){ renderJson(response,Constant.FILE_OVERSETSIZE); }else{ if(e instanceof IOException){ renderJson(response,Constant.IO_ERROR); }else{ renderJson(response,Constant.UPLOAD_FAILED); } } logger.error(e.getMessage(),e.getCause()); }else{ logger.info("--- 文件上传失败 ---"); deleteErrorFile(parameters.getSaveDirectory()+rename.getFileSaveName()); } return; } |
5、在web.xml中配置上传类
代码如下 | 复制代码 |
<servlet> <servlet-name>swfUploader</servlet-name> <servlet-class>com.upload.swf.SwfUploadServlet</servlet-class> <!--上传文件大小上限 单位M--> <init-param> <param-name>FILE_SIZE_LIMIT</param-name> <param-value>10</param-value> </init-param> <!--一次性上传的文件个数上限--> <init-param> <param-name>FILE_COUNT_LIMIT</param-name> <param-value>10</param-value> </init-param> <!--请求响应编码格式--> <init-param> <param-name>ENCODING</param-name> <param-value>utf-8</param-value> </init-param> <!--是否使用自定义目录--> <init-param> <param-name>USE_CUSTOMDIR</param-name> <param-value>false</param-value> </init-param> <!--自定义的文件保存目录--> <init-param> <param-name>ROOT_UPLOADDIR</param-name> <param-value>rootUpload</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>swfUploader</servlet-name> <url-pattern>/swf/upload</url-pattern> </servlet-mapping> |
如果一切顺利的话,你将看到如下的运行效果图
时间: 2024-09-27 10:24:06