`
- 浏览:
671100 次
-
客户端
=============================================
import java.io.*;
import java.net.*;
public class RemoteFileClient
{
PRotected String hostip;
protected int hostPort;
protected BufferedReader socketReader;
protected PrintWriter socketWriter;
public static void main(String[] args)
{
RemoteFileClient remoteFileClient = new RemoteFileClient("127.0.0.1", 3000);
remoteFileClient.setUpConnection();
String fileContents =remoteFileClient.getFile("E:\RemoteFile.txt");
remoteFileClient.tearDownConnection();
System.out.println(fileContents);
}
//==========================
public RemoteFileClient(String aHostIp, int aHostPort)
{
hostIp = aHostIp;
hostPort = aHostPort;
}
//===========================
public void setUpConnection()
{
try
{
Socket client = new Socket(hostIp, hostPort);
socketReader = new BufferedReader(
new InputStreamReader(client.getInputStream()));
socketWriter = new PrintWriter(client.getOutputStream());
} catch (UnknownHostException e) {
System.out.println("Error setting up socket connection: unknown host at " + hostIp + ":" + hostPort);
} catch (IOException e) {
System.out.println("Error setting up socket connection: " + e);
}
}
//=============================
public String getFile(String fileNameToGet)
{
StringBuffer fileLines = new StringBuffer();
try
{
socketWriter.println(fileNameToGet);
socketWriter.flush();
String line = null;
while ((line = socketReader.readLine()) != null)
fileLines.append(line + "");
}
catch (IOException e)
{
System.out.println("Error reading from file: " + fileNameToGet);
}
return fileLines.toString();
}
//========================
public void tearDownConnection()
{
try
{
socketWriter.close();
socketReader.close();
}
catch (IOException e)
{
System.out.println("Error tearing down socket connection: " + e);
}
}
//=========================
}
=================================================================
服务器端
=========
import java.io.*;
import java.net.*;
public class RemoteFileServer {
protected int listenPort = 3000;
public static void main(String[] args)
{
RemoteFileServer server = new RemoteFileServer();
server.acceptConnections();
}
public void acceptConnections() {
try {
ServerSocket server = new ServerSocket(listenPort);
Socket incomingConnection = null;
while (true) {
incomingConnection = server.accept();
handleConnection(incomingConnection);
}
} catch (BindException e) {
System.out.println("Unable to bind to port " + listenPort);
} catch (IOException e) {
System.out.println("Unable to instantiate a ServerSocket on port: " + listenPort);
}
}
public void handleConnection(Socket incomingConnection)
{
try
{
OutputStream outputToSocket = incomingConnection.getOutputStream();
InputStream inputFromSocket = incomingConnection.getInputStream();
BufferedReader streamReader =
new BufferedReader(new InputStreamReader(inputFromSocket));
FileReader fileReader = new FileReader(new File(streamReader.readLine()));
BufferedReader bufferedFileReader = new BufferedReader(fileReader);
PrintWriter streamWriter =
new PrintWriter(incomingConnection.getOutputStream());
String line = null;
while ((line = bufferedFileReader.readLine()) != null)
{
streamWriter.println(line);
}
fileReader.close();
streamWriter.close();
streamReader.close();
}
catch (Exception e)
{
System.out.println("Error handling a client: " + e);
}
}
}
===========================================
输出结果:
---------------看!我读取了E:\RemoteFile.txt啊!
================================================
Wrapping up the server
Before we move on to another, more practical example, let´s review the steps to create and
use a ServerSocket:
1. Instantiate a ServerSocket with a port on which you want it to listen for incoming
client connections (throws an Exception if there´s a problem).
2. Call accept() on the ServerSocket to block while waiting for connection.
3. Get the streams on that underlying Socket for reading and writing.
4. Wrap the streams as necessary to simplify your life.
5. Read from and write to the Socket.
6. Close your open streams (and remember, never close your Reader before your Writer).
You can find the complete code listing for RemoteFileServer at Code listing for
RemoteFileServer on page 33.
===================================
Wrapping up the server
Before we move on to another, more practical example, let´s review the steps to create and
use a ServerSocket:
1. Instantiate a ServerSocket with a port on which you want it to listen for incoming
client connections (throws an Exception if there´s a problem).
2. Call accept() on the ServerSocket to block while waiting for connection.
3. Get the streams on that underlying Socket for reading and writing.
4. Wrap the streams as necessary to simplify your life.
5. Read from and write to the Socket.
6. Close your open streams (and remember, never close your Reader before your Writer).
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
JAVA实现远程文件读取 JAVA是一种广泛应用于软件开发的编程语言,随着网络技术的发展,JAVA也提供了许多支持远程文件读取的功能,本文将详细介绍JAVA实现远程文件读取的知识点。 1. Socket 编程 在JAVA中,Socket...
本文件是将 Java 中 通过远程url访问 转换成 HTML 文件 ,通过 dom4j转换成Java对象元素
java 读取远程共享文件,一个操作简单的读取远程资源的控件。
JAVA读取远程网页文件并保存本地 从远程URL地址获取网页文件下载到本地 这个方法可以生成静态HTML文件使用!
主要实现的是登录服务器操作服务器的中的文件数据,支持读写的操作。主要使用的方法getProperties是设置配置的login(参数一是访问服务器的配置,参数二是设置读还是写)方法是读写连接服务器
用JAVA实现缓冲多线程无阻塞读取远程文件
java 读取远程网站上的 数据,如 :图片,附件,视频等
用Java多线程实现无阻塞读取远程文件!
Java多线程读大文件 java多线程写文件:多线程往队列中写入数据
用JAVA实现缓冲多线程无阻塞读取远程文件.pdf
NULL 博文链接:https://listen-raining.iteye.com/blog/1944136
3. 文件读取(File Reading) 4、优点: 封装性:工具类封装了NFS相关的复杂连接、认证和I/O操作细节,使得业务层代码无需关心底层通信协议,只需通过简单API即可操作远程文件。 可配置性:允许灵活配置NFS服务器...
java读取远程网页乱码解决方案 java读取远程网页乱码解决方案
本程序可用于在线编辑器提交内容时自动从网页中获得src图片地址,然后把远程图片下载到本地服务器,并把原网页中的地址替换成本地的地址。直接放在根目录上运行就行了。
用JAVA实现缓冲多线程无阻塞读取远程文件.doc
这是一个详细介绍Java怎么样写XML文件和远程读取XML文件的文档,里面有详细的例子。
java读取文件大全 写入字节流 读取字节流 在实际运用中相当的广泛 大家共享下资料
java调用cmd命令实现访问windows共享目录(需不需要密码均可),然后根据指定文件路径下载文件到我们自己配置的本地目录,main函数为测试代码
使用Java读取Excel文件的内容,主要是使用POI框架,可以读取最新的Excel文件xlsx后缀文件,即2007版本之后的。
基于JAVA CS远程监控系统软件的实现(源代码+WORD论文文档论文) 基于JAVA C/S远程监控系统软件的实现 摘 要 近年来,网络技术的不断发展,为远程监控技术的发展创造了条件。远程监控系统软件越来越受到人们的重视,...