62,620
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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;
}
}
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();
}
public class IllegalMonitorStateException
extends RuntimeException
抛出的异常表明某一线程已经试图等待对象的监视器,
或者试图通知其他正在等待对象的监视器而本身没有指定监视器的线程。
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)