Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    TCP transfer of fileName

    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

    Code:
    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

  • #2
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This can be closed, figured it out


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •