用java在客户端读取mongodb中的数据并发送至服务器
使用Java自带的socket端口来实现,程序如下:
Client.java
package com.cn.gao;
import java.net.*;
import java.io.*; import com.mongodb.*;
/**
* 客户端发送消息给服务器
* @author hadoop
*
*/
public class Client {
private Socket client;
private boolean connected;
//客户端构造函数
public Client(String host,int port){
try {
client = new Socket(host,port);
System.out.println("连接服务器成功!");
this.connected = true;
} catch (UnknownHostException e) {
System.out.println("无法解析主机名!");
this.connected = false;
} catch (IOException e) {
System.out.println("输入输出错误!");
this.connected = false;
closeSocket();
}
}
//判断是否连接
public boolean isConnected(){
return connected;
}
//设置连接状态
public void setConnected(boolean connected){
this.connected = connected;
}
/**
* 发送数据到端口
* @param dbname mongodb数据库名字
* @param collectionName 该数据库中要发送数据的collection名
*/
public void sendFile(String dbname,String collectionName){
DataOutputStream dos = null;
DataInputStream dis = null;
if(client==null) return;
//从mongodb数据库中读取数据
Mongo connection = new Mongo("localhost:27017");
DB db = connection.getDB(dbname);
DBCollection input = db.getCollection(collectionName);
/* BasicDBObject condition=new BasicDBObject();//条件
BasicDBObject key=new BasicDBObject("vtext",2);//指定需要显示列
DBCursor cur = input.find(condition,key);*/
DBCursor cur = input.find();
try {
while(cur.hasNext()){
DBObject document = cur.next();
dis = new DataInputStream(new ByteArrayInputStream(document.toString().getBytes()));
dos = new DataOutputStream(client.getOutputStream());
int bufferSize = 10240;
byte[] buf = new byte[bufferSize];
int num =0;
while((num=dis.read(buf))!=-1){
System.out.println(new String(buf));
dos.write(buf, 0, num);
}
dos.flush();
}
System.out.println("传输成功!");
} catch (IOException e) {
e.printStackTrace();
// System.out.println("输入输出错误!");
closeSocket();
} finally{
try {
if(dis!=null) dis.close();
if(dos!=null) dos.close();
} catch (IOException e) {
e.printStackTrace();
// System.out.println("输入输出错误!");
}
} } //关闭客户端
public void closeSocket(){
if(client!=null){
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* @param args
* 参数格式如下
* hostname dbname collectionName
*/
public static void main(String[] args){
//默认端口为8888
if(args.length!=3){
System.out.println("参数格式不对!");
return;
}
String hostName = args[0];
int port = 8888;
Client client = null;
client = new Client(hostName,port);
String dbname = args[1];
String collectionName = args[2];
if(client.isConnected()){
client.sendFile(dbname, collectionName);
client.closeSocket();
} }
}
Server.java
package com.cn.gao;
import java.io.*;
import java.net.*; /**
* 服务器端
* @author hadoop
*
*/
public class Server {
private int port;
private String host;
private static ServerSocket server; public Server(int port){
this.port = port;
this.server = null;
} public void run(){
if(server==null){
try {
server = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("服务已启动...");
while(true){
try {
Socket client = server.accept();
if(client==null) continue;
new SocketConnection(client).run();
} catch (IOException e) {
e.printStackTrace();
}
}
} public class SocketConnection extends Thread{
private Socket client;
public SocketConnection(Socket client){
this.client = client;
} public void run(){
if(client==null) return;
DataInputStream in= null;
boolean flag = true;
try {
while(flag){
in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
int bufferSize = 10240;
byte[] buf = new byte[bufferSize];
int num =0;
while((num=in.read(buf))!=-1){
System.out.println(new String(buf));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
if(in!=null) in.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args){
int port = 8888;
new Server(port).run();
}
}
用java在客户端读取mongodb中的数据并发送至服务器的更多相关文章
- java读取请求中body数据
java读取请求中body数据 /** * 获取request中body数据 * * @author lifq * * 2017年2月24日 下午2:29:06 * @throws IOExcepti ...
- java.util.Properties 读取配置文件中的参数
用法 getProperty方法的返回值是String类型. java.util.Properties 读取配置文件中的参数 //读取配置文件 FileInputStream inStream = n ...
- 编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时会产生Additional information: 阅读器关闭时尝试调用 Read 无效问题,解决方法与解释
在自学杨中科老师的视频教学时,拓展编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时 会产生Additional information: 阅读器关闭时尝试调用 ...
- 读取redis中的数据时出现:MISCONF Redis is configured to save RDB snapshots
读取redis中的数据时出现:MISCONF Redis is configured to save RDB snapshots 以下为异常详细信息: Exception in thread &q ...
- 使用Hive读取ElasticSearch中的数据
本文将介绍如何通过Hive来读取ElasticSearch中的数据,然后我们可以像操作其他正常Hive表一样,使用Hive来直接操作ElasticSearch中的数据,将极大的方便开发人员.本文使用的 ...
- MongoDB中导入数据命令的使用(mongoimport)
MongoDB中导入数据命令的使用(mongoimport) 制作人:全心全意 语法: mongoimport <options> <file> 介绍: 该命令可以将CSV,T ...
- Spark读取Hbase中的数据
大家可能都知道很熟悉Spark的两种常见的数据读取方式(存放到RDD中):(1).调用parallelize函数直接从集合中获取数据,并存入RDD中:Java版本如下: JavaRDD<Inte ...
- Python中如何读取xls中的数据
要想读取EXCEL中的数据,首先得下载xlrd包,地址:https://pypi.python.org/pypi/xlrd 安装方法:下载解压后,利用windows dos命令进入解压目录eg,c ...
- sql 读取excel中的数据
select 列名 as 字段名 from openBowSet('MSDASQL.1','driver=Microsoft Excel Driver(*.xls);dbq=文件存放地址','sele ...
随机推荐
- Session机制二(简易购物车案例)
一:案例一(简易购物车) 1.目录结构 2.step1.jsp <%@ page language="java" contentType="text/html; c ...
- git获取帮助
想了解 Git 的各式工具该怎么用,可以阅读它们的使用帮助,方法有三: $ git help <verb> $ git <verb> --help $ man git-< ...
- "characterEncoding" must end with the ';' delimiter.
17/04/20 17:27:10 FATAL conf.Configuration: error parsing conf file:/usr/local/apache-hive-1.2.2-bin ...
- WebLogic和Tomcat的区别
J2ee开发主要是浏览器和服务器进行交互的一种结构.逻辑都是在后台进行处理,然后再把结果传输回给浏览器.可以看出服务器在这种架构是非常重要的. 这几天接触到两种Java的web服务器,做项目用的Tom ...
- SQL注入使用Django中继数据包bypassWAF
原理 本人基于文章bypassword的文章在HTTP协议层面绕过WAF所编写一款工具. 环境 Python3.7.0 Django 2.1 Requests 使用范围 POST注入 可以分块传输的漏 ...
- 在qemu上运行BusyBox
BusyBox 前文“在qemu环境中用gdb调试Linux内核”和“Initramfs 原理和实践”分别描述了怎么用qemu来运行一个编译好的内核,以及怎么指定initramfs,但都是简单的演示. ...
- JavaScript DOM大纲
DOM 定义了访问和操作HTML文档的标准方法 访问(查找标签) ---- 直接查找 document.getElementById(“idname”) document.getElementsByT ...
- 社会主义核心价值观js代码
效果如下: 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- Mistakes(Updating)
1.当调试时发现无法正常调用函数时,检查是否发生爆栈 对于每个栈仅有4MB的空间,开int只能开大约5*10^5. 大数组一定要开全局变量 2.当long long=int*int时会爆int,一定要 ...
- 【线性基】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem A. XOR
题意:给你一些数,问你是否能够将它们划分成两个集合,使得这两个集合的异或和之差的绝对值最小. 设所有数的异或和为S,集合A的异或和为A. 首先,S的0的位对答案不造成影响. S的最高位1,所对应的A的 ...