//这是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();
}
}
分享到:
相关推荐
jquery之Uploadify实现的带进度条的文件上传
jsp,servlet,jquey最新版的jquery uploadify多文件上传 采用最新 版本的jquery uploadify。 有注释。
Jquery Uploadify上传带进度条 可以用来上传大文件,代码精简易懂。
jquery+uploadify3.2实现多文件上传并且有进度(IE,FF,Google浏览器兼容),本人已经通过IE,火狐,google浏览器测试通过了。将项目直接导入myeclipse下就可以直接运行!(IE,FF,Google浏览器兼容) 附件大小...
Uploadify多文件上传+servlet 一个简单的例子
HTML5 PHP jquery uploadify上传文件,带进度条,author:吕大豹。仿照uploadify写的,www.codesc.net已做过修正,本例的配置参数均与uploadify官网一致,参照官网的api就可以了,直接把文件夹上传到支持php的服务器...
JQuery的Uploadify实现多文件上传,最基本的jsp+servlet方式
jsp使用uploadify插件实现文件上传的示例,测试时将uploadify 直接导入myeclipse就能用了。 uploadify是一个与jquery和flash结合实现的上传程序,界面挺漂亮,用起来也方便,不过浏览器需要安装有Flash插件,好像...
jquery上传插件uploadify+php完美实现强大的文件上传功能实例+程序_【PHP】.rar
struts2 +jquey uploadify3 2 实现多文件上传 可以运行的myeclipse工程 绝对好用 访问方式http: 127 0 0 1:8080 Struts2Uploadify upload jsp uploadify 使用说明: <a href "javascript:$ "#file ...
uploadify多文件上传例子代码,jsp+servlet实现
最基本的jquery uploadify 的文件上传,struts2版。其中包含jquery uploadify资源包。保证代码的运行。拿出来和大家分享。
使用servlet结合jquery.uploadify 上传附件
Jquery 多文件上传,jquery+flash 多文件上传,界面美观, 很炫的上传组件,支持中文! 本实例经过严格测试,保证能正常使用!网上其他好多实例都不能正常运行。 uploadify是一款容量小、功能强的Js批量上传工具,...
uploadify3 uploadify2 jsp 文件上传 进度条,官网提供的是php版,这里我改为jsp了,并且uploadify版本2和最新版本3都有。
仿uploadify HTML5 jquery多文件上传 进度条,仿照uploadify写的,但是官方的html5版本是要收费的,索性自己动手写,配置参数均与uploadify官网一致,直接把文件夹上传到支持php的服务器即可,上传的文件保存在...
程序进过本人亲测,附件中含SQL,用的是MySQL数据库,实现了带进度条的多文件上传,并提供下载功能,解压后可直接导入Eclipse,希望对你有所帮助。
这是根据uploadify3 2结合struts2搭建的文件上传环境 可以直接导入eclipse运行 每步实现基本都加了注释 以下是我碰到的问题: 1 判断session是否失效 本实例没测试这个问题 但在工作项目中碰到了 但原因在这里...
最近要用Servlet/JSP做一个上传功能demo供新人学习。我采用的是Apache提供的commons-fileupload jar包,以及jquery uploadify插件组合实现多文件上传,单文件上传等。
uploadify2 uploadify3 jsp 文件 上传 进度条,官网提供的是php版,我改为jsp版了,并且版本2和版本3都有哦,分享给大家,文档可参考官网的http://www.uploadify.com/documentation/