通过TCP连接传输mp3文件。使用JProgressBar显示下载进度

时间:2013-11-29 05:38:14

标签: file tcp transfer jprogressbar

我正在开发一个通过TCP连接发送mp3文件的程序。我正在尝试使用JProgressBar来显示下载进度。但是在完全下载之前我不知道文件大小。无论如何我可以在从服务器端下载之前获取文件大小吗?

提前致谢。这是我的代码

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();

客户端:

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();

1 个答案:

答案 0 :(得分:1)

解决: 解决方案实际上并不是很难。我只是在发送文件之前发送文件大小。因此,客户端接收文件大小将其最大值设置为文件大小JProgressBar.setMaximum(fileLength);然后使用setValue()增加进度条的值; 这是我更新的代码:

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();

客户端:

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();