⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlhttpclient.java

📁 简单的java http 服务器和客户端源代码
💻 JAVA
字号:
package testHttp.httpclient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import org.apache.commons.lang.StringUtils;

/**
 * 
 * <p>
 * Title: HttpClient.java
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright:OnewaveInc Copyright (c) 2007
 * </p>
 * <p>
 * Company: OnewaveInc
 * </p>
 * 
 * @author Zhengrw
 * @version 3.0
 */
public class XMLHttpClient {

	private final static String CRLF = "\r\n";

	private static Socket socket;

	private static BufferedReader in;

	private static PrintWriter out;

	public static void send(String xmlFileName, String host, int port) {
		send(new File(xmlFileName), host, port);
	}

	public static void send(File xmlFile, String host, int port) {
		String content = getContentFromXml(xmlFile);
		try {
			socket = new Socket(host, port);
			
			in = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));
			out = new PrintWriter(socket.getOutputStream(), true);
			out.println(content);
			String line = null;
			while(!StringUtils.isEmpty((line = in.readLine()))){
				System.out.println(line);
				System.out.println(CRLF);
//				line = in.readLine();
			}
			
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				out.close();
				in.close();
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private static String getContentFromXml(File xmlFile) {
		StringBuffer sb = new StringBuffer();
		try {
			InputStream stream = new FileInputStream(xmlFile);
			BufferedReader line = new BufferedReader(new InputStreamReader(
					stream));
			String str = null;
			while (!StringUtils.isEmpty((str = line.readLine()))) {
				sb.append(str);
				sb.append(CRLF);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		XMLHttpClient.send("F:\\test.xml", "127.0.0.1", 8180);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -