java 禁止接受get,无法让我的Java服务器接受来自客户端的文件传输

I have two files: a chat server and a chat client. The chat client is supposed to say that it wants to upload a file to the server. And then it uploads. However, right now, all of the messages are being sent / received properly, but when I try to get the file transfer, the only thing I get is a file with 0 bytes (which is at the path I specify inside of the server class.

Broken part of the chatclient class:

/**

* Sends a broadcast to the server

*/

public static void broadcast() throws IOException {

if (UserInput.getText() == "/upload") {

File myFile = new File (FILE_TO_SEND);

byte [] mybytearray = new byte [(int)myFile.length()];

fis = new FileInputStream(myFile);

bis = new BufferedInputStream(fis);

bis.read(mybytearray,0,mybytearray.length);

os = Socket.getOutputStream();

System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");

os.write(mybytearray,0,mybytearray.length);

os.flush();

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

}

System.out.println("" + UserInput.getText());

outputStream.println(UserInput.getText());

outputStream.flush();

}

Broken part of the server class:

if (input.contains("/upload")) {

byte [] mybytearray = new byte [FILE_SIZE];

InputStream is = csocket.getInputStream();

fos = new FileOutputStream(FILE_TO_RECEIVED);

bos = new BufferedOutputStream(fos);

bytesRead = is.read(mybytearray,0,mybytearray.length);

current = bytesRead;

do {

bytesRead = is.read(mybytearray, current, (mybytearray.length-current));

if (bytesRead >= 0) current += bytesRead;

}

while(bytesRead > -1);

bos.write(mybytearray, 0 , current);

bos.flush();

System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");

}

解决方案

Your copy loop is nonsense. The canonical way to copy a stream in Java is as follows:

while ((count = in.read(buffer)) > 0)

{

out.write(buffer, 0, count);

}

where 'count' is an int, and 'buffer' is a byte[] array of length > 0. I usually use 8192.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。首先,你需要使用Java中的Socket类来创建一个服务器端,并监听客户端的连接请求。然后,你需要根据HTTP协议规范来解析客户端发送的请求信息,包括请求方法、请求路径、请求头、请求体等信息。 对于GET请求,你需要根据请求路径读取相应的文件并将其发送给客户端。对于POST请求,你需要从请求体中读取相应的参数,并根据参数执行相应的操作,并将结果返回给客户端。 下面是一个简单的Java代码示例,用于创建一个基本的HTTP服务器,可以接收客户端的GET和POST请求,并向客户端传输请求的文件: ```java import java.io.*; import java.net.*; import java.util.*; public class HttpServer { public static void main(String[] args) throws IOException { // 创建一个服务器Socket,并监听指定的端口号 ServerSocket serverSocket = new ServerSocket(8888); System.out.println("HTTP Server is running on port 8888..."); // 不断接收客户端的连接请求 while (true) { Socket clientSocket = serverSocket.accept(); System.out.println("Accepted connection from " + clientSocket.getRemoteSocketAddress()); // 获取客户端发送的HTTP请求信息 BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String requestLine = in.readLine(); System.out.println("Received request: " + requestLine); // 解析HTTP请求信息中的请求方法和请求路径 StringTokenizer st = new StringTokenizer(requestLine); String method = st.nextToken(); String requestPath = st.nextToken(); // 处理GET请求 if (method.equals("GET")) { // 读取请求路径对应的文件内容 File file = new File("." + requestPath); if (file.exists() && file.isFile()) { byte[] fileContent = Files.readAllBytes(file.toPath()); // 返回HTTP响应信息 String response = "HTTP/1.1 200 OK\r\n" + "Content-Type: " + Files.probeContentType(file.toPath()) + "\r\n" + "Content-Length: " + fileContent.length + "\r\n" + "\r\n"; OutputStream out = clientSocket.getOutputStream(); out.write(response.getBytes()); out.write(fileContent); out.flush(); out.close(); } else { // 请求路径对应的文件不存在,返回404错误 String response = "HTTP/1.1 404 Not Found\r\n" + "\r\n"; OutputStream out = clientSocket.getOutputStream(); out.write(response.getBytes()); out.flush(); out.close(); } } // 处理POST请求 else if (method.equals("POST")) { // 读取请求体中的参数 String line; int contentLength = 0; while ((line = in.readLine()) != null && line.length() > 0) { if (line.startsWith("Content-Length: ")) { contentLength = Integer.parseInt(line.substring(16)); } } char[] buf = new char[contentLength]; in.read(buf, 0, contentLength); String requestBody = new String(buf); // 根据请求参数执行相应操作,并返回结果 // ... // 返回HTTP响应信息 String response = "HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: " + response.length() + "\r\n" + "\r\n" + response; OutputStream out = clientSocket.getOutputStream(); out.write(response.getBytes()); out.flush(); out.close(); } // 关闭连接 in.close(); clientSocket.close(); } } } ``` 注意,这个代码示例只是一个基本的HTTP服务器实现,实际应用中还需要考虑一些安全性、性能、并发等方面的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值