客户端-服务器应用程序文件传输
我目前正在尝试设计一个客户端服务器应用程序,如下所示:用户连接到服务器,当身份验证正常时,服务器向用户发送一些文件。问题是这些文件被写入单个文件中(使用我的方法)。
下面是一些代码:
传输文件的函数
public void processFile(int id, DataOutputStream oStream, Socket socket, int tip){
String fileName;
if(tip==0){
fileName="File"+Integer.toString(Intrebari[id])+".txt";
}else{
fileName="Image"+Integer.toString(Intrebari[id])+".jpg";
}
byte[] buffer=null;
int bytesRead = 0;
FileInputStream file=null;
try {
file = new FileInputStream(fileName);
buffer = new byte[socket.getSendBufferSize()];
while((bytesRead = file.read(buffer))>0)
{
oStream.write(buffer,0,bytesRead);
}
file.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
以及选择必须发送哪个文件的函数
private void send(int id) throws IOException {
os.writeBytes("sendFile" + id+"\n");
System.out.println("sendFile" + id);
generatedFiles.processFile(id, os, comunicare, 0);
if (generatedFiles.Imagini[id] == 1) {
os.writeBytes("sendImage" + id+"\n");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(clientThread.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("sendImage" + id);
generatedFiles.processFile(id, os, comunicare, 1);
}
}
我必须提到 os
是 DataOutputStream
和 comunicare< /code> 是
Socket
类型。
我认为问题在于我将 writeBytes 与 write 结合起来。谁能帮我解决这个问题吗?如何让服务器和客户端同时接收文件和消息?
I am currently trying to design a client-server application, something like this: the user connects to the server and when the authentication is OK the server send to the user some files. The problem is that those files are written in a single file (with my method).
Here is some code:
The function which transfers the file
public void processFile(int id, DataOutputStream oStream, Socket socket, int tip){
String fileName;
if(tip==0){
fileName="File"+Integer.toString(Intrebari[id])+".txt";
}else{
fileName="Image"+Integer.toString(Intrebari[id])+".jpg";
}
byte[] buffer=null;
int bytesRead = 0;
FileInputStream file=null;
try {
file = new FileInputStream(fileName);
buffer = new byte[socket.getSendBufferSize()];
while((bytesRead = file.read(buffer))>0)
{
oStream.write(buffer,0,bytesRead);
}
file.close();
} catch (IOException ex) {
System.out.println(ex);
}
}
And the function that choose which file have to be send
private void send(int id) throws IOException {
os.writeBytes("sendFile" + id+"\n");
System.out.println("sendFile" + id);
generatedFiles.processFile(id, os, comunicare, 0);
if (generatedFiles.Imagini[id] == 1) {
os.writeBytes("sendImage" + id+"\n");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(clientThread.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("sendImage" + id);
generatedFiles.processFile(id, os, comunicare, 1);
}
}
I have to mention that os
is DataOutputStream
and comunicare
is Socket
type.
I think that the problem is that I combine writeBytes
with write
. Can anyone help me with this problem? How can I make the server and the client to receive both files and messages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我做了这样的事情:
Server -send Command
服务器发送文件
客户端 - 确认文件成功
服务器 - 发送命令
服务器发送文件
客户端-确认文件成功
...
像这样...假设您有一个来自客户端的套接字,那么...
socket.getOutputStream.write("FILE: SomeFile.bin, SIZE: 872973493\r\n".getBytes("选择一种编码”))
socket.getOutputStream.flush(); (仅当您期望服务器字符串响应时才需要刷新:OK SEND ME THE FILE, IM READY,否则也不需要)
客户端读取并看到这是一个文件并且它具有此字节大小,因此它开始从 socket.getInputStream 读取,直到它按预期获得文件的长度。
在此客户端确认他收到文件后
,服务器可以发送另一个文件,您可以代替 FILE,使用 IMAGE: 或您想要的任何内容。您只需从客户端读取消息即可查看它是文件还是图像
以下是一些可能对您有帮助的函数:
I do something like this:
Server -send Command
Server -send File
Client -confirms file sucessfull
Server -send Command
Server -send File
Client -confirms file sucessfull
...
like this...supose you have a socket from a client, than you...
socket.getOutputStream.write("FILE: SomeFile.bin, SIZE: 872973493\r\n".getBytes("choose an encoding"))
socket.getOutputStream.flush(); (you will only need to flush if you are expecting a server string response like: OK SEND ME THE FILE, IM READY, otherwise no need too)
client reads and see that is a file and it has this bytes size, so it starts reading from socket.getInputStream untill it gets the length of file as expected.
after this clients confirm he recived the file
then server can send another file, you could do instead of FILE, use IMAGE: or anything you want. You just have to read the message from client side to see if it is a file or image
Here are some functions that might help you: