jsp+servlet+jquery 用jquery uploadify最新版本实现多文件上传
//这是script代码
<link rel="stylesheet" type="text/css" href="uploadify/uploadify.css">
<script type="text/javascript" src="jquery-1.8.0.js"></script>
<script type="text/javascript" src="uploadify/jquery.uploadify.min.js"></script>
<script>
$(document).ready(function() {
$('#file_upload').uploadify( {
'debug':'true',
'swf' : 'uploadify/uploadify.swf',//上传按钮的图片,默认是这个flash文件
'uploader' : 'LoginServlet',//上传所处理的服务器
'cancelImg' : 'uploadfiy/uploadify-cancel.png',//取消图片
'method':'post',
'folder' : '/UploadFile',//上传后,所保存文件的路径
'queueID' : 'fileQueue',//上传显示进度条的那个div
'buttonText' : '请选择文件',
//'onUploadComplete': function(file){alert('The file'+file.name+'finished processing!')},//每个文件上传成功后的函数
progressData : 'percentage',
'auto' : false,
'multi' : true,
//'onSelect':function(file){
//alert("文件"+file.name+"被选择了!");
//}
//'onQueueComplete' : function(queueData) {
// alert(queueData.filesQueued + 'files were successfully!')
//},//当队列中的所有文件上传成功后,弹出共有多少个文件上传成功
'onDisable' : function() {
alert('uploadify is disable');
},//在调用disable方法时候触发
//'onCancel':function(){alert('你取消了文件上传')}
//'onUploadStart' : function(file) {//在调用上传前触发
//alert('The file ' + file.name + ' is being uploaded.')}
'onError' : function(errorType,errObj) {
alert('The error was: ' + errObj.info)
} });
}); </script>
//这是表单元素
<div id="fileQueue"></div>
<input id="file_upload" name="file_upload" type="file" multiple="true">
<p>
<!-- 加上“*”表示当第一个文件上传成功后,立即上传以后队列中的文件,否则需要自己手动 -->
<a href="javascript:$('#file_upload').uploadify('upload','*')">上传</a>|
<a
href="javascript:$('#file_upload').uploadify('cancel',$('.uploadifive-queue-item').first().data('file'))">取消上传</a>
<a href="javascript:$('#file_upload').uploadify('cancel','*')">清空所有的上传文件</a>
<a href="javascript:$('#file_upload').uploadify('stop','*')">暂停</a>
<!-- 如果填入true则表示禁用上传按钮 -->
<a href="javascript:$('#file_upload').uploadify('disable','true')">禁用</a>
<a href="javascript:$('#file_upload').uploadify('debug')">调试</a>
</p>
//后台servlet
package com.accp.upload;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
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 LoginServlet extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8;");
doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8;");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
// 设置接收的编码格式
request.setCharacterEncoding("UTF-8");
Date date = new Date();// 获取当前时间
SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat sdfFolder = new SimpleDateFormat("yyMM");
String newfileName = sdfFileName.format(date);// 文件名称
String fileRealPath = "";// 文件存放真实地址 String fileRealResistPath = "";// 文件存放真实相对路径 // 名称 界面编码 必须 和request 保存一致..否则乱码
String name = request.getParameter("name"); String firstFileName = "";
// 获得容器中上传文件夹所在的物理路径
String savePath = this.getServletConfig().getServletContext()
.getRealPath("/")
+ "uploads\\" + newfileName + "\\";
System.out.println("路径" + savePath + "; name:" + name);
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) {
System.out.println("xxxxxxxxxxxxx");
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 + newfileName + formatName;// 文件存放真实地址 BufferedInputStream in = new BufferedInputStream(item
.getInputStream());// 获得文件输入流
BufferedOutputStream outStream = new BufferedOutputStream(
new FileOutputStream(new File(fileRealPath)));// 获得文件输出流
Streams.copy(in, outStream, true);// 开始把文件写到你指定的上传文件夹
// 上传成功,则插入数据库
if (new File(fileRealPath).exists()) {
// 虚拟路径赋值
fileRealResistPath = sdfFolder.format(date)
+ "/"
+ fileRealPath.substring(fileRealPath
.lastIndexOf("\\") + 1);
// 保存到数据库
System.out.println("保存到数据库:");
System.out.println("name:" + name);
System.out.println("虚拟路径:" + fileRealResistPath);
} }
}
}
} catch (org.apache.commons.fileupload.FileUploadException ex) {
ex.printStackTrace();
System.out.println("没有上传文件");
return;
}
response.getWriter().write("1");
out.flush();
out.close(); } }


jsp+servlet+jquery 用jquery uploadify最新版本实现多文件上传的更多相关文章
- jquery ajax file upload NET MVC 无刷新文件上传
网上有各种各样的文件上传方法,有基于JS框架的.也有基于flash swf插件的. 这次分享一个比较简单而且实用能快速上手的文件上传方法,主要步骤: 1.引用Jquery包,我用的是jquery-1. ...
- springboot整合web开发(整合servlet、filter、listener、访问静态、文件上传)
整合servlet 1.继承HttpServlet 2.添加@WebServlet注解 @WebServlet(name="FirstServlet",urlPatterns=&q ...
- SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)
SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...
- Java Servlet异步处理、非阻塞I/O和文件上传
异步处理 应用服务器中的 web容器通常对各个客户端情求分别使用一个服务器线程.在工作负载很繁重的情况下,容器常要大量线程来为所有客户端请求服务.可扩展性限制包括内存用尽,或容器线程池耗尽.为了创建可 ...
- JSP-Runoob:JSP 文件上传
ylbtech-JSP-Runoob:JSP 文件上传 1.返回顶部 1. JSP 文件上传 JSP 可以与 HTML form 标签一起使用,来允许用户上传文件到服务器.上传的文件可以是文本文件或图 ...
- Java基础——Servlet(八)文件上传下载
一.简单的文件上传常见的组件Smartupload , Apache 的 commons FileUploadSmartupload上传的步骤: 1.初始化上传上下文 2.准备上传 3.保存文件 &l ...
- 文件上传利器JQuery上传插件Uploadify
在做日常项目中,经常在后台需要上传图片等资源文件,之前使用过几次这个组件,感觉非常好用 ,但是每次使用的时候都是需要经过一番查阅,所以还不如记住在这里,以后使用的时候就翻翻. 他的官方网站如下:htt ...
- 关于jquery文件上传插件 uploadify 3.1的使用
要使用uplaodify3.1,自然要下载相应的包,下载地址http://www.uploadify.com/download/,这里有两种包,一个是基于flash,免费的,一个是基于html5,需要 ...
- jquery uploadify文件上传插件用法精析
jquery uploadify文件上传插件用法精析 CreationTime--2018年8月2日11点12分 Author:Marydon 一.参数说明 1.参数设置 $("#fil ...
随机推荐
- submit和button的区别
两者主要区别在于:submit可以提交表单(form),而button如果不指定onclick等事件处理函数,它是不做任何事情的.注意哦,在页面上<input type="submit ...
- 【转载】MyBatis之传入参数
原文地址:http://blog.csdn.net/liaoxiaohua1981/article/details/6862764 在MyBatis的select.insert.update.dele ...
- Poj 2528 Mayor's posters 分类: Brush Mode 2014-07-23 09:12 84人阅读 评论(0) 收藏
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40570 Accepted: 11798 ...
- matrix_2015_1 138 - ZOJ Monthly, January 2015
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3844 第一个,n个数,每次操作最大数和最小数都变成他们的差值,最后n个数相 ...
- Sqlite中使用rowid来表示行号,用于分页。
在SQLite的查询结果中显示行号,可以使用select rowid as RowNumber ,* from WSCLanguage: select rowid as RowNumber ,* fr ...
- jQuery中的height()、innerheight()、outerheight()的区别总结
在前端jQuery代码中突然看到outerheight(),第一感觉就是,这是什么鬼?然后仔细查阅了一下,居然发现还有这么多相似的东西. 在jQuery中,获取元素高度的函数有3个,它们分别是heig ...
- 首次发布App,In-App Purchase 无法submit for review 问题的解决方案
原地址:http://blog.csdn.net/blucenong/article/details/7819195 一个IDP首次create app 然后首次create new IAP的时候,我 ...
- js弹出图片原图效果
1.将js方法独立出来common.js function openwin(src){ var basePath = document.getElementById("basePath&qu ...
- 充分发挥异步在 ASP.NET 中的强大优势
作者:Brij Bhushan Mishra 最近几年,异步编程受到极大关注,主要是出于两个关键原因:首先,它有助于提供更好的用户体验,因为不会阻塞 UI 线程,避免了处理结束前出现 UI 界面挂起. ...
- Android开发者:你真的会用AsyncTask吗?
[导读]在Android应用开发的过程中,我们需要时刻注意保证应用程序的稳定和UI操作响应及时,因为不稳定或响应缓慢的应用将给应用带来不好的印象,严重的用户卸载你的APP,这样你的努力就没有体现的价值 ...