本次试验主要用到了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;}@Overridepublic 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();// 创建sessionsession = 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并点击某个消息

可看到具体发送的消息,里面我们封装了文件名称,文件大小,文件路径等。这样我们就可以实时监控文件上传情况。

源码下载

转载于:https://www.cnblogs.com/tongrui/p/6376251.html

activeMQ在文件上传的应用相关推荐

  1. Pocsuite编写ActiveMQ任意文件上传漏洞脚本

    ActiveMQ任意文件上传漏洞 该漏洞出现在fileserver应用中,ActiveMQ中的fileserver服务允许用户通过HTTP PUT方法上传文件到指定目录.Fileserver支持写入文 ...

  2. springboot设置文件上传大小(tomcat默认1M)

    application.yml # 设置文件上传大小(tomcat默认1M) server:tomcat:max-http-form-post-size: -1 spring:servlet:mult ...

  3. 将文件上传至ftp服务器,FTP文件上传工具类,将文件上传至服务器指定目录

    将文件上传至ftp服务器,传入File对象,将文件上传至ftp服务器 需要配置修改的点: 1. 服务器ip端口(服务器ip 端口22/21). 2. 服务器账号密码(服务器登录用户名密码). 3. 上 ...

  4. Springboot 多文件上传

    其实多个文件和单个文件上传是一样的,可以使用同一个Controller 添加依赖 <!-- https://mvnrepository.com/artifact/commons-fileuplo ...

  5. django文件上传

    Django在处理文件上传时,文件数据被打包封装在request.FILES中. 一.简单上传 首先,写一个form模型,它必须包含一个FileField: # forms.py from djang ...

  6. spring boot 文件上传工具类(bug 已修改)

    以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...

  7. 模拟文件上传(一):手动文件上传

    关于上传文件,首先我的第一个案例是一个文本文件的上传,简单容易上手! 首先我们上传文件肯定就属于实体内容部分了:所以不能过GET方式请求了,要通过POST方式请求: 因为: 1.get方式是URL传值 ...

  8. Java中的文件上传2(Commons FileUpload:commons-fileupload.jar)

    相比上一篇使用Servlet原始去实现的文件上传(http://www.cnblogs.com/EasonJim/p/6554669.html),使用组件去实现相对来说功能更多,省去了很多需要配置和处 ...

  9. 转 [JAVA] 使用 common-fileupload 实现文件上传

    就在前段时间,还在苦于找到不到合适的上传组件,虽然很早就知道了 common-fileupload,但当时却因为没有找到如何获取表单参数的方法而使用 jspSmartUpload,历尽艰辛终于找到了它 ...

最新文章

  1. Nagios的安装和基本配置(四:调试验证 错误总结)
  2. 目前有没有好用的RNASeq的pipeline呢?
  3. 美化浏览器的radio和checkbox样式
  4. 恭喜我的同事黄玉奇入选开放原子开源基金会TOC
  5. Blackcat主题-Wordpress
  6. struts 通配符的使用
  7. 华为c8818刷原生Android 6,华为C8818(电信4G)一键救砖教程,轻松刷回官方系统
  8. 全国大学生计算机技能应用大赛Java模拟题
  9. Myeclipse笔记
  10. springmvc 响应封装_SpringMVC 封装返回结果对象
  11. 动态规划:最长上升子序列(二分算法 nlogn)
  12. 免费分享一套狂雨小说cms采集规则
  13. DM368开发 -- 毕设之硬件
  14. Win8快捷键的使用
  15. C语言游戏开发——打飞机游戏1.0
  16. java收割者模式,王牌战士收割者怎么玩 海拉技巧玩法介绍
  17. 被认为是世界史上50个最伟大的发明有哪些?
  18. SpringSecurity框架用法简介
  19. 秒速五厘米(为情怀而补的题)
  20. SQL注入:sqli-labs lesson-8 lesson -9 基于布尔值和基于时间的盲注!

热门文章

  1. java文件解压文件_java 文件解压缩
  2. java调用浏览器组件来_Java 网页浏览器组件介绍
  3. 计算机科学计算方面分为,计算机方面的专业分为哪些类?【资讯与计算科学】和【电脑科学与技术专业】有什么不同?...
  4. 华北科技学院计算机期末考试,华北科技学院 专业计算机 考试专用
  5. ios 百度地图指定区域_获取百度地图可视区域范围的数据
  6. C#| 使用String.Format()方法将小数点前的数字四舍五入
  7. C++函数名的修饰规则
  8. 461. 汉明距离 golang
  9. golang实现聊天室(二)
  10. UVa-12333:Revenge of Fibonacci 高精度