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();
//clientFrame.isMark初始值为false
int j = 0;
if(!clientSocket.isClosed()){
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.setMark(true);
clientFrame.notify();
j=0;
}
}
}
}else{
throw new IOException("服务器socket已关闭");
}
JOptionPane.showMessageDialog(null, "上传成功,如果还要上传,请关闭程序重新上传", "OK", JOptionPane.OK_OPTION);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "服务器端已关闭,上除失败,请关闭程序重新上传", "错误", JOptionPane.ERROR_MESSAGE);

}
}
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;
private ClientFrame clientFrame;
boolean  = 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());
pm.setMillisToPopup(1000);
pm.setMillisToDecideToPopup(2000);
while(true){
synchronized (clientFrame) {
if(!clientFrame.isMark()){
try {
clientFrame.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
pm.setProgress(count.getCount());
clientFrame.setMark(false);
clientFrame.notify();
}
}
}
}
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;
}
}运行的时候图片卡住了,如图还有同样的一个贴http://topic.csdn.net/u/20091128/15/a7bc7d29-fc78-4efe-ac5d-299be925f892.html
忘高手解答,都想了1周了,头都通啦

解决方案 »

  1.   

    图片网站http://home.51.com/ww78026541/photo/item/100135451.html
      

  2.   


    package cn.edu.syict.tcp.client;import java.awt.Button;
    import java.awt.Font;
    import java.awt.Label;import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;public class ClientFrame extends JFrame { private static final long serialVersionUID = 1L; //登陆界面
    private JPanel jp1 = new JPanel();
    private Label lab1 = new Label("文件上传系统");
    private Label lab2 = new Label("用户名");
    private Label lab3 = new Label("密    码");

    private JTextField userName = new JTextField(20);
    private JPasswordField password = new JPasswordField(20);

    private Button resetButton = new Button("重置");
    private Button logButton = new Button("登陆");

    public void Jp1Init(){
    jp1.setLayout(null);
    lab1.setBounds(75,39 - 20,150,39);
    lab1.setFont(new Font("华文仿宋", 1, 24));
    jp1.add(lab1);

    lab2.setBounds(34, 94 - 20, 82, 30);
    lab2.setFont(new Font("仿宋_GB2312", 0, 18));
    jp1.add(lab2);

    userName.setBounds(121, 98 - 20, 136, 30);
    jp1.add(userName);

    lab3.setBounds(34, 153 - 20, 82, 30);
    lab3.setFont(new Font("仿宋_GB2312", 0, 18));
    jp1.add(lab3);

    password.setBounds(121, 153 - 20, 136, 30);
    jp1.add(password);
    password.setEchoChar('*');

    resetButton.setBounds(34, 219 - 20, 85, 35);
    resetButton.setFont(new Font("仿宋_GB2312", 0, 18));
    jp1.add(resetButton);

    logButton.setBounds(163, 219 - 20, 85, 35);
    logButton.setFont(new Font("仿宋_GB2312", 0, 18));
    jp1.add(logButton);
    logButton.addActionListener(new LoginActionListener(userName,password,this));
    }

    public ClientFrame(){
    this.jframeInit();
    }

    public void jframeInit(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setBounds(350, 190, 300, 300);

    this.Jp1Init();
    this.add(jp1); this.setVisible(true);
    }

    public static void main(String[] args) {
    new ClientFrame();
    } public JPanel getJp1() {
    return jp1;
    } public void setJp1(JPanel jp1) {
    this.jp1 = jp1;
    }
    }
    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.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;import javax.swing.JOptionPane;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;public class LoginActionListener implements ActionListener { private Socket clientSocket;

    private ClientFrame clientFrame;
    private JTextField userName;
    private JPasswordField password; public LoginActionListener(JTextField userName,JPasswordField password,ClientFrame clientFrame){
    try {
    this.clientSocket = new Socket(InetAddress.getByName("192.168.18.4"),7999);
    } catch (UnknownHostException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    this.userName = userName;
    this.password = password;
    this.clientFrame = clientFrame;
    }

    public void actionPerformed(ActionEvent event) {
    String name = userName.getText();
    String mima = String.copyValueOf(password.getPassword());
    if("".equals(name) && "".equals(mima)){
    System.out.println("账号密码格式不正确");
    }else{
    System.out.println("账号密码输入格式正确");
    OutputStream os = null;
    //向服务器发送账号密码
    try {
    os = clientSocket.getOutputStream();
    } catch (IOException e) {
    e.printStackTrace();
    }
    BufferedOutputStream bos = new BufferedOutputStream(os);
    DataOutputStream dos = new DataOutputStream(bos);
    try {
    dos.writeUTF(name);
    dos.flush();
    dos.writeUTF(mima);
    dos.flush();
    System.out.println("账号信息发送完毕");
    } catch (IOException e) {
    e.printStackTrace();
    }
    //返回是否登陆成功
    InputStream is = null;
    try {
    is = clientSocket.getInputStream();
    } catch (IOException e) {
    e.printStackTrace();
    }
    BufferedInputStream bis = new BufferedInputStream(is);
    DataInputStream dis = new DataInputStream(bis);
    String logMark = null;
    try {
    logMark = dis.readUTF();
    } catch (IOException e) {
    e.printStackTrace();
    }
    //登陆成功
    if("true".equals(logMark)){
    System.out.println("登陆成功");
    new LoginFrame(clientFrame,clientSocket,os,bos,dos,is,bis,dis);
    }
    //登陆失败
    if("false".equals(logMark)){
    System.out.println("账号和密码错误");
    JOptionPane.showMessageDialog(null, "密码不正确,请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
    }
    }
    }
    }
      

  3.   

    楼主,我这里又缺少LoginFrame,而且
    clientFrame.setMark和clientFrame.isMark方法不存在。
    所以没法帮你调。我也懒得自己去实现了。
    ps:看你标题,好像你有点放弃得意思啊,我捐给你分了,虽然少,也是点心意,再接再厉,解决这个问题!
    友情UP!
      

  4.   

    2009-11-29 02:44:18   170   可用分捐赠   给分,他得问题1星期都没人帮系统说明:crazylaa   在2009-11-29   02:44:18给   greenbar   捐赠了170可用分;greenbar   收到了:153可用分
      

  5.   

    谢谢crazylaa,一定不放弃,天天up
      

  6.   

    http://www.rayfile.com/files/d6d70080-dc91-11de-8e95-0014221b798a/
    这个是代码下载的地址,有服务器端和客户端,服务器端在你的机器上跑要改几个位置,客户端登陆进去
    后一个显示的进度条下面那个框是写上传文件相关信息的,不是进度条,弹出来的才是,如果时间充足的话
    帮我看一看 谢谢
      

  7.   

    up,我去载来看看。不行的话明天去公司找个专门研究socket和thread的人看看
      

  8.   

    上传到第四步就停止了,就是10240*3以后会出现错误, 只是改了下IP.还要改几个位置啊.
    到处都是SOCKET错误,就不明白了,刚写进去,然后读出来就错.
      

  9.   

    那个file存的位置要改,你根本没这个目录  应该就可以了
    应该是SocketRecvThread.java这个类把