...

View Full Version : TCP transfer of fileName



esaner
01-20-2012, 05:59 PM
Hi,

Basically what I'm trying to do is send the file name from a client device to the server and for the server to read the file name and then create the file.

Having difficulties as I've managed to successful transfer the file between the two devices creating a manual file name but when I try it with this code trying to send the file name the error message I receive is:

java.io.FileNotFoundException: C:\Desktop\2012-01-12_15-53-45.jpg
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at ss.comds.Connection.run(Main.java:62)

So as you can see in the first line it has managed to successful read in the name of the file but then throws up the error on the FileOutputStream even though it works on its own. I can't figure out what the problem is. Here is the problem code area for the server


class Connection extends Thread {

DataInputStream input;
FileInputStream FIS;

Socket clientSocket;

public Connection (Socket aClientSocket) {
try {
clientSocket = aClientSocket;
input = new DataInputStream( clientSocket.getInputStream());
this.start();
}
catch(IOException e) {
System.out.println("Connection:"+e.getMessage());
}
}

public void run()
{
byte[] bb = new byte[1024];

try {
input.read(bb, 0, bb.length);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String fileName = new String(bb);

try {

FileOutputStream fos = new FileOutputStream("C:\\Desktop\\" + fileName);


while (true)
{
int result= input.read(bb,0,bb.length);
if (result==-1)
{
break;
}
fos.write(bb,0,result);
}
fos.close();
input.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Any help would be greatly appreciated.

Cheers :)

esaner
01-28-2012, 07:45 PM
This can be closed, figured it out :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum