The Artima Developer Community
Sponsored Link

Java Answers Forum
Create a directory on the server

2 replies on 1 page. Most recent reply: Feb 13, 2004 10:56 AM by steps

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
steps

Posts: 14
Nickname: spanishste
Registered: Nov, 2003

Create a directory on the server Posted: Feb 13, 2004 1:35 AM
Reply to this message Reply
Advertisement
hello,
After the user is authenticated on the client side, I'd like to create a directory on the server on the user name who has logged into the application on the client.I tried using mkdir and it worked on the local drive.How can i implement on the server side.Any example code would be highly appreciable.

Thanks in advance


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Create a directory on the server Posted: Feb 13, 2004 4:09 AM
Reply to this message Reply
I don't know what you're using on the Server side. If you're using servlets then you should be able to use the java.io.File mkdir() method to create a directory on the server.

If you're not using servlets, then let us know.

Adam

steps

Posts: 14
Nickname: spanishste
Registered: Nov, 2003

Re: Create a directory on the server Posted: Feb 13, 2004 10:56 AM
Reply to this message Reply
Adam,

Here's what am trying to do:I've a GUI where the user enters his username and password.After, the user is authenticated, he shld be able to send files to a remote pc.Ip-address and port no's would be the inputs.when the user enters the ip-address and port no and press a button, a directory has to be created on the remote pc and then a file chooser program will open in order to send files.I dont want to use the sun.net.ftp package.I've written a simple client/server program.With this program, I'm able to send the file to a remote pc but the contents are written into a txt file.
I'm quiet confused with the streams.Could somebody please help me with my code.

Thanks in advance

Client code
public void tcpClient() {
			  int port = 1234;
			  String server = "xxx.xxx.xxx.xx";//Remote pc Ip-address
			  Socket socket = null;
			  int ERROR = 1;
 
			  //connect to server
			  try {
				   socket = new Socket(server, port);
				   System.out.println("Connected with server "+socket.getInetAddress()+":"+socket.getPort());
			  }
			  catch (UnknownHostException e) {
				   errortext.setText("Could not connect with server");
				   System.out.println(e);
				   System.exit(ERROR);
			  }
			  catch (IOException e) {
				   System.out.println(e);
				   System.exit(ERROR);
			  }
 
			  try {
				   byte[] buf = new byte[1024];
				   OutputStream os = socket.getOutputStream();
				   BufferedOutputStream out = new BufferedOutputStream(os,1024);
				   String file = uploadfile; // the file being read.
				   FileInputStream in = new FileInputStream(file);
				   int i; 
				   int bytecount=1024;
 
				   while ((i = in.read(buf,0,1024)) != -1) {
						bytecount=bytecount+1024; 
						out.write(buf,0,1024);
						out.flush();
				   } 
				   in.close();
				   out.close();
				   System.out.println("Bytes Sent :"+bytecount);
				errortext.setText("Sending File");
			  }
			  catch (IOException e) {
				   System.out.println(e);
			  }
 
			  try {
				   socket.close();
			  }
			  catch (IOException e) {
				   System.out.println(e);
			  }
	}
} 


Server code
import java.net.*;
import java.io.*;
 
public class tcpServer {
	public static MatlabRCServer serverrc;
	public MyField myf;
	//public String uploadfile;
	//private javax.swing.JTextField errortext;
	public static void main(String args[]) {
		  serverrc = new MatlabRCServer();
          int port;
          ServerSocket server_socket;
 
          try { 
               port = Integer.parseInt(args[0]);
          }
          catch (Exception e) {
               System.out.println("port = 1234 (default)");
               port = 1234;
          }
 
          try {
 
               server_socket = new ServerSocket(port);
               System.out.println("Server waiting for client on port " + server_socket.getLocalPort());
			   // server infinite loop
               while(true) {
                    Socket socket = server_socket.accept();
                    System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort());
 
                    try { 
                         byte[] b = new byte[1024];
                         int len=0;
                         int bytcount=1024;
                         String FileName="sent.txt"; //writing to a file named ftptest.txt
                         FileOutputStream inFile = new FileOutputStream(FileName);
                         InputStream is = socket.getInputStream();
                         BufferedInputStream in2 = new BufferedInputStream(is,1024);
 
                         while( (len = in2.read(b,0,1024)) != -1 ) {
                              bytcount=bytcount+1024;
                              inFile.write(b,0,1024);
                         }
                         in2.close();
                         inFile.close(); 
                         System.out.println("Bytes Writen : "+bytcount);
						//errortext.setText("Upload Successful");
                    }
                    catch(IOException e) {
                         System.out.println("Unable to open file" + e);
                         return;
                    }
                    try {
                         socket.close();
                         System.out.println("Connection closed by client");
                    }
                    catch (IOException e) {
                         System.out.println(e);
                    }
 
               }
 
          }
 
          catch (IOException e) {
               System.out.println(e);
          }
     }
}

Flat View: This topic has 2 replies on 1 page
Topic: Reading file input Previous Topic   Next Topic Topic: Network listing using JAVA

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use