Java新AIO/NIO2:AsynchronousServerSocketChannel和AsynchronousSocketChannel简单服务器-客户端

Java新AIO/NIO2:AsynchronousServerSocketChannel和AsynchronousSocketChannel简单服务器-客户端

用AsynchronousServerSocketChannel和AsynchronousSocketChannel实现一个最简单的服务器-客户端程序。服务器用AsynchronousServerSocketChannel实现,客户端用AsynchronousSocketChannel实现。服务器绑定本地端口80,等待客户端连接。服务器与客户端建立连接后服务器发给客户端字符串“zhangphil”,然后服务器关闭Socket连接,客户端接收数据后也关闭Socket连接。
服务器端:

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.Future;

public class Server {
	public static void main(String[] args) {
		try {
			Server server = new Server();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Server() throws Exception {
		AsynchronousServerSocketChannel serverSocketChannel = AsynchronousServerSocketChannel.open();
		InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 80);
		serverSocketChannel.bind(inetSocketAddress);

		Future<AsynchronousSocketChannel> accept;

		while (true) {
			// accept()不会阻塞。
			accept = serverSocketChannel.accept();

			System.out.println("=================");
			System.out.println("服务器等待连接...");
			AsynchronousSocketChannel socketChannel = accept.get();// get()方法将阻塞。

			System.out.println("服务器接受连接");
			System.out.println("服务器与" + socketChannel.getRemoteAddress() + "建立连接");

			ByteBuffer buffer = ByteBuffer.wrap("zhangphil".getBytes());
			Future<Integer> write=socketChannel.write(buffer);
			
			while(!write.isDone()) {
				Thread.sleep(10);
			}
			
			System.out.println("服务器发送数据完毕.");
			socketChannel.close();
		}
	}
}

 

客户端:

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.Future;

public class Client {
	public static void main(String[] args) {
		AsynchronousSocketChannel socketChannel = null;
		try {
			socketChannel = AsynchronousSocketChannel.open();
			InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 80);
			Future<Void> connect = socketChannel.connect(inetSocketAddress);

			while (!connect.isDone()) {
				Thread.sleep(10);
			}

			System.out.println("建立连接" + socketChannel.getRemoteAddress());

			ByteBuffer buffer = ByteBuffer.allocate(1024);
			Future<Integer> read = socketChannel.read(buffer);

			while (!read.isDone()) {
				Thread.sleep(10);
			}

			System.out.println("接收服务器数据:" + new String(buffer.array(), 0, read.get()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

先运行服务器端程序,再运行客户端程序。然后服务器端输出:

 

客户端输出:

建立连接localhost/127.0.0.1:80
接收服务器数据:zhangphil

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于这个URL,你需要使用一些库来将其转换为PDF并将其保存到指定目录中。可以使用Apache PDFBox库来转换HTML或XHTML内容为PDF格式。 以下是一个使用PDFBox库将HTML内容转换为PDF格式并将其保存的例子: ```java import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory; import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.pdfbox.text.PDFTextStripperByArea; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class UrlToPdf { public static void main(String[] args) throws IOException { String urlStr = "http://127.0.0.1:9006/itsp-web-aio/pub/houseInfo/mortageArchivesForm_mould?orderId=" + orderId; String outputPath = "path/to/save/file.pdf"; // Parse HTML content from URL Document document = Jsoup.connect(urlStr).get(); String htmlContent = document.html(); // Create PDF document and add pages PDDocument pdfDocument = new PDDocument(); PDPage page = new PDPage(); pdfDocument.addPage(page); // Add content to the page PDPageContentStream contentStream = new PDPageContentStream(pdfDocument, page); contentStream.beginText(); contentStream.setFont(PDType1Font.TIMES_ROMAN, 12); contentStream.newLineAtOffset(100, 700); contentStream.showText(htmlContent); contentStream.endText(); contentStream.close(); // Save the PDF document pdfDocument.save(outputPath); pdfDocument.close(); } } ``` 在这个例子中,我们使用Jsoup库从URL中解析HTML内容,然后使用PDFBox库将HTML内容添加到PDF文件中并将其保存到指定路径中。你需要将 `orderId` 替换为你的订单ID,将 `path/to/save/file.pdf` 替换为你要保存PDF文件的路径和文件名。 需要注意的是,这个例子只是将HTML内容添加到PDF文件中,并没有将HTML内容转换为PDF格式,因此PDF文件中可能存在一些格式问题。如果你需要更好的PDF输出质量,你可能需要使用其他的库或工具来将HTML内容转换为PDF格式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值