无法使用Java中的套接字将文件从客户端发送到服务器
我正在尝试编写一些代码,允许客户端将文件发送到服务器。但是我不能用这个代码发送文件 服务器:无法使用Java中的套接字将文件从客户端发送到服务器,java,file,sockets,network-programming,client-server,Java,File,Sockets,Network Programming,Client Server,我正在尝试编写一些代码,允许客户端将文件发送到服务器。但是我不能用这个代码发送文件 服务器: package Server; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class SimpleServer { public static int serverThreadCount = 0; public static final int maxUser =
package Server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleServer {
public static int serverThreadCount = 0;
public static final int maxUser = 10;
public static String[] username = new String[maxUser];
public static String[] password = new String[maxUser];
public static String[] online = new String[maxUser];
public static void main(String[] args) {
int id = 1;
username[0] = "a";
username[1] = "b";
username[2] = "c";
username[3] = "d";
username[4] = "e";
username[5] = "f";
username[6] = "g";
username[7] = "h";
username[8] = "i";
username[9] = "j";
for(int i=0; i<maxUser; i++)
{
password[i] = "123";
online[i] = "free";
}
try
{
ServerSocket ss = new ServerSocket(7777);
System.out.println("Server has successfully been started.");
while(true)
{
Socket socket = ss.accept();
new Thread(new ServerThread(socket, id)).start();
serverThreadCount++;
System.out.println("Client " + id + " is now connected to the server.");
id++;
}
}
catch(IOException e)
{
System.err.println("IOException in serverSocket");
e.printStackTrace();
}
}
public static boolean verifyLogin(String u, String p)
{
for(int i=0; i<maxUser; i++)
{
if(username[i].equals(u) && password[i].equals(p)) return true;
}
return false;
}
public static void setOnline(String name)
{
for(int i=0; i<maxUser; i++)
{
if(online[i].equals("free"))
{
online[i] = name;
break;
}
}
}
public static boolean isOnline(String u)
{
for(int i=0; i<maxUser; i++)
{
if(online[i].equals(u)) return true;
}
return false;
}
}
客户:
package Client;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class SimpleClient {
private static Socket socket;
private static BufferedReader br;
private static PrintWriter pw;
private static String fileName;
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String send = "", rec = "";
try
{
socket = new Socket("localhost", 7777);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
}
catch(UnknownHostException unknownHostException)
{
System.err.println("I don't know this" + socket.getPort() +"host!");
unknownHostException.printStackTrace();
System.exit(1);
}
catch(IOException e)
{
System.err.println("An IO exception has occured when initializing client.");
closeconn();
System.exit(1);
}
try
{
rec = br.readLine();
if(rec != null)
{
System.out.println("Server: " + rec);
}
else
{
System.err.println("Cannot read from socket.");
closeconn();
System.exit(0);
}
}
catch(IOException e)
{
System.err.println("Cannot read from socket.");
closeconn();
System.exit(0);
}
while(true)
{
System.out.println("Tell something to server:");
try
{
send = scanner.nextLine();
if(send.equals("sendfile"))
{
sendFile();
break;
}
}
catch(NoSuchElementException noSuchElementException)
{
noSuchElementException.printStackTrace();
}
catch(IllegalStateException illegalStateException)
{
illegalStateException.printStackTrace();
}
pw.println(send);
pw.flush();
try
{
rec = br.readLine();
if(rec != null)
{
System.out.println("Server: " + rec);
}
else
{
System.err.println("Cannot read from socket.");
break;
}
}
catch(IOException e)
{
System.err.println("Cannot read from Socket");
e.printStackTrace();
break;
}
if(send.equals("BYE"))
{
System.out.println("See you later.");
break;
}
}
closeconn();
}
private static void closeconn() {
try
{
br.close();
pw.close();
socket.close();
}
catch(IOException e)
{
System.err.println("An error occured when closing client.");
e.printStackTrace();
}
}
private static String some()
{
return "success!";
}
private static void sendFile()
{
try
{
System.out.print("Please enter file name: ");
fileName = br.readLine();
File file = new File(fileName);
byte[] bytearray = new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
dis.readFully(bytearray, 0, bytearray.length);
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(file.getName());
dos.writeLong(bytearray.length);
dos.write(bytearray, 0, bytearray.length);
dos.flush();
System.out.println("success!");
}
catch(FileNotFoundException fileNotFoundException)
{
System.err.println("File does not exist!");
fileNotFoundException.printStackTrace();
}
catch(NullPointerException nullPointerException)
{
nullPointerException.printStackTrace();
}
catch(EOFException eOFException)
{
System.err.println("End of file exception.");
eOFException.printStackTrace();
}
catch(IOException iOException)
{
iOException.printStackTrace();
}
}
public static void receiveFile(String fileName)
{
try
{
int bytesRead;
InputStream is = socket.getInputStream();
DataInputStream dis = new DataInputStream(is);
fileName = dis.readUTF();
OutputStream os = new FileOutputStream("received_from_server_" + fileName);
long size = dis.readLong();
byte[] buffer = new byte[1024];
while(size > 0 && (bytesRead = dis.read(buffer, 0, (int)Math.min(buffer.length, size))) != -1)
{
os.write(buffer, 0, bytesRead);
size -= bytesRead;
}
os.close();
is.close();
}
catch(IOException iOException)
{
iOException.printStackTrace();
}
}
}
要登录,需要输入登录密码用户名
从客户端接收到“sendfile”后,类客户端调用sendfile方法,该方法将所需文件转换为字节数组。从客户端接收“sendfile”时,服务器调用接收并生成从客户端发送的文件的方法receiveFile “我不能用此代码发送文件”是什么意思?您是否在服务器或客户端收到错误/异常,它是否在某个位置阻塞,在哪一点阻塞,您是否收到无效/不完整的数据等?@RolandBär,代码根本不起作用。没有例外或错误。在我将文件名输入到客户端后,它就保留在那里,什么也没有发生。这是什么意思?”fileName=br.readLine();'在客户端不返回?我想你在下一行尝试了一个断点,然后单步走了?
package Client;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class SimpleClient {
private static Socket socket;
private static BufferedReader br;
private static PrintWriter pw;
private static String fileName;
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String send = "", rec = "";
try
{
socket = new Socket("localhost", 7777);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
pw = new PrintWriter(socket.getOutputStream());
}
catch(UnknownHostException unknownHostException)
{
System.err.println("I don't know this" + socket.getPort() +"host!");
unknownHostException.printStackTrace();
System.exit(1);
}
catch(IOException e)
{
System.err.println("An IO exception has occured when initializing client.");
closeconn();
System.exit(1);
}
try
{
rec = br.readLine();
if(rec != null)
{
System.out.println("Server: " + rec);
}
else
{
System.err.println("Cannot read from socket.");
closeconn();
System.exit(0);
}
}
catch(IOException e)
{
System.err.println("Cannot read from socket.");
closeconn();
System.exit(0);
}
while(true)
{
System.out.println("Tell something to server:");
try
{
send = scanner.nextLine();
if(send.equals("sendfile"))
{
sendFile();
break;
}
}
catch(NoSuchElementException noSuchElementException)
{
noSuchElementException.printStackTrace();
}
catch(IllegalStateException illegalStateException)
{
illegalStateException.printStackTrace();
}
pw.println(send);
pw.flush();
try
{
rec = br.readLine();
if(rec != null)
{
System.out.println("Server: " + rec);
}
else
{
System.err.println("Cannot read from socket.");
break;
}
}
catch(IOException e)
{
System.err.println("Cannot read from Socket");
e.printStackTrace();
break;
}
if(send.equals("BYE"))
{
System.out.println("See you later.");
break;
}
}
closeconn();
}
private static void closeconn() {
try
{
br.close();
pw.close();
socket.close();
}
catch(IOException e)
{
System.err.println("An error occured when closing client.");
e.printStackTrace();
}
}
private static String some()
{
return "success!";
}
private static void sendFile()
{
try
{
System.out.print("Please enter file name: ");
fileName = br.readLine();
File file = new File(fileName);
byte[] bytearray = new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
dis.readFully(bytearray, 0, bytearray.length);
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(file.getName());
dos.writeLong(bytearray.length);
dos.write(bytearray, 0, bytearray.length);
dos.flush();
System.out.println("success!");
}
catch(FileNotFoundException fileNotFoundException)
{
System.err.println("File does not exist!");
fileNotFoundException.printStackTrace();
}
catch(NullPointerException nullPointerException)
{
nullPointerException.printStackTrace();
}
catch(EOFException eOFException)
{
System.err.println("End of file exception.");
eOFException.printStackTrace();
}
catch(IOException iOException)
{
iOException.printStackTrace();
}
}
public static void receiveFile(String fileName)
{
try
{
int bytesRead;
InputStream is = socket.getInputStream();
DataInputStream dis = new DataInputStream(is);
fileName = dis.readUTF();
OutputStream os = new FileOutputStream("received_from_server_" + fileName);
long size = dis.readLong();
byte[] buffer = new byte[1024];
while(size > 0 && (bytesRead = dis.read(buffer, 0, (int)Math.min(buffer.length, size))) != -1)
{
os.write(buffer, 0, bytesRead);
size -= bytesRead;
}
os.close();
is.close();
}
catch(IOException iOException)
{
iOException.printStackTrace();
}
}
}