进度条问题

greenbar 2009-11-28 03:19:47
进度条问题,希望高手解答,传文件的时候,进度条弹出来,但是卡住了
代码如下:

package cn.edu.syict.tcp.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class UploadActionListener implements ActionListener {

private String path;
private Socket clientSocket;
private OutputStream os;
private BufferedOutputStream bos;
private DataOutputStream dos;
private InputStream is;
private BufferedInputStream bis;
private DataInputStream dis;
private JTextField jtf2;
private ClientFrame clientFrame;
public UploadActionListener(String path,Socket clientSocket,
OutputStream os,BufferedOutputStream bos,DataOutputStream dos,
InputStream is,BufferedInputStream bis,DataInputStream dis,
JTextField jtf2,ClientFrame clientFrame){
this.path = path;
this.clientSocket = clientSocket;
this.os = os;
this.bos = bos;
this.dos = dos;
this.is = is;
this.bis = bis;
this.dis = dis;
this.jtf2 = jtf2;
this.clientFrame = clientFrame;
}
public void actionPerformed(ActionEvent arg0) {
FileInputStream fis = null;
BufferedInputStream bis1 = null;
try {
File file = new File(path);
//发送文件大小
long size = 0;
size = file.length();
if(!clientSocket.isClosed()){
dos.writeLong(size);
dos.flush();
}else{
throw new IOException("服务器socket已关闭");
}

if(!clientSocket.isClosed()){
//发送details
String details = jtf2.getText();
dos.writeUTF(details);
dos.flush();

//获取文件后缀名
int str = path.lastIndexOf(".");
String ext = path.substring(str + 1, path.length());

//向服务器端发送文件后缀名
dos.writeUTF(ext);
dos.flush();
}else{
throw new IOException("服务器socket已关闭");
}

//本地文件输入输出流
fis = new FileInputStream(file);
bis1 = new BufferedInputStream(fis);

byte [] buffer = new byte[10240];
int i = -1;
Count count = new Count(0,0,(int)size);
new Thread(new PMThread(clientFrame,count)).start();
if(!clientSocket.isClosed()){
while((i = bis1.read(buffer)) > 0){
dos.write(buffer,0,i);
dos.flush();
count.setCount(count.getCount() + i);
}
}else{
throw new IOException("服务器socket已关闭");
}
if(bis1!=null){
bis1.close();
}
if(fis!=null){
fis.close();
}
if(dos!=null){
dos.close();
}
if(bos!=null){
bos.close();
}
if(os!=null){
os.close();
}
if(dis!=null){
dis.close();
}
if(bis!=null){
bis.close();
}
if(is!=null){
is.close();
}
if(clientSocket!=null){
clientSocket.close();
}
JOptionPane.showMessageDialog(null, "上传成功,如果还要上传,请关闭程序重新上传", "OK", JOptionPane.OK_OPTION);
} catch (IOException e) {
e.printStackTrace();
try{
if(bis1!=null){
bis1.close();
}
if(fis!=null){
fis.close();
}
if(dos!=null){
dos.close();
}
if(bos!=null){
bos.close();
}
if(os!=null){
os.close();
}
if(dis!=null){
dis.close();
}
if(bis!=null){
bis.close();
}
if(is!=null){
is.close();
}
if(clientSocket!=null){
clientSocket.close();
}
JOptionPane.showMessageDialog(null, "服务器端已关闭,上除失败,请关闭程序重新上传", "错误", JOptionPane.ERROR_MESSAGE);
}catch(Exception es){
es.printStackTrace();
}

}
}
public String getPath()
return path;
}
public void setPath(String path) {
this.path = path;
}
}

当传送文件的时候,进度条卡住,打印的也只有一次
我将里面的setProgress由1-100时,当文件传送完又有进度条了。在传的过程中还是卡的,也只打印一条,
传完后进度条正常走动

package cn.edu.syict.tcp.client;

import javax.swing.ProgressMonitor;

