[英]Client-server application file transfer
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. 我不得不提到
os
是DataOutputStream
而comunicare
是Socket
类型。
I think that the problem is that I combine writeBytes
with write
. 我认为问题在于我将
writeBytes
与write
结合在一起。 Can anyone help me with this problem? 谁能帮助我解决这个问题? How can I make the server and the client to receive both files and messages?
如何使服务器和客户端同时接收文件和消息?
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(); socket.getOutputStream.write(“ FILE:SomeFile.bin,大小:872973493 \\ r \\ n” .getBytes(“选择编码”))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.
(如果期望服务器字符串响应,则只需要刷新即可:OK,发送文件,IM READY,否则也不需要)客户端读取并看到该文件的大小为字节,因此开始读取从socket.getInputStream直到它获得预期的文件长度。 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. 然后服务器可以发送另一个文件,您可以代替IMAGE做,使用IMAGE:或您想要的任何东西。 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: 以下是一些可能对您有帮助的功能:
public static void readInputStreamToFile(InputStream is, FileOutputStream fout,
long size, int bufferSize) throws Exception
{
byte[] buffer = new byte[bufferSize];
long curRead = 0;
long totalRead = 0;
long sizeToRead = size;
while(totalRead < sizeToRead)
{
if(totalRead + buffer.length <= sizeToRead)
{
curRead = is.read(buffer);
}
else
{
curRead = is.read(buffer, 0, (int)(sizeToRead - totalRead));
}
totalRead = totalRead + curRead;
fout.write(buffer, 0, (int) curRead);
}
}
public static void writeFileInputStreamToOutputStream(FileInputStream in, OutputStream out, int bufferSize) throws Exception
{
byte[] buffer = new byte[bufferSize];
int count = 0;
while((count = in.read(buffer)) != -1)
{
out.write(buffer, 0, count);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.