私は現在、次のようなクライアントサーバーアプリケーションを設計しようとしています。ユーザーはサーバーに接続し、認証がOKになると、サーバーはユーザーにいくつかのファイルを送信します。問題は、それらのファイルが(私の方法では)単一のファイルに書き込まれることです。
ここにいくつかのコードがあります:
ファイルを転送する機能
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
がSocket
タイプであることに言及しなければなりません。
writeBytes
問題は、と組み合わせるということだと思いますwrite
。誰かがこの問題で私を助けることができますか?サーバーとクライアントにファイルとメッセージの両方を受信させるにはどうすればよいですか?