Java TCP Server read file name
I want to create small client-server TCP file transfer program. And I have problem with one thing. When I send file from Client to Server, for example a txt file: omg.txt, I want the Server to read the incoming file name.
So - Client send omg.txt, Server says "New file recived: omg.txt". I tried to use BufferedReader
(Server) and DataOutputStream
(Client, because you have to write name of the file to send it) but it didnt work.
EDIT:
Client:
Socket sock = new Socket("localhost",13267);
System.out.println("Wait...");
Scanner input = new Scanner(System.in);
while(true){
// wysylanie
System.out.println("Filename");
String p = input.nextLine();
File myFile = new File (p);
boolean exists = (new File(p)).exists();
if (exists) {
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(sock.getOutputStream());
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Wait...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
System.out.println("Done");
sock.close();
}
Server:
int filesize=6022386;
int bytesRead;
int current = 0;
ServerSocket servsock = new ServerSocket(13267);
Scanner input = new Scanner(System.in);
while (true) {
System.开发者_运维知识库out.println("Wait...");
Socket sock = servsock.accept();
System.out.println("OK : " + sock);
// odbior pliku
byte [] mybytearray = new byte [filesize];
InputStream is = sock.getInputStream();
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
System.out.println("New filename");
String p = input.nextLine();
File plik = new File(p);
FileOutputStream fos = new FileOutputStream(plik);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(mybytearray, 0 , current);
bos.flush();
System.out.println("File saved");
bos.close();
sock.close();
And I tried to do something like that:
Client addon:
DataOutputStream outToServer = new DataOutputStream(sock.getOutputStream());
sentence = input.nextLine();
outToServer.writeBytes(sentence);
Server addon:
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(sock.getInputStream()));
clientSentence = inFromClient.readLine();
System.out.println(clientSentence);
...but unfortunetly I have "Wait.." all the time for Client
TCP just transfers raw data. If you want to send files with a filename, you may want to use a higher level protocol, such as FTP or TFTP.
If you really want to use plain TCP, you'll need to encode the filename in your message somehow. You could, for example, send the filename as the first line of the message, and then have the other end turn the rest of the message into a file with that name.
This may be helpful to you: http://www.adp-gmbh.ch/blog/2004/november/15.html
精彩评论