本次试验主要用到了activeMq和上传插件uploadify的知识,感谢以下两篇文章的作者。
1.http://itindex.net/detail/47160-java-jquery-%E4%B8%8A%E4%BC%A0
2.http://blog.csdn.net/jiuqiyuliang/article/details/47160259
本文中不再提供activeMq和uploadify的介绍。
项目结构
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>uploadifyDemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>com.dao.chu.upload.Upload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/servlet/Upload</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
文件中很多注释掉的代码有兴趣可以打开探究uploadify插件。
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Upload</title>
<!--装载文件-->
<link href="css/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="uploadify/jquery-1.9.1.js"></script>
<script type="text/javascript" src="uploadify/jquery.uploadify.min.js"></script>
<!--ready事件-->
<script type="text/javascript">
$(document).ready(function() {
$("#uploadify").uploadify({
'uploader' : 'servlet/Upload',
'swf' : 'uploadify/uploadify.swf',
'cancelImg' : 'img/uploadify-cancel.png',
'folder' : 'uploads',//您想将文件保存到的路径
'queueID' : 'fileQueue',//与下面的id对应
'queueSizeLimit' : 20,
'fileDesc' : 'rar文件或zip文件',
'fileExt' : '*.rar;*.zip', //控制可上传文件的扩展名,启用本项时需同时声明fileDesc
'auto' : false,
'multi' : true,
'simUploadLimit' : 2,
'buttonText' : '选择文件',
'onDialogOpen' : function() {//当选择文件对话框打开时触发
/* alert( 'Open!'); */
},
'onSelect' : function(file) {//当每个文件添加至队列后触发
/* alert( 'id: ' + file.id
+ ' - 索引: ' + file.index
+ ' - 文件名: ' + file.name
+ ' - 文件大小: ' + file.size
+ ' - 类型: ' + file.type
+ ' - 创建日期: ' + file.creationdate
+ ' - 修改日期: ' + file.modificationdate
+ ' - 文件状态: ' + file.filestatus); */
},
'onSelectError' : function(file,errorCode,errorMsg) {//当文件选定发生错误时触发
alert( 'id: ' + file.id
+ ' - 索引: ' + file.index
+ ' - 文件名: ' + file.name
+ ' - 文件大小: ' + file.size
+ ' - 类型: ' + file.type
+ ' - 创建日期: ' + file.creationdate
+ ' - 修改日期: ' + file.modificationdate
+ ' - 文件状态: ' + file.filestatus
+ ' - 错误代码: ' + errorCode
+ ' - 错误信息: ' + errorMsg);
},
'onDialogClose' : function(swfuploadifyQueue) {//当文件选择对话框关闭时触发
if( swfuploadifyQueue.filesErrored > 0 ){
alert( '添加至队列时有'
+swfuploadifyQueue.filesErrored
+'个文件发生错误n'
+'错误信息:'
+swfuploadifyQueue.errorMsg
+'n选定的文件数:'
+swfuploadifyQueue.filesSelected
+'n成功添加至队列的文件数:'
+swfuploadifyQueue.filesQueued
+'n队列中的总文件数量:'
+swfuploadifyQueue.queueLength);
}
},
'onQueueComplete' : function(stats) {//当队列中的所有文件全部完成上传时触发
/* alert( '成功上传的文件数: ' + stats.successful_uploads
+ ' - 上传出错的文件数: ' + stats.upload_errors
+ ' - 取消上传的文件数: ' + stats.upload_cancelled
+ ' - 出错的文件数' + stats.queue_errors); */
},
'onUploadComplete' : function(file,swfuploadifyQueue) {//队列中的每个文件上传完成时触发一次
/* alert( 'id: ' + file.id
+ ' - 索引: ' + file.index
+ ' - 文件名: ' + file.name
+ ' - 文件大小: ' + file.size
+ ' - 类型: ' + file.type
+ ' - 创建日期: ' + file.creationdate
+ ' - 修改日期: ' + file.modificationdate
+ ' - 文件状态: ' + file.filestatus); */
},
'onUploadError' : function(file,errorCode,errorMsg,errorString,swfuploadifyQueue) {//上传文件出错是触发(每个出错文件触发一次)
alert( 'id: ' + file.id
+ ' - 索引: ' + file.index
+ ' - 文件名: ' + file.name
+ ' - 文件大小: ' + file.size
+ ' - 类型: ' + file.type
+ ' - 创建日期: ' + file.creationdate
+ ' - 修改日期: ' + file.modificationdate
+ ' - 文件状态: ' + file.filestatus
+ ' - 错误代码: ' + errorCode
+ ' - 错误描述: ' + errorMsg
+ ' - 简要错误描述: ' + errorString);
},
'onUploadProgress' : function(file,fileBytesLoaded,fileTotalBytes,queueBytesLoaded,swfuploadifyQueueUploadSize) {//上传进度发生变更时触发
/* alert( 'id: ' + file.id
+ ' - 索引: ' + file.index
+ ' - 文件名: ' + file.name
+ ' - 文件大小: ' + file.size
+ ' - 类型: ' + file.type
+ ' - 创建日期: ' + file.creationdate
+ ' - 修改日期: ' + file.modificationdate
+ ' - 文件状态: ' + file.filestatus
+ ' - 当前文件已上传: ' + fileBytesLoaded
+ ' - 当前文件大小: ' + fileTotalBytes
+ ' - 队列已上传: ' + queueBytesLoaded
+ ' - 队列大小: ' + swfuploadifyQueueUploadSize); */
},
'onUploadStart': function(file) {//上传开始时触发(每个文件触发一次)
/* alert( 'id: ' + file.id
+ ' - 索引: ' + file.index
+ ' - 文件名: ' + file.name
+ ' - 文件大小: ' + file.size
+ ' - 类型: ' + file.type
+ ' - 创建日期: ' + file.creationdate
+ ' - 修改日期: ' + file.modificationdate
+ ' - 文件状态: ' + file.filestatus ); */
},
'onUploadSuccess' : function(file,data,response) {//上传完成时触发(每个文件触发一次)
/* alert( 'id: ' + file.id
+ ' - 索引: ' + file.index
+ ' - 文件名: ' + file.name
+ ' - 文件大小: ' + file.size
+ ' - 类型: ' + file.type
+ ' - 创建日期: ' + file.creationdate
+ ' - 修改日期: ' + file.modificationdate
+ ' - 文件状态: ' + file.filestatus
+ ' - 服务器端消息: ' + data
+ ' - 是否上传成功: ' + response); */
}
});
});
</script>
</head>
<body>
<div id="fileQueue"></div>
<input type="file" name="uploadify" id="uploadify" />
<p>
<!-- 上传第一个未上传的文件 -->
<a href="javascript:$('#uploadify').uploadify('upload')">上传</a>
<!-- 取消第一个未取消的文件 -->
<a href="javascript:$('#uploadify').uploadify('cancel')">取消上传</a>
<a href="javascript:$('#uploadify').uploadify('upload','*')">上传所有文件</a>
<a href="javascript:$('#uploadify').uploadify('cancel','*')">取消所有上传</a>
</p>
</body>
</html>
FileVo.java
这里封装了文件的一些信息。
package com.dao.chu.orm;
import java.io.Serializable;
public class FileVo implements Serializable
{
/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;
//保存的包路径
private String savePath;
//保存全路径
private String filePath;
//原文件名
private String oldFileName;
//新文件名
private String newFileName;
//文件大小
private String fileSize;
//扩展名
private String extName;
public String getSavePath()
{
return savePath;
}
public void setSavePath(String savePath)
{
this.savePath = savePath;
}
public String getFilePath()
{
return filePath;
}
public void setFilePath(String filePath)
{
this.filePath = filePath;
}
public String getOldFileName()
{
return oldFileName;
}
public void setOldFileName(String oldFileName)
{
this.oldFileName = oldFileName;
}
public String getNewFileName()
{
return newFileName;
}
public void setNewFileName(String newFileName)
{
this.newFileName = newFileName;
}
public String getFileSize()
{
return fileSize;
}
public void setFileSize(String fileSize)
{
this.fileSize = fileSize;
}
public String getExtName()
{
return extName;
}
public void setExtName(String extName)
{
this.extName = extName;
}
@Override
public String toString()
{
return "FileVo [savePath=" + savePath + ", filePath=" + filePath + ", oldFileName=" + oldFileName
+ ", newFileName=" + newFileName + ", fileSize=" + fileSize + ", extName=" + extName + "]";
}
public FileVo(String savePath, String filePath, String oldFileName, String newFileName, String fileSize,
String extName)
{
super();
this.savePath = savePath;
this.filePath = filePath;
this.oldFileName = oldFileName;
this.newFileName = newFileName;
this.fileSize = fileSize;
this.extName = extName;
}
}
Upload.java
package com.dao.chu.upload;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.dao.chu.orm.FileVo;
import com.dao.chu.util.MqProducer;
@SuppressWarnings("serial")
public class Upload extends HttpServlet
{
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String savePath = this.getServletConfig().getServletContext().getRealPath("");
savePath = savePath + "/uploads/";
File f1 = new File(savePath);
System.out.println(savePath);
if (!f1.exists())
{
f1.mkdirs();
}
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("utf-8");
List fileList = null;
try
{
fileList = upload.parseRequest(request);
}
catch (FileUploadException ex)
{
return;
}
Iterator<FileItem> it = fileList.iterator();
String oldFileName = "";
String newFileName = "";
String extName = "";
while (it.hasNext())
{
FileItem item = it.next();
if (!item.isFormField())
{
oldFileName = item.getName();
long size = item.getSize();
String type = item.getContentType();
System.out.println(size + " " + type);
if (oldFileName == null || oldFileName.trim().equals(""))
{
continue;
}
// 扩展名格式:
if (oldFileName.lastIndexOf(".") >= 0)
{
extName = oldFileName.substring(oldFileName.lastIndexOf("."));
}
File file = null;
do
{
// 生成文件名:
newFileName = UUID.randomUUID().toString();
file = new File(savePath + newFileName + extName);
} while (file.exists());
File saveFile = new File(savePath + newFileName + extName);
try
{
item.write(saveFile);
Map<String, String> map = setFileVo(savePath, savePath, oldFileName, newFileName, String.valueOf(size), extName);
//MQ发送消息
MqProducer.sendMessage(map);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
response.getWriter().print(newFileName + extName);
}
@SuppressWarnings("unused")
private Map<String, String> setFileVo(String savePath, String filePath, String oldFileName, String newFileName, String fileSize,
String extName)
{
FileVo fileVo = new FileVo(savePath, filePath, oldFileName, newFileName, fileSize, extName);
Map<String, String> map = mapPutFileVo(fileVo);
return map;
}
private Map<String, String> mapPutFileVo(FileVo fileVo){
Map<String, String> map = new HashMap<String,String>();
map.put("savePath", fileVo.getSavePath());
map.put("filePath", fileVo.getFilePath());
map.put("newFileName", fileVo.getNewFileName());
map.put("oldFileName", fileVo.getOldFileName());
map.put("extName", fileVo.getExtName());
map.put("fileSize", fileVo.getFileSize());
return map;
}
}
MqProducer.java
注意此处将每次发送的消息设为10条,也就是上传一个文件发送十条消息。一般只设为1即可。
package com.dao.chu.util;
import java.util.Map;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
/**
*
* MQ生产者,发送消息
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class MqProducer
{
// 默认连接用户名
private static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
// 默认连接密码
private static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
// 默认连接地址
private static final String BROKEURL = ActiveMQConnection.DEFAULT_BROKER_URL;
// 发送的消息数量
private static final int SENDNUM = 10;
/**
*
* 发送消息,外部调用方法
*
* @see [类、类#方法、类#成员]
*/
public static void sendMessage(Map<String, String> map)
{
// 连接工厂
ConnectionFactory connectionFactory;
// 连接
Connection connection = null;
// 会话 接受或者发送消息的线程
Session session;
// 消息的目的地
Destination destination;
// 消息生产者
MessageProducer messageProducer;
// 实例化连接工厂
connectionFactory =
new ActiveMQConnectionFactory(MqProducer.USERNAME, MqProducer.PASSWORD, MqProducer.BROKEURL);
try
{
// 通过连接工厂获取连接
connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 创建session
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
// 创建一个名称为NewFileList的消息队列
destination = session.createQueue("NewFileList");
// 创建消息生产者
messageProducer = session.createProducer(destination);
// 发送消息
send(session, messageProducer,map);
session.commit();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch (JMSException e)
{
e.printStackTrace();
}
}
}
}
/**
* 发送消息,内部方法
*
* @param session
* @param messageProducer 消息生产者
* @throws Exception
*/
public static void send(Session session, MessageProducer messageProducer,Map<String, String> map)
throws Exception
{
for (int i = 0; i < MqProducer.SENDNUM; i++)
{
MapMessage message = session.createMapMessage();
message.setObject("map", map);
System.out.println("发送消息:Activemq 发送消息" + map.toString());
// 通过消息生产者发出消息
messageProducer.send(message);
}
}
}
运行效果
主页
选择文件并点击上传所有文件
控制台共打印出50条消息
打开本地的mq,看队列
可看到共有50条消息。
点击NewFileList并点击某个消息
可看到具体发送的消息,里面我们封装了文件名称,文件大小,文件路径等。这样我们就可以实时监控文件上传情况。