最近弄了一个JQuery Uploadify上传的例子,分享一下。
先看下效果:
初始页面:
上传成功:
展示页面:
看代码:
add.jsp:
<form:form method="Post" action="/uploadDemo/admin/photo/add.spring" commandName="photoBean" id="photoBean_form" class="form-horizontal"> <fieldset> <div class="control-group"> <label class="control-label" for="focusedInput">NAME</label> <div class="controls"> <form:input class="input-xlarge focused" id="name" path="name" type="text" /> </div> </div> <div class="control-group"> <label class="control-label" for="focusedInput">IMAGE</label> <div class="controls"> <form:hidden path="path"/> <input id="fileUpload" name="fileUpload" multiple="true" /><img src="" id="imgId" style="display:none"/> </div> </div> <div class="form-actions"> <button type="submit" class="btn btn-primary">Save changes</button> </div> </fieldset> </form:form>
js:
<script type="text/javascript"> var imgId = Math.uuid(); $(document).ready(function() { $("#fileUpload").uploadify({ 'swf' : '../img/uploadify.swf', 'uploader' : '/uploadDemo/scripts/uploadify?name=' + imgId, height : 20, width : 120, 'queueSizeLimit' :1, 'fileTypeDesc' : 'png或者jpg', 'fileTypeExts' : '*.png;*.jpg', 'multi' : false, 'buttonText' : '上传', 'wmode' : 'transparent', onUploadSuccess:function(file,data,response){ $("#path").val('/uploads/' + imgId + file.type); showImg('/uploads/' + imgId + file.type); } }); }); function showImg(path) { $("#imgId").attr('src', '/uploadDemo/' + path); $("#imgId").show(); } </script>
上传的Servert,借鉴的是baidu出来的,具体网址忘了,作者别生气:
package com.pro.controller; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.disk.DiskFileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.util.Streams; public class Uploadify extends HttpServlet{ private static final long serialVersionUID = 1L; /** * 实现多文件的同时上传 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String fileRealPath = ""; String name = request.getParameter("name"); String firstFileName=""; String savePath = this.getServletConfig().getServletContext().getRealPath("/") + "uploads\\" ; File file = new File(savePath); if (!file.isDirectory()) { file.mkdirs(); } try { DiskFileItemFactory fac = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(fac); upload.setHeaderEncoding("UTF-8"); // 获取多个上传文件 List fileList = fileList = upload.parseRequest(request); // 遍历上传文件写入磁盘 Iterator it = fileList.iterator(); while (it.hasNext()) { Object obit = it.next(); if(obit instanceof DiskFileItem){ DiskFileItem item = (DiskFileItem) obit; // 如果item是文件上传表单域 // 获得文件名及路径 String fileName = item.getName(); if (fileName != null) { firstFileName=item.getName().substring(item.getName().lastIndexOf("\\")+1); String formatName = firstFileName.substring(firstFileName.lastIndexOf("."));//获取文件后缀名 fileRealPath = savePath + name + formatName;//文件存放真实地址 BufferedInputStream in = new BufferedInputStream(item.getInputStream());// 获得文件输入流 BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(fileRealPath)));// 获得文件输出流 Streams.copy(in, outStream, true);// 开始把文件写到你指定的上传文件夹 } } } } catch (org.apache.commons.fileupload.FileUploadException ex) { ex.printStackTrace(); System.out.println("没有上传文件"); return; } } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
相关推荐
Uploadify简单说来,是基于Jquery的一款文件上传插件。它的功能特色总结如下: 支持单文件或多文件上传,可控制并发上传的文件数 在服务器端支持各种语言与之配合使用,诸如PHP,.NET,Java…… 通过参数可配置...
jquery Uploadify jsp 官网例子中没有jsp 开始了解的人不知道怎么用jqueryUploadify可以看这个例子很简单
JQuery uploadify 实现文件批量上传完整例子,所有js,和uploadify插件js 都放在这个例子中
这是一个用php做的文件上传的例子,比较简单,可以运行通过,仅供参考,希望能给大家带来帮助。
超好看的中文按钮Jquery.uploadify上传例子带演示页面
完美uploadify php flash jquery结合的例子完美uploadify php flash jquery结合的例子完美uploadify php flash jquery结合的例子完美uploadify php flash jquery结合的例子完美uploadify php flash jquery结合的例子...
JAVA WEB工程,支持多文件上传各种文件,并显示各自的进度条。
jquery.uploadify-v2.1.4上传完整例子,很好用,很方便,很经典。
JQuery的Uploadify实现多文件上传,最基本的jsp+servlet方式
jquery xheditor是jquery中较好的编辑器插件,而jquery.uploadify也是jquery实现文件上传很优秀的插件,本项目实例解决了两个插件文件上传的问题,对于应用学习两个插件,实现文件上传,具有很好的参考价值,同时本例子是...
有人要例子,上传个呗有人要例子,上传个呗有人要例子,上传个呗有人要例子,上传个呗
使用Jquery.uploadify上传文件。...jquery有很多插件,其中也有不少上传文件的插件,像ajaxfileupload.js、uploadify.js,不过网上的例子几乎全是php的,很郁闷,研究了半天,终于把uploadify用jsp版本弄出来了
利用 jquery uploadify + java 做的批量上传的例子,可以直接运行
JQuery uploadify3.1 jsp servlet文件上传实例,内附源码,jsp+servlet实现,适合初学者,网络上很多的例子虽然说是3.1版本的,但是调用方法都是老版本的,跑不起来,经过查看doc,这个例子可以在tomcat中运行。...
基于flash的文件批量上传,根据官网文档写的一个使用java的小例子,除了JDK,其他的所需自愿均存在
今天下午整理文件上传的例子,感觉收集到的例子都很不人性话,后来找到一个还可以的,本来想改成类似于腾讯QQ相册那种方式,仔细看了一下是Flash的, 而且那个极速上传插件也不知道用什么做的?问了一下,说是什么cgi...
jquery上传时候需要的进度条 非常好的一个例子
uploadify jquery jsp servlet 可用 例子
这是一个配置成功的例子,大家下载后可以自由扩展!
这是两个基于django框架的例子,通过运用python脚本语言实现web前端与后台的链接。一个是做博客的,一个是使用uploadify插件的。觉得很有用,分享一下。