public class PMThread implements Runnable {

private ProgressMonitor pm = null;
private Count count = null;
public PMThread(ClientFrame clientFrame,Count count){
pm = new ProgressMonitor(clientFrame,"Monitoring Progress","Test",0,100);
this.count = count;
}
public void run() {
pm.setMaximum(count.getMax());
pm.setMinimum(count.getMin());
while(count.getCount()!=count.getMax()){
try {
Thread.sleep(2000);
pm.setProgress(count.getCount());
System.out.println(count.getCount());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


package cn.edu.syict.tcp.client;

public class Count {

private int count;

private int max;

private int min;

public Count(int count,int min,int max){
this.count = count;
this.max = max;
this.min = min;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public int getMax() {
return max;
}

public void setMax(int max) {
this.max = max;
}

public int getMin() {
return min;
}

public void setMin(int min) {
this.min = min;
}
}

请高手帮忙解答解答,谢谢咯
...全文
250 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
greenbar 2009-11-29
  • 打赏
  • 举报
回复
http://www.rayfile.com/files/d6d70080-dc91-11de-8e95-0014221b798a/
这个是代码下载的地址,有服务器端和客户端,服务器端在你们机器上跑要改几个位置,客户端登陆进去
后一个显示的进度条下面那个框是写上传文件相关信息的,不是进度条,弹出来的才是,如果时间充足的话
帮我看一看 谢谢
greenbar 2009-11-28
  • 打赏
  • 举报
回复
up
greenbar 2009-11-28
  • 打赏
  • 举报
回复

while((i = bis1.read(buffer)) > 0){
synchronized (clientFrame) {
if(clientFrame.isMark()){
try {
clientFrame.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
dos.write(buffer,0,i);
dos.flush();
j++;
if(j==200){
clientFrame.notify();
j=0;
}
}
}


while(true){
synchronized (clientFrame) {
if(!clientFrame.isMark()){
try {
clientFrame.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
pm.setProgress(count.getCount());
clientFrame.setMark(false);
clientFrame.notify();
}

这个实现了2个线程的调用,还是不行,出来的进度条卡在那儿不动,什么都看不到
为什么啊
老张-AI 2009-11-28
  • 打赏
  • 举报
回复
public class IllegalMonitorStateException
extends RuntimeException
抛出的异常表明某一线程已经试图等待对象的监视器,
或者试图通知其他正在等待对象的监视器而本身没有指定监视器的线程。
greenbar 2009-11-28
  • 打赏
  • 举报
回复

package cn.edu.syict.tcp.client;

import javax.swing.ProgressMonitor;

public class PMThread implements Runnable {

private ProgressMonitor pm = null;
private Count count = null;
private ClientFrame clientFrame;
boolean mark = false;
public PMThread(ClientFrame clientFrame,Count count){
pm = new ProgressMonitor(clientFrame,"Monitoring Progress","Test",0,100);
this.count = count;
this.clientFrame = clientFrame;
}
public void run() {
pm.setMaximum(count.getMax());
pm.setMinimum(count.getMin());
while(true){
synchronized (clientFrame) {
if(!clientFrame.isMark()){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
pm.setProgress(count.getCount());
clientFrame.setMark(false);
notify();
}
}
}
}


package cn.edu.syict.tcp.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class UploadActionListener implements ActionListener {

private String path;
private Socket clientSocket;
private OutputStream os;
private BufferedOutputStream bos;
private DataOutputStream dos;
private InputStream is;
private BufferedInputStream bis;
private DataInputStream dis;
private JTextField jtf2;
private ClientFrame clientFrame;
public UploadActionListener(String path,Socket clientSocket,
OutputStream os,BufferedOutputStream bos,DataOutputStream dos,
InputStream is,BufferedInputStream bis,DataInputStream dis,
JTextField jtf2,ClientFrame clientFrame){
this.path = path;
this.clientSocket = clientSocket;
this.os = os;
this.bos = bos;
this.dos = dos;
this.is = is;
this.bis = bis;
this.dis = dis;
this.jtf2 = jtf2;
this.clientFrame = clientFrame;
}
public void actionPerformed(ActionEvent arg0) {
FileInputStream fis = null;
BufferedInputStream bis1 = null;
try {
File file = new File(path);
//发送文件大小
long size = 0;
size = file.length();
if(!clientSocket.isClosed()){
dos.writeLong(size);
dos.flush();
}else{
throw new IOException("服务器socket已关闭");
}

if(!clientSocket.isClosed()){
//发送details
String details = jtf2.getText();
dos.writeUTF(details);
dos.flush();

//获取文件后缀名
int str = path.lastIndexOf(".");
String ext = path.substring(str + 1, path.length());

//向服务器端发送文件后缀名
dos.writeUTF(ext);
dos.flush();
}else{
throw new IOException("服务器socket已关闭");
}

//本地文件输入输出流
fis = new FileInputStream(file);
bis1 = new BufferedInputStream(fis);

byte [] buffer = new byte[10240];
int i = -1;
Count count = new Count(0,0,(int)size);
new Thread(new PMThread(clientFrame,count)).start();
//false
if(!clientSocket.isClosed()){
while((i = bis1.read(buffer)) > 0){
//count.setCount(count.getCount() + i);
synchronized (clientFrame) {
if(clientFrame.isMark()){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
dos.write(buffer,0,i);
dos.flush();
clientFrame.setMark(true);
notify();
}
}
}else{
throw new IOException("服务器socket已关闭");
}
if(bis1!=null){
bis1.close();
}
if(fis!=null){
fis.close();
}
if(dos!=null){
dos.close();
}
if(bos!=null){
bos.close();
}
if(os!=null){
os.close();
}
if(dis!=null){
dis.close();
}
if(bis!=null){
bis.close();
}
if(is!=null){
is.close();
}
if(clientSocket!=null){
clientSocket.close();
}
JOptionPane.showMessageDialog(null, "上传成功,如果还要上传,请关闭程序重新上传", "OK", JOptionPane.OK_OPTION);
} catch (IOException e) {
e.printStackTrace();
try{
if(bis1!=null){
bis1.close();
}
if(fis!=null){
fis.close();
}
if(dos!=null){
dos.close();
}
if(bos!=null){
bos.close();
}
if(os!=null){
os.close();
}
if(dis!=null){
dis.close();
}
if(bis!=null){
bis.close();
}
if(is!=null){
is.close();
}
if(clientSocket!=null){
clientSocket.close();
}
JOptionPane.showMessageDialog(null, "服务器端已关闭,上除失败,请关闭程序重新上传", "错误", JOptionPane.ERROR_MESSAGE);
}catch(Exception es){
es.printStackTrace();
}

}
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}

改成这样抛异常了

Exception in thread "Thread-4" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:485)
at cn.edu.syict.tcp.client.PMThread.run(PMThread.java:23)
at java.lang.Thread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at cn.edu.syict.tcp.client.UploadActionListener.actionPerformed(UploadActionListener.java:101)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

client.isMark()最初是false
老张-AI 2009-11-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 nj_dobetter 的回复:]
...
        while(count.getCount()!=count.getMax()){
            try {
                Thread.sleep(2000);
                pm.setProgress(count.getCount());
                                    System.out.println(count.getCount());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
...

Thread.sleep(2000);
不应该停两秒,应该是线程1装满一次buf,通知线程2设置 pm.setProgress()
[/Quote]
试试看
whut0802 2009-11-28
  • 打赏
  • 举报
回复
学习,帮顶
crazylaa 2009-11-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 nj_dobetter 的回复:]
...
        while(count.getCount()!=count.getMax()){
            try {
                Thread.sleep(2000);
                pm.setProgress(count.getCount());
                                    System.out.println(count.getCount());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
...

Thread.sleep(2000);
不应该停两秒,应该是线程1装满一次buf,通知线程2设置 pm.setProgress()
[/Quote]

up
nj_dobetter 2009-11-28
  • 打赏
  • 举报
回复
...
while(count.getCount()!=count.getMax()){
try {
Thread.sleep(2000);
pm.setProgress(count.getCount());
System.out.println(count.getCount());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
...


Thread.sleep(2000);
不应该停两秒,应该是线程1装满一次buf,通知线程2设置 pm.setProgress()

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