我重写了一个简单的服务器和客户端之间文件传输代码的示例。
而且它有效。
但我想让它能够传输特定目录中的多个文件。用户将写入文件名(位于该特定目录中),客户端将从服务器下载它们。我怎样才能做到这一点?有任何想法吗?谢谢。
客户端代码:
import java.net.*;
import java.io.*;
public class Client {
static String hostname = "127.0.0.1";
static int port = 4588;
static int processedByte;
static byte[] theByte = new byte[1];
static Socket client = null;
static InputStream inuputSt = null;
public static void main(String[] args) throws InterruptedException {
System.out.println("connecting...");
Thread.sleep(500);
try {
client = new Socket(hostname, port);
inuputSt = client.getInputStream();
} catch (IOException ex) {
System.out.println("connection error.");
}
ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream();
if (inuputSt != null) {
FileOutputStream fileOutput = null;
BufferedOutputStream bufferedOutput = null;
try {
System.out.println("downloading target file...");
Thread.sleep(800);
fileOutput = new FileOutputStream("file1_downloaded.txt");
bufferedOutput = new BufferedOutputStream(fileOutput);
processedByte = inuputSt.read(theByte, 0, theByte.length);
do {
arrayOutput.write(theByte);
processedByte = inuputSt.read(theByte);
} while (processedByte != -1);
bufferedOutput.write(arrayOutput.toByteArray());
bufferedOutput.flush();
bufferedOutput.close();
System.out.println("file downloaded");
client.close();
} catch (IOException ex) {
System.out.println("file transfer error.");
}
}
}
}
服务器代码:
import java.net.*;
import java.io.*;
public class Server {
static int port = 4588;
public static void main(String[] args) {
while (true) {
ServerSocket server = null;
Socket connection = null;
BufferedOutputStream bufferedOutput = null;
try {
server = new ServerSocket(port);
connection = server.accept();
bufferedOutput = new BufferedOutputStream(connection.getOutputStream());
} catch (IOException ex) {
// Do exception handling
}
if (bufferedOutput != null) {
File fileToSend = new File("files\\file1.txt");
byte[] mybytearray = new byte[(int) fileToSend.length()];
FileInputStream fileInputSt = null;
try {
fileInputSt = new FileInputStream(fileToSend);
} catch (FileNotFoundException ex) {
// exception stuff
}
BufferedInputStream bufferedInput = new BufferedInputStream(fileInputSt);
try {
bufferedInput.read(mybytearray, 0, mybytearray.length);
bufferedOutput.write(mybytearray, 0, mybytearray.length);
bufferedOutput.flush();
bufferedOutput.close();
connection.close();
//file1.txt has been downloaded
return;
} catch (IOException ex) {
// exception stuff
}
}
}
}
}
最佳答案
您建议将 HTTP 作为客户端和服务器的协议(protocol) - HTTP 是一个很好的协议(protocol),但如果您想自己完成整个事情,可能会遇到很大的实现障碍。 HTTP PUT
verb可用于上传文件,以这种方式使用 HTTP 的好处是您的客户端和服务器可以与设计用于使用 PUT
请求的其他工具进行通信。 (PUT
比其他 HTTP 动词使用较少,因此并非所有 HTTP 工具都可以工作;curl(1)
程序确实支持PUT
通过 -T
命令行选项。如果您选择 HTTP,这将是一个很好的实现帮助。)
有多种REST Frameworks可以帮助你编写HTTP软件;我听说过关于Restlet的好消息,这将是我推荐的起点。
但是您不必选择 HTTP 作为协议(protocol)。我认为,如果您实现自己的协议(protocol),您可以学到很多有关网络编程的知识 - 它将教您很多有关 API 设计和套接字编程的知识,而通过使用预编程很难学习到这些知识。编写的 HTTP 协议(protocol)工具(如果您尝试自己完整地实现 HTTP,那么令人沮丧)。
考虑这个对话:
客户端 -> 服务器:上传名为“flubber”的文件,大小为 200000 字节
服务器 -> 客户端:好的
客户端 -> 服务器:错误的内容
服务器 -> 客户端:好的
客户端 -> 服务器:上传名为“blort”的文件,大小为 10 字节
服务器 -> 客户端:错误,文件存在
...
您可能需要添加新命令以提供对两端文件进行哈希处理以确保文件传输成功、用于发送特定字节范围的命令(附加到现有文件或重新启动失败的传输)、命令列出服务器上现有的文件名、覆盖现有文件的命令、从服务器删除文件的命令等等。编写自己的协议(protocol)的最好部分是您可以决定您的程序将支持什么。缺点是您需要测试您编写的功能,并且测试某些情况可能很困难。 (比如说,考虑到客户端可能会在不同的 TCP 数据包中发送命令的每个字符。实现缓冲来存储整个命令并不是微不足道,但它已经为您完成了工具,例如 ReSTLet。)
Juan's advice to use multiple TCP sessions并不是绝对必要的——尽管这可能是您最简单的前进道路。您需要添加一些机制来向远程对等点提供文件名,这可能最好通过“控制” channel (运行的第一个 session - 类似于 FTP)来完成,否则可能会是您在文件内容之前立即发送的内容(类似于 HTTP)。
不过,我建议避免多个连接——每个连接需要三倍于系统之间的往返时间来设置和开始传输字节。这种延迟可能会极其令人烦恼,尤其是当您尝试传输数百个小文件时。您甚至可以花费比实际发送数据更多的时间来设置连接。
但它是你的工具——你可以按照你的意愿设计它。玩得开心。
关于java - 如何在客户端和服务器之间传输多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10628295/