首页 > 代码库 > 不同服务器上的Java项目文件同步 解决方案(socket 、http)

不同服务器上的Java项目文件同步 解决方案(socket 、http)

最近两个项目之间的需要做数据同步,要求是A项目的数据以及图片文件要同步到B项目,首先两个项目都是独立的采集录入信息的项目,数据库数据同步不说了,还有一些图片等文件也得需要同步, 首先想到的是B项目调用A项目的图片路径,因为两个项目的图片文件路径有很多种生成情况,两项目都在运行中,这种图片路径方法就排除了。 后面就想到了两种解决方法:

1 socket 方法 就是在B项目添加侦听 B项目启动时候start, 在A项目中用Client 。

2 在A项目中通过HttpURLConnection模拟post表单提交到B项目对应方法。(个人感觉这种比较好)

HttpURLConnection:

package test.httpUp;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class HttpPostUtil {
	

	URL url;
	HttpURLConnection conn;
	String boundary = "--------http";
	Map<String, String> textParams = new HashMap<String, String>();
	Map<String, File> fileparams = new HashMap<String, File>();
	DataOutputStream ds;

	public HttpPostUtil(String url) throws Exception {
		this.url = new URL(url);
	}
    //重新设置要请求的服务器地址,即上传文件的地址。
	public void setUrl(String url) throws Exception {
		this.url = new URL(url);
	}
    //增加一个普通字符串数据到form表单数据中
	public void addTextParameter(String name, String value) {
		textParams.put(name, value);
	}
    //增加一个文件到form表单数据中
	public void addFileParameter(String name, File value) {
		fileparams.put(name, value);
	}
    // 清空所有已添加的form表单数据
	public void clearAllParameters() {
		textParams.clear();
		fileparams.clear();
	}
    // 发送数据到服务器,返回一个字节包含服务器的返回结果的数组
	public byte[] send() throws Exception {
		initConnection();
		try {
			conn.connect();
		} catch (SocketTimeoutException e) {
			// something
			throw new RuntimeException();
		}
		ds = new DataOutputStream(conn.getOutputStream());
		writeFileParams();
		writeStringParams();
		paramsEnd();
		InputStream in = conn.getInputStream();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int b;
		while ((b = in.read()) != -1) {
			out.write(b);
		}
		conn.disconnect();
		return out.toByteArray();
	}
    //文件上传的connection的一些必须设置
	private void initConnection() throws Exception {
		conn = (HttpURLConnection) this.url.openConnection();
		
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		conn.setConnectTimeout(10000); //连接超时为10秒
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type",
				"multipart/form-data; boundary=" + boundary);
	}
    //普通字符串数据
	private void writeStringParams() throws Exception {
		Set<String> keySet = textParams.keySet();
		for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
			String name = it.next();
			String value = http://www.mamicode.com/textParams.get(name);>=====================
socket   client

package test.socket;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URL;

/**
 * 文件发送客户端主程序
 * @author admin_Hzw
 *
 */
public class BxClient {
	
	/**
	 * 程序main方法
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		
		int length = 0;
		double sumL = 0 ;
		byte[] sendBytes = null;
		Socket socket = null;
		DataOutputStream dos = null;
		FileInputStream fis = null;
		boolean bool = false;
		try {
			File file = new File("d:/272.jpg"); //要传输的文件路径
			long l = file.length(); 
			socket = new Socket();  
			socket.connect(new InetSocketAddress("192.168.1.110", 8877));
			dos = new DataOutputStream(socket.getOutputStream());
			fis = new FileInputStream(file);      
			sendBytes = new byte[1024];  
			while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
				sumL += length;  
				System.out.println("已传输:"+((sumL/l)*100)+"%");
				dos.write(sendBytes, 0, length);
				dos.flush();
			} 
			//虽然数据类型不同,但JAVA会自动转换成相同数据类型后在做比较
			if(sumL==l){
				bool = true;
			}
		}catch (Exception e) {
			System.out.println("客户端文件传输异常");
			bool = false;
			e.printStackTrace();  
		} finally{  
			if (dos != null)
				dos.close();
			if (fis != null)
				fis.close();   
			if (socket != null)
				socket.close();    
		}
		System.out.println(bool?"成功":"失败");
	}
}
socket server

web.xml

 <listener>  
  <listener-class>XXX.web.listener.SocketServiceLoader</listener-class>  
</listener>  


import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class SocketServiceLoader implements ServletContextListener { //socket server 线程  
    private SocketThread socketThread;  
      
    @Override  
    public void contextDestroyed(ServletContextEvent arg0) {  
        if(null!=socketThread && !socketThread.isInterrupted())  
        {  
         socketThread.closeSocketServer();  
         socketThread.interrupt();  
        }  
 }  
  
    @Override  
    public void contextInitialized(ServletContextEvent arg0) {  
        // TODO Auto-generated method stub  
        if(null==socketThread)  
        {  
         //新建线程类  
         socketThread=new SocketThread(null);  
         //启动线程  
         socketThread.start();  
        }  
    }  }


import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketThread extends Thread{  
    private ServerSocket serverSocket = null;  
      
    public SocketThread(ServerSocket serverScoket){  
        try {  
            if(null == serverSocket){  
                this.serverSocket = new ServerSocket(8877);  
                System.out.println("socket start");  
            }  
        } catch (Exception e) {  
            System.out.println("SocketThread创建socket服务出错");  
            e.printStackTrace();  
        }  
  
    }  
      
    public void run(){  
        while(!this.isInterrupted()){  
            try {  
                Socket socket = serverSocket.accept();  
                  
                if(null != socket && !socket.isClosed()){     
                    //处理接受的数据  
                    new SocketOperate(socket).start();  
                }  
                socket.setSoTimeout(30000);  
                  
            }catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
    }  
      
      
    public void closeSocketServer(){  
       try {  
            if(null!=serverSocket && !serverSocket.isClosed())  
            {  
             serverSocket.close();  
            }  
       } catch (IOException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
       }  
     }  
      
      
}

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Random;
public class SocketOperate extends Thread{  
    private Socket socket;  
      
    public SocketOperate(Socket socket) {  
       this.socket=socket;  
    }  
    @SuppressWarnings("unused")  
   public void run()  
    {  
        try{   
			System.out.println("开始监听...");
			/*
			 * 如果没有访问它会自动等待
			 */
			System.out.println("有链接");
			receiveFile(socket);
		} catch (Exception e) {
			System.out.println("服务器异常");
			e.printStackTrace();
		}   
    } 
    
    
    /*public void run() {
	}*/

	/**
	 * 接收文件方法
	 * @param socket
	 * @throws IOException
	 */
	public static void receiveFile(Socket socket) throws IOException {
		byte[] inputByte = null;
		int length = 0;
		DataInputStream dis = null;
		FileOutputStream fos = null;
		String filePath = "D:/temp/"+new Random().nextInt(10000)+".jpg";
		try {
			try {
				dis = new DataInputStream(socket.getInputStream());
				File f = new File("D:/temp");
				if(!f.exists()){
					f.mkdir();  
				}
				/*  
				 * 文件存储位置  
				 */
				fos = new FileOutputStream(new File(filePath));    
				inputByte = new byte[1024];   
				System.out.println("开始接收数据...");  
				while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
					fos.write(inputByte, 0, length);
					fos.flush();    
				}
				System.out.println("完成接收:"+filePath);
			} finally {
				if (fos != null)
					fos.close();
				if (dis != null)
					dis.close();
				if (socket != null)
					socket.close(); 
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}  



不同服务器上的Java项目文件同步 解决方案(socket 、http)