patternjavaMinor
Program to transfer files from one server to another in java and also display a progress bar on each file transfer
Viewed 0 times
barfileeachandprogramjavadisplayalsoonefiles
Problem
I have written a java code to transfer files from one server to another using the concept of socket programming. Now, I also want to check for the network connection availability before each file is transferred. I also want that the files transferred should be transferred according to their numerical sequence, and while the files are being transferred, the transfer progress of each file i.e the progress percentage bar should also be visible on the screen.
I would really appreciate if someone can help me with the code. This is the code I have written for the file transfer.
ClientMain.java
```
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ClientMain {
private DirectoryTxr transmitter = null;
Socket clientSocket = null;
private boolean connectedStatus = false;
private String ipAddress;
String srcPath = null;
String dstPath = "";
public ClientMain() {
}
public void setIpAddress(String ip) {
this.ipAddress = ip;
}
public void setSrcPath(String path) {
this.srcPath = path;
}
public void setDstPath(String path) {
this.dstPath = path;
}
private void createConnection() {
Runnable connectRunnable = new Runnable() {
public void run() {
while (!connectedStatus) {
try {
clientSocket = new Socket(ipAddress, 3339);
connectedStatus = true;
transmitter = new DirectoryTxr(clientSocket, srcPath, dstPath);
} catch (IOException io) {
io.printStackTrace();
}
}
}
};
Thread connectionThread = new Thread(connectRunnable);
connectionThread.start();
}
public static void main(String[] args) {
ClientMain main = new ClientMain();
ma
I would really appreciate if someone can help me with the code. This is the code I have written for the file transfer.
ClientMain.java
```
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ClientMain {
private DirectoryTxr transmitter = null;
Socket clientSocket = null;
private boolean connectedStatus = false;
private String ipAddress;
String srcPath = null;
String dstPath = "";
public ClientMain() {
}
public void setIpAddress(String ip) {
this.ipAddress = ip;
}
public void setSrcPath(String path) {
this.srcPath = path;
}
public void setDstPath(String path) {
this.dstPath = path;
}
private void createConnection() {
Runnable connectRunnable = new Runnable() {
public void run() {
while (!connectedStatus) {
try {
clientSocket = new Socket(ipAddress, 3339);
connectedStatus = true;
transmitter = new DirectoryTxr(clientSocket, srcPath, dstPath);
} catch (IOException io) {
io.printStackTrace();
}
}
}
};
Thread connectionThread = new Thread(connectRunnable);
connectionThread.start();
}
public static void main(String[] args) {
ClientMain main = new ClientMain();
ma
Solution
General advice here:
- All your created object instances look like they can be immutable: make them so (for instance by using builders instead of beans -- God do I hate beans).
- You should not embed thread starting logic in a method call -- how do you know anything about the thread after that? Use a method returning a
RunnableorCallable(ideally aFutureTasksince you get both) and manage threads in a separateExecutorService.
- You have many constants: make them
private static final.
Context
StackExchange Code Review Q#27138, answer score: 3
Revisions (0)
No revisions yet.