Search code examples
filetcptransferjprogressbar

Transfering mp3 files over a TCP connection. Displaying the download progress using JProgressBar


I am working on a program that sends an mp3 file over a TCP connection. I am trying to use a JProgressBar to show the download progress. However I don't know the file size before fully downloading it. Is there anyway I can get the file size before downloading it from the server side?

Thanks in advance. Here is my code

SERVER:

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
dataOut = new BufferedOutputStream(clientSocket.getOutputStream());
file = new File(LIBRARY, data);
//get length of file
int fileLength = (int)file.length();

fileData = new byte[fileLength];
fileIn = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fileIn);
bis.read(fileData, 0 , fileData.length);
OutputStream os = clientSocket.getOutputStream();
os.write(fileData, 0 , fileData.length);
os.flush();
clientSocket.close();

CLIENT:

int filesize=999999999; //A TEMPORARY FILE SIZE

long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
// localhost for testing
Socket sock = new Socket(SERVER_NAME,DEST_PORT);
PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
// receive file
byte [] mybytearray  = new byte [filesize];
out.println(request);
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream(request);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
System.out.println(bytesRead);

do {
      bytesRead = is.read(mybytearray, current, (mybytearray.length-current));
      if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);

bos.write(mybytearray, 0 , current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(current);
bos.close();
sock.close();

Solution

  • SOLVED: The solution was not very hard actually. I simply sent the file size before sending the file. So, client receives the file size sets its maximum to file size JProgressBar.setMaximum(fileLength); Then increment the progressbar's value using setValue(); Here's my updated code:

    SERVER:

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
    
    dataOut = new BufferedOutputStream(clientSocket.getOutputStream());
    file = new File(LIBRARY, data);
    //get length of file
    int fileLength = (int)file.length();
    
    out.println(fileLength);// this sends the file length
    
    fileData = new byte[fileLength];
    fileIn = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fileIn);
    bis.read(fileData, 0 , fileData.length);
    OutputStream os = clientSocket.getOutputStream();
    os.write(fileData, 0 , fileData.length);
    os.flush();
    clientSocket.close();
    

    CLIENT:

    progressBar.setValue(0);
    int bytesRead;
    int current = 0;
    // localhost for testing
    Socket sock = new Socket(SERVER_NAME,DEST_PORT);
    PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
    // receive file
    BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));   //CREATE BUFFERED READER TO READ THE FILE SIZE
    out.println(request);
    InputStream is = sock.getInputStream();
    FileOutputStream fos = new FileOutputStream(request);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    int fileLen = Integer.parseInt(in.readLine());
    progressBar.setMaximum(fileLen); // READ THE FILE SIZE
    byte [] byteArray  = new byte [fileLen+100]; //SET THE BYTEARRAY 100 BYTES EXTRA
    bytesRead = is.read(byteArray,0,byteArray.length);
    current = bytesRead;
    do {
          bytesRead = is.read(byteArray, current, (byteArray.length-current));
          if(bytesRead >= 0) current += bytesRead;
          progressBar.setValue(current);
    } while(bytesRead > -1);
    
    bos.write(byteArray, 0 , current);
    bos.flush();
    //System.out.println(current);
    bos.close();
    fos.close();
    in.close();
    is.close();
    sock.close();