关于uploadify
来源:互联网 发布:js传值给jsp页面 编辑:程序博客网 时间:2024/06/16 07:48
Jsp
/* 上传商品详细图片 */ $("#productPic").uploadify({ 'uploader' : '<%=request.getContextPath()%>/js/uploadify/uploadify.swf', 'script' : '<%=request.getContextPath()%>/ProductImage/ProductImage_upload',//后台处理的请求 'cancelImg' : '<%=request.getContextPath()%>/js/uploadify/uploadify-cancel.png', 'folder' : 'upload', //您想将文件保存到的路径 'fileDataName' : 'uploadify', //和input的name属性值保持一致就好,Struts2就能处理了 'queueID' : 'image', //与下面的id对应 'method' :'GET', //如果要传参数,就必须改为GET 'queueSizeLimit' : 1, //上传6张 'fileDesc' : '*.jpg,*.png,*.gif,*.JPEG,*.PNG,*.GIF', 'fileExt' : '*.jpg;*.gif;*.jpeg;*.png;*.bmp', //控制可上传文件的扩展名,启用本项时需同时声明fileDesc /* 'fileDesc' : '*.zip,*.rar', 'fileExt' : '*.zip;*.rar', */ 'auto' : true, //自动上传 'multi' : false, 'simUploadLimit' : 1, //上传个数 'buttonText' : '浏览', //'buttonImg' :'${ctx}/images/browse2.png', 'displayData' : 'speed', //有speed和percentage两种,一个显示速度,一个显示完成百分比 //'scriptData' : {'type':'product'}, onOpen: function(event, queueID, fileObj) { if(fileObj.size==0){ alert("文件 " +fileObj.name +"内容为空取消其上传!"); $("#productPic").uploadifyCancel(queueID); } if(fileObj.size / 1024 / 1024 > fileSize){ alert("文件 " +fileObj.name +"内容大于"+fileSize+"M,取消其上传!"); $("#productPic").uploadifyCancel(queueID); } if(fileObj.type != '.jpg' &&fileObj.type != '.png' &&fileObj.type != '.gif' &&fileObj.type != '.JPEG' &&fileObj.type != '.PNG' && fileObj.type != '.GIF'){ alert("请您选择jpg、png、gif、JPEG、PNG、GIF格式的文件!"); $("#productPic").uploadifyCancel(queueID); } }, onComplete : function(event,queueID, fileObj, response,data) { $("#path").val(response); $("#path").siblings("img").attr("src",response); $("#pathmng").attr("style","color:black"); $("#msg").text(""); } });
Java代码:
private File uploadify; public File getUploadify() { return uploadify; } public void setUploadify(File uploadify) { this.uploadify = uploadify; } private String uploadifyFileName; public String getUploadifyFileName() { return uploadifyFileName; } public void setUploadifyFileName(String uploadifyFileName) { this.uploadifyFileName = uploadifyFileName; } /** * 判上传图片,生成唯一的UUID */ public String upload() throws Exception{ HttpServletResponse response = ServletActionContext.getResponse(); HttpServletRequest request = ServletActionContext.getRequest(); response.setCharacterEncoding("utf-8"); // 项目路径 String path = request.getSession().getServletContext().getRealPath(""); // 项目上一级路径 path = path.substring(0,path.lastIndexOf(File.separator)); String url ="/"+this.getPath("uploads")+"/images/"; Date date = new Date(); SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd"); url += df.format(date);// 文件夹--日期 url += "/"; File f2 = new File(path+url); if (!f2.exists()) { f2.mkdirs(); } String name = ""; String extName = ""; // 获取扩展名 if (uploadifyFileName.lastIndexOf(".") >= 0) { extName = uploadifyFileName.substring(uploadifyFileName.lastIndexOf(".")); } File file = null; String image200px=""; do { name = UUID.randomUUID().toString(); file = new File( path + url + name + extName); } while (file.exists()); CopyFileUtil.copyFile(uploadify.getPath(),file.getPath(), true); //生成原图 uploadify.delete(); String Path200px = ""; //生成200px的图 if(extName.equalsIgnoreCase(".jpg") || extName.equalsIgnoreCase(".png") || extName.equalsIgnoreCase(".gif") || extName.equalsIgnoreCase(".JPEG") || extName.equalsIgnoreCase(".PNG") || extName.equalsIgnoreCase(".GIF")){ String fileS =path + url + name + extName; int point = fileS.lastIndexOf("."); image200px = fileS.substring(0, point) + "new" + extName;//(列表页搜索结果图) FileUtility.generateImageThumb(fileS, image200px, 200, 200); //利用原图截取200px的图 File f=new File(fileS); //删除原图片 f.delete(); Path200px=image200px.substring(image200px.indexOf("/"), image200px.length()); } response.getWriter().print(Path200px); return null; } /** * uploadify压缩包上传 */ public String fileUpload() throws Exception{ HttpServletResponse response = ServletActionContext.getResponse(); HttpServletRequest request = ServletActionContext.getRequest(); response.setCharacterEncoding("utf-8"); String path = request.getSession().getServletContext().getRealPath(""); path = path.substring(0,path.lastIndexOf(File.separator)); String url ="/"+this.getPath("uploads")+"/images/"; String savePath = path + url; File f1 = new File(savePath); if (!f1.exists()) { f1.mkdirs(); } DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("utf-8"); List fileList = null; try { fileList = upload.parseRequest(request); //获取到上传的东西 } catch (FileUploadException ex) { ex.printStackTrace(); System.out.println(1); return ""; } Iterator<FileItem> it = fileList.iterator(); String name = ""; String extName = ""; while (it.hasNext()) { FileItem item = it.next(); if (!item.isFormField()) { long size = item.getSize(); String type = item.getContentType(); name = item.getName(); if (name == null || name.trim().equals("")) { continue; } // 扩展名格式: if (name.lastIndexOf(".") >= 0) { extName = name.substring(name.lastIndexOf(".")); } File saveFile = new File(savePath + name); try { item.write(saveFile); } catch (Exception e) { e.printStackTrace(); } } } return savePath + name; }
0 0
- 关于uploadify
- 关于uploadify的一些心得
- Uploadify
- uploadify
- Uploadify
- uploadify
- Uploadify
- uploadify
- 关于Uploadify文件上传 io error
- 关于uploadify的多文件上传控件
- 关于上传插件uploadify使用问题
- 关于uploadify导致chrome页面崩溃
- 关于uploadify插件的一点坑坑洼洼
- 关于Uploadify 不显示效果原因
- 关于文件上传jQuery插件jquery.uploadify的使用心得
- 关于uploadify在Firefox下session丢失的问题
- 关于uploadify插件的使用,导入excel03和07版本。
- 关于uploadify插件上传之chome浏览器兼容问题的解决办法.
- JavaSwing拼图游戏与源码
- ArrayList中的toArray()和toArray(T[] a)
- sync.WaitGroup的错误用法与纠正
- 蓝桥杯_剪邮票_暴力+广搜
- 《java-正则表达式提取复杂短信验证码》-(用户:654321,的验证码是:【123456】)
- 关于uploadify
- React-Native 背景图片全屏显示及登录页面
- 数据的规范化,归一化,标准化,正则化
- FORM表单及其属性
- mui单页面指定显示的是单页面的那个页面,获取地址栏参数
- go语言学习
- RabbitMQ Windows7下单机多节点安装
- HDU
- 1013. Battle Over Cities (25)