gpt4 book ai didi

java - 通过java中的套接字从服务器请求特定文件

转载 作者:行者123 更新时间:2023-11-30 09:49:26 25 4
gpt4 key购买 nike

我找到了一些在客户端和服务器之间传输文件的代码。但是文件位置和端口号是硬编码的。我想知道是否有一种方法可以让客户端指定他/她需要从服务器获得的文件——这样当服务器收到请求时,它可以将该特定文件发送给客户端。谢谢。


编辑[1]:代码片段和上下文描述:

我正在根据反馈和评论添加目前已有的代码。希望这能回答评论部分中的一些问题。

import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Original coder adapted from:
* http://www.rgagnon.com/javadetails/java-0542.html
*
* Best intentions:
* This program runs both as server and client.
*
* The client asks for a specific file from _
* the server x number of times in a loop.
*
* Server simply serves the file requested.
*/

public class FileServer extends Thread {

public static void server() throws IOException {
ServerSocket servsock = new ServerSocket(13267);
while (true) {
System.out.println("Waiting...");

Socket sock = servsock.accept();
System.out.println("Accepted connection : " + sock);

//Retrieve filename to serve
InputStream is = sock.getInputStream();
BufferedReader bfr = new BufferedReader(new InputStreamReader(is));
String fileName = bfr.readLine();
bfr.close();

System.out.println("Server side got the file name:" + fileName);

//Sendfile
File myFile = new File(fileName);
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray, 0, mybytearray.length);
os.flush();
sock.close();
}
}

public static void client(int index) throws IOException {
int filesize = 6022386; // filesize temporary hardcoded

long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
//Localhost for testing
Socket sock = new Socket("127.0.0.1", 13267);

System.out.println("Connecting...");

//Ask for specific file: source1
String fileName = "source1";
OutputStream os = sock.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.println(fileName);


//Receive file
byte[] mybytearray = new byte[filesize];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream("source1-copy" + index);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray, 0, mybytearray.length);
current = bytesRead;

// thanks to A. Cádiz for the bug fix
do {
bytesRead =
is.read(mybytearray,
current, (mybytearray.length - current));
if (bytesRead >= 0) {
current += bytesRead;
}
} while (bytesRead > -1);

bos.write(mybytearray, 0, current);
bos.flush();

long end = System.currentTimeMillis();
System.out.println(end - start);
os.flush();

bos.close();
sock.close();
}

public static void main(String[] args) throws IOException {

FileServer fs = new FileServer();
fs.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(
FileServer.class.getName()).log(Level.SEVERE, null, ex);
}

for (int i = 0; i < 5; i++) {
client(i);
}

}

@Override
public void run() {
try {
server();
} catch (IOException ex) {
Logger.getLogger(
FileServer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

当我运行这段代码时,它卡在了“Connecting ...”行。这是输出:

Waiting...
Accepted connection : Socket[addr=/127.0.0.1,port=44939,localport=13267]
Connecting...

最佳答案

@moejoe 我认为你想多了。

如果您已经准备好发送文件,那么要做的第一件事就是抽象出该功能,以便您可以将其作为方法运行并提供路径名/文件名。

然后您可以使用套接字(这是两种方式)从客户端向服务器发送一条消息,询问您想要什么文件。除此之外,这是如何从 UI 中获取所需文件的问题。您可能需要让服务器提供一种“列出可用文件”的方法,即 ls 功能。

这一切都非常容易实现。

关于java - 通过java中的套接字从服务器请求特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5793698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com