使用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. 32:从1到n整数中1出现的次数

    import java.util.Arrays; /** * 面试题32:从1到n整数中1出现的次数 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数? * 为此他特别数了 ...

  2. Dijkstra算法---HDU 2544 水题(模板)

    /* 对于只会弗洛伊德的我,迪杰斯特拉有点不是很理解,后来发现这主要用于单源最短路,稍稍明白了点,不过还是很菜,这里只是用了邻接矩阵 套模板,对于邻接表暂时还,,,没做题,后续再更新.现将这题贴上,应 ...

  3. jupyter notebook变量高亮

    首先声明,anaconda安装的时候,一定要勾选“Add Anaconda to my PATH environment variable”! 否则会有一堆麻烦的问题,做了这一步就能自动添加好路径!不 ...

  4. iview-cli 项目、iView admin 跨域问题解决方案

    在build 目录的 webpack.dev.config.js 目录中 module.exports = merge(webpackBaseConfig, { devtool: '#source-m ...

  5. 将已有的项目提交到GitHub

    1.目的: 将已有的项目提交到GitHub 2.准备工作 2.1 此教程建立在对git有初步的理解上 2.2 此教程之前需准备工作 a.熟悉git的一些基本命令和原理. b.已注册有GitHub账号. ...

  6. Android 中的广播(Broadcast)

    Android 广播(broadcast) 饮水思源 本文章内容学习和总结自 郭霖大神:<Android第一行代码> Overview 就像我们的学校里的喇叭一样,是用来通知的.而Andr ...

  7. ARM 中必须明白的几个概念

    文章具体介绍了关于ARM的22个常用概念. 1.ARM中一些常见英文缩写解释 MSB:最高有效位: LSB:最低有效位: AHB:先进的高性能总线: VPB:连接片内外设功能的VLSI外设总线: EM ...

  8. scrapy运行机制

    Scrapy主要包括了以下组件: 引擎(Scrapy)用来处理整个系统的数据流, 触发事务(框架核心) 调度器(Scheduler)用来接受引擎发过来的请求, 压入队列中, 并在引擎再次请求的时候返回 ...

  9. Needed Learning(Updating)

    决定把掌握不熟练或是模型见的少的知识点在这里列一列 希望能在自己AFO前成功get技能点吧…… 优先级:动态规划-分治-字符串-图论-数据结构-数学-计算几何-其它 动态规划 1.四边形不等式优化 2 ...

  10. (Nginx) URL REWRITE

    URL重写的基础介绍 把URI地址用作参数传递:URL REWRITE 最简单的是基于各种WEB服务器中的URL重写转向(Rewrite)模块的URL转换: 这样几乎可以不修改程序的实现将 news. ...