I need a simple application, preferably a cross-platform one, that enables sending of files between two computers.
It just need to accept and send the files, and show a progress bar. What applications could I use or how could I write one?
I need a simple application, preferably a cross-platform one, that enables sending of files between two computers.
It just need to accept and send the files, and show a progress bar. What applications could I use or how could I write one?
You can write one by using Socket programming in Java. You would need to write a Server and a Client program. The server would use a ServerSocket to listen for connections, and the Client would use a Socket to connect to that server on the specified port.
Here's a tutorial: http://www.javaworld.com/jw-12-1996/jw-12-sockets.html
Check out this tutorial, it's a really basic example. You would probably also want to send control headers prior to the actual file being sent, containing the size of the file, filename, etc.
Alternatively, base it on an existing protocol, like this project.
I would strongly consider using FTP. Apache has a FTP client and a server
Edit: spdenne's suggestion of HTTP is also good, especially if everyone has Java 6. If not, you can use something like Tiny Java Web Server.
Can you install FTP servers on (one of) your machines ?
If you can, you will just have to use a FTP client (FileZilla for example, which have a progress bar).
Sun's Java 6 includes a light-weight HTTP server API and implementation. You could fairly easily use this to serve your file, using URLConnection to obtain it.
Sending and Receiving Files
The sending and receiving of a file basically breaks down to two simple pieces of code.
Recieving code:
ServerSocket serverSoc = new ServerSocket(LISTENING_PORT);
Socket connection = serverSoc.accept();
// code to read from connection.getInputStream();
Sending code:
File fileToSend;
InputStream fileStream = new BufferedInputStream(fileToSend);
Socket connection = new Socket(CONNECTION_ADDRESS, LISTENING_PORT);
OutputStream out = connection.getOutputStream();
// my method to move data from the file inputstream to the output stream of the socket
copyStream(fileStream, out);
The sending piece of code will be ran on the computer that is sending the code when they want to send a file.
The receiving code needs to be put inside a loop, so that everytime someone wants to connect to the server, the server can handle the request and then go back to waiting on serverSoc.accept().
To allow sending files between both computers, each computer will need to run the server (receiving code) to listen for incoming files, and they will both need to run the sending code when they want to send a file.
Progress Bar
The JProgressBar
in Swing is easy enough to use. However, getting it to work properly and show current progress of the file transfer is slightly more difficult.
To get a progress bar to show up on a form only involves dropping it onto a JFrame
and perhaps setting setIndeterminate(false)
so hat it shows that your program is working.
To implement a progress bar correctly you will need to create your own implementation of a SwingWorker
. The Java tutorials have a good example of this in theirlesson in concurrency.
This is a fairly difficult issue on its's own though. I would recommend asking this in it's own question if you need more help with it.
Two popular apps are "scp" and "rsync". These are standard on Linux, are generally available on Unix and can be run on Windows under cygwin, although you may be able to find windows-native apps that can do it as well. (PuTTY can serve as an SCP client).
For any sort of pc-to-pc file transfer, you need to have a listener on the destination PC. This can be a daemon app (or Windows system process), or it can be a Unix-style "superserver" that's configured to load and run the actual file-copy app when someone contacts the listening port.
SCP and one of the rsync modes do require that there be some sort of remote login capability. Rsync can also publish resources that it will handle directory. Since the concept of a Windows "remote login" isn't as well-established as it is under Linux, this may be preferable. Plus it limits remote access to defined sources/targets on the destination machine instead of allowing access to any (authorized) part of the filesystem.
I have Written a code. the code works if the server and the client is the same system but does not work when they are 2 different system can any one help me..
Server.java
import java.io.*;
import java.lang.*;
import java.net.*;
import java.util.*;
import java.io.File;
class Server
{
static final int PORT = 26548;
static String path;
public static void main(String args[])
{
System.out.println("Starting Server");
receive();
}
public static void receive()
{
while ( true )
{
try
{
//Create a socket
ServerSocket srvr = new ServerSocket(PORT);
Socket skt = srvr.accept();
String clientname= skt.getInetAddress().getHostAddress();
int clientport=skt.getPort();
long time = System.currentTimeMillis();
path= "C:\\Ankit Gupta\\Programs\\"+clientname+"\\"+time+" test.pdf";
System.out.println("File Recieved from : "+clientname+" : "+clientport);
//HttpServletRequest req;
//String remoteHost = req.getRemoteHost();
//String adr = getRemoteUser();
FileOutputStream fos = new FileOutputStream(path);
BufferedOutputStream out = new BufferedOutputStream(fos);
BufferedInputStream in = new BufferedInputStream( skt.getInputStream() );
//Read, and write the file to the socket
int i;
System.out.println("Receiving data...");
while ((i = in.read()) != -1)
{
out.write(i);
}
out.flush();
in.close();
out.close();
skt.close();
srvr.close();
System.out.println("Transfer complete.");
printfile();
}
catch(Exception e)
{
System.out.print("Error! It didn't work! " + e + "\n");
}
try
{
Thread.sleep(1000);
}
catch (InterruptedException ie)
{
System.err.println("Interrupted");
}
} //end infinite while loop
}
public static void printfile()
{
delete();
}
public static void delete()
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException ie)
{
System.err.println("Interrupted");
}
System.out.println("Deleting file");
File f = new File(path);
// Make sure the file or directory exists and isn't write protected
if (!f.exists())
throw new IllegalArgumentException("Delete: no such file or directory: " + path);
if (!f.canWrite())
throw new IllegalArgumentException("Delete: write protected: "+ path);
// If it is a directory, make sure it is empty
if (f.isDirectory())
{
String[] files = f.list();
if (files.length > 0)
throw new IllegalArgumentException("Delete: directory not empty: " + path);
}
// Attempt to delete it
boolean success = f.delete();
if (!success)
throw new IllegalArgumentException("Delete: deletion failed");
}
}
Client.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.net.*;
public class Client extends JFrame {
static final int PORT = 26548;
static final String HOST = "10.35.9.152";
JTextField m_fileNameTF = new JTextField(25);
JFileChooser m_fileChooser = new JFileChooser();
Client() {
m_fileNameTF.setEditable(false);
JButton openButton = new JButton("Open");
openButton.addActionListener(new OpenAction());
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(openButton);
content.add(m_fileNameTF);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(content);
this.setSize(400,200);
}
class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent ae) {
int retval = m_fileChooser.showOpenDialog(Client.this);
if (retval == JFileChooser.APPROVE_OPTION) {
File file = m_fileChooser.getSelectedFile();
m_fileNameTF.setText(file.getAbsolutePath());
send(file.getAbsolutePath());
}
}
}
public static void main(String[] args) {
JFrame window = new Client();
window.setVisible(true);
}
public static boolean send( String filename )
{
try
{
System.out.println("Connected to Server");
System.out.println("Sending data...\n");
Socket skt = new Socket(HOST, PORT);
//Create a file input stream and a buffered input stream.
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream( skt.getOutputStream() );
//Read, and write the file to the socket
int i;
while ((i = in.read()) != -1)
{
out.write(i);
//System.out.println(i);
}
System.out.println("Transfer complete.");
//Close the socket and the file
out.flush();
out.close();
in.close();
skt.close();
return true;
}
catch( Exception e )
{
System.out.print("Error! It didn't work! " + e + "\n");
return false;
}
}
}
To transfer over a network more efficiently. Take a look at this article that explains efficient data transfer through zero copy