使用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中的数据并发送至服务器的更多相关文章

  1. java读取请求中body数据

    java读取请求中body数据 /** * 获取request中body数据 * * @author lifq * * 2017年2月24日 下午2:29:06 * @throws IOExcepti ...

  2. java.util.Properties 读取配置文件中的参数

    用法 getProperty方法的返回值是String类型. java.util.Properties 读取配置文件中的参数 //读取配置文件 FileInputStream inStream = n ...

  3. 编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时会产生Additional information: 阅读器关闭时尝试调用 Read 无效问题,解决方法与解释

    在自学杨中科老师的视频教学时,拓展编写SqlHelper使用,在将ExecuteReader方法封装进而读取数据库中的数据时 会产生Additional information: 阅读器关闭时尝试调用 ...

  4. 读取redis中的数据时出现:MISCONF Redis is configured to save RDB snapshots

    读取redis中的数据时出现:MISCONF Redis is configured to save RDB snapshots   以下为异常详细信息: Exception in thread &q ...

  5. 使用Hive读取ElasticSearch中的数据

    本文将介绍如何通过Hive来读取ElasticSearch中的数据,然后我们可以像操作其他正常Hive表一样,使用Hive来直接操作ElasticSearch中的数据,将极大的方便开发人员.本文使用的 ...

  6. MongoDB中导入数据命令的使用(mongoimport)

    MongoDB中导入数据命令的使用(mongoimport) 制作人:全心全意 语法: mongoimport <options> <file> 介绍: 该命令可以将CSV,T ...

  7. Spark读取Hbase中的数据

    大家可能都知道很熟悉Spark的两种常见的数据读取方式(存放到RDD中):(1).调用parallelize函数直接从集合中获取数据,并存入RDD中:Java版本如下: JavaRDD<Inte ...

  8. Python中如何读取xls中的数据

    要想读取EXCEL中的数据,首先得下载xlrd包,地址:https://pypi.python.org/pypi/xlrd  安装方法:下载解压后,利用windows  dos命令进入解压目录eg,c ...

  9. sql 读取excel中的数据

    select 列名 as 字段名 from openBowSet('MSDASQL.1','driver=Microsoft Excel Driver(*.xls);dbq=文件存放地址','sele ...

随机推荐

  1. 集群运维ansible

    ssh免密登录 集群运维 生成秘钥,一路enter cd ~/.ssh/ ssh-keygen -t rsa 讲id_rsa.pub文件追加到授权的key文件中 cat ~/.ssh/id_rsa.p ...

  2. 文件系统层级结构标准(FHS)

    参考资料:FHS 简介 FHS目前发展到3.0版本,发布于2015年6月3日,由Linux基金会在负责维护.它规定了Linux的文件层级结构,使得各Linux发行版.软件开发商知道应该将哪些文件放在哪 ...

  3. Python 实现扫码二维码登录

    最近在做一个扫码登录功能,为此我还在网上搜了一下关于微信的扫描登录的实现方式.当这个功能完成了后,我决定将整个实现思路整理出来,方便自己以后查看也方便其他有类似需求的程序猿些. 要实现扫码登录我们需要 ...

  4. 基于ONVIF协议的摄像头开发总结

    <什么是ONVIF协议>     2008年5月,由安讯士(AXIS)联合博世(BOSCH)及索尼(SONY)公司三方宣布携手共同成立一个国际开放型网络视频产品标准网络接口开发论坛,取名为 ...

  5. JZYZOJ1140 飞船控制站

    http://172.20.6.3/Problem_Show.asp?id=1140 p1140 就一道非常普通的二分,但是非常蛋疼的是验证mid left的过程一直错(就是写一个k次循环然后根据可行 ...

  6. 【Floyd矩阵乘法】BZOJ1706- [usaco2007 Nov]relays 奶牛接力跑

    [题目大意] 给出一张无向图,求出恰巧经过n条边的最短路. [思路] 首先题目中只有100条边,却给出了10000个点(实际上最多只能有200个),离散化一下. 后面就是Floyd的新姿势,以前看过的 ...

  7. Go语言特点

    作者:asta谢链接:https://www.zhihu.com/question/21409296/answer/18184584来源:知乎 1.Go有什么优势 可直接编译成机器码,不依赖其他库,g ...

  8. Lost connection to MySQL server at 'reading initial communication packet', system error: 0的一个解决方案

    -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT在icmp之前/etc/init.d/iptables restar ...

  9. 解决请求参数的中文乱码问题(get、post)

    2018-11-28 在web请求与响应中,会遇到乱码问题,比如填写表单数据时,难免会输入中文,姓名.公司名称等.由于HTML设置了浏览器在传递请求参数时,采用的编码方式是UTF-8,但在解码时采用的 ...

  10. 【BZOJ-1396&2865】识别子串&字符串识别 后缀自动机/后缀树组 + 线段树

    1396: 识别子串 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 312  Solved: 193[Submit][Status][Discuss] ...