SWFUpload官方的例子都是PHP的,在这里提供一个Java版的最简单的使用例子,使用JSP页面完成所有操作。
?
实现上传,分为三步:
1、JavaScript设置SWFUpload部分(与官方例子类似):
?
- ?var?upload;??
- ??
- ?window.onload?=?function()?{??
- upload?=?new?SWFUpload({??
- ??
- upload_url:?"${pageContext.request.contextPath}/swfupload/example.jsp?upload=1",??????
- //?上传文件限制设置??
- file_size_limit?:?"10240",????
- file_types?:?"*.jpg;*.gif;*.png",?????
- file_types_description?:?"Image?Files",??
- file_upload_limit?:?"0",248); line-height:18px"> file_queue_limit?:?"1",0); background-color:inherit">//?事件处理设置(所有的自定义处理方法都在handler.js文件里)??
- file_dialog_start_handler?:?fileDialogStart,??
- file_queued_handler?:?fileQueued,248); line-height:18px"> file_queue_error_handler?:?fileQueueError,248); line-height:18px"> file_dialog_complete_handler?:?fileDialogComplete,248); line-height:18px"> upload_start_handler?:?uploadStart,248); line-height:18px"> upload_progress_handler?:?uploadProgress,248); line-height:18px"> upload_error_handler?:?uploadError,248); line-height:18px"> upload_success_handler?:?uploadSuccess,248); line-height:18px"> upload_complete_handler?:?uploadComplete,0); background-color:inherit">//?按钮设置??
- button_image_url?:?"swfupload/xpbutton.png",??????
- button_placeholder_id?:?"spanButtonPlaceholder",248); line-height:18px"> button_width:?61,248); line-height:18px"> button_height:?22,0); background-color:inherit">//?swf设置??
- flash_url?:?"swfupload/swfupload.swf",248); line-height:18px"> custom_settings?:?{??
- ????progressTarget?:?"fsUploadProgress",248); line-height:18px"> ????cancelButtonId?:?"btnCancel"??
- },248); line-height:18px"> ??????
- ????debug:?false??
- });??
- ?}??
2、页面显示部分:
Html代码 ?

<div?class="flash"?id="fsUploadProgress"></div>??
- div?style="padding-left:?5px;" ?????span?id="spanButtonPlaceholder"spaninput?id="btnCancel"?type="button"?value="取消"?onclick="cancelQueue(upload);"???
- disabled="disabled"?style="margin-left:?2px;?height:?22px;?font-size:?8pt;"?/>??
- >??
3、Java处理文件上传部分:
Java代码 ?

?String?uploadSign?=?request.getParameter("upload");??
- ?String?rootPath?=?request.getParameter("rootPath");??
- ?String?path?=?request.getParameter("path");??
- ?if(rootPath?==?null)?rootPath?=?"";??
- ????rootPath?=?rootPath.trim();??
- if(rootPath.equals("")){??
- rootPath?=?application.getRealPath("/swfupload/files");??
- ?}??
- if(path?==?null)?{??
- path?=?rootPath;??
- ?}else{??
- path?=?new?String(Base64.decodeBase64(path.getBytes()));??
- ???
- if(null?!=?uploadSign?&&?!"".equals(uploadSign)){??
- ??FileItemFactory?factory?=?new?DiskFileItemFactory();??
- ??ServletFileUpload?upload?=?new?ServletFileUpload(factory);??
- ????
- ??try{??
- ??????List?items?=?upload.parseRequest(request);??
- ??????null?!=?items){??
- ??????????Iterator?itr?=?items.iterator();??
- ??????????while(itr.hasNext()){??
- ??????????????FileItem?item?=?(FileItem)itr.next();??
- ??????????????if(item.isFormField()){??
- ??????????????????continue;??
- ??????????????} ??????????????????????????????????????????
- ??????????????????SimpleDateFormat?sdf=new?SimpleDateFormat("yyyyMMddkkmmss");??
- ??????????????????String?type?=?item.getName().split(".")[1];??
- ??????????????????File?savedFile?=?new?File(path,sdf.format(new?Date())+"."+type);??
- ??????????????????item.write(savedFile);??
- ??????????????}??
- ??????????}??
- ??????}??
- ??}catch(Exception?e){??
- ??????e.printStackTrace();??
- ??}??
- ?}??
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|