【发布时间】:2016-03-29 15:12:54
【问题描述】:
我需要为我的项目制作一个带有 Sockets 的文件传输应用程序。到目前为止,我已经编写了一个简单的客户端-服务器通信,但是每当我尝试从客户端输出接收字符数据到服务器输入时,我的代码都会锁定。代码如下:
服务器
public class ClientServer extends Thread {
Socket connection;
File file;
public ClientServer (Socket connection){
this.connection = connection;
this.start();
}
public void run(){
try {
System.out.println("Starting Client Thread...");
BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
System.out.println("IO created...");
System.out.println("input = " + input.readLine());
String s = input.readLine();
System.out.println(s);
file = new File(s);
if(file.isFile()){
System.out.println("File " + s + " exists");
FileInputStream fileOutput = new FileInputStream(file);
byte[] buffer = new byte[1024*1024];
int length = 0;
while((length = fileOutput.read(buffer)) != -1){
output.write(buffer, 0, buffer.length);
}
System.out.println("File " + s + " sent.");
fileOutput.close();
output.close();
input.close();
} else {
System.out.println(s + " is not a file");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
int serverPort = 9000;
ServerSocket server = new ServerSocket(serverPort);
Socket connectionSocket = server.accept();
ClientServer con = new ClientServer(connectionSocket);
}
}
客户
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String ip = "localhost";
int port = 9000;
Socket clientSocket = new Socket(ip, port);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter valid file name and press 'ENTER': ");
String message = userInput.readLine();
System.out.println("Sending message...");
out.write(message);
System.out.println("File name sent: " + message);
int length = in.readInt();
byte[] buffer = new byte[length];
for(int i=0; i< length; i++){
buffer[i] = in.readByte();
System.out.println("Reading byte... " + buffer[i]);
}
File file = new File("C:/Users/Dominik/Desktop/lol/new.php");
FileOutputStream fos = new FileOutputStream(file);
fos.write(buffer);
fos.close();
clientSocket.close();
System.out.println("Closing");
}
}
编辑
这是我的输出,因为没有一个答案能解决我的问题(可能是我做错了什么):
客户
Enter valid file name and press 'ENTER':
C:\\Users\\Dominik\\Desktop\\Login.php //my input
Sending message...
File name sent: C:\\Users\\Dominik\\Desktop\\Login.php
服务器
Starting Client Thread...
IO created...
//Server should sysout the message sent, but it doesn't do so, its just stuck here
【问题讨论】:
-
你有执行的输出吗?你有很多日志可以帮助指出问题
-
你能告诉我你想做什么。我想你想传输文件。对吗?
-
你的
FileInputStream到底为什么叫fileOutput?不要写这样的代码。请。 -
我想通过客户端输入的路径从服务器传输文件。