62,621
社区成员
发帖
与我相关
我的任务
分享
public class UploadActionListener implements ActionListener {
private String path;
1. private ClientFrame clientFrame;
private Socket clientSocket;
private OutputStream os;
private BufferedOutputStream bos;
private DataOutputStream dos;
private InputStream is;
private BufferedInputStream bis;
private DataInputStream dis;
//进度条
2. private ProgressMonitor pm;
3. public UploadActionListener(String path,ClientFrame clientFrame,Socket clientSocket,
OutputStream os,BufferedOutputStream bos,DataOutputStream dos,
InputStream is,BufferedInputStream bis,DataInputStream dis){
this.path = path;
this.clientFrame = clientFrame;
this.clientSocket = clientSocket;
this.os = os;
this.bos = bos;
this.dos = dos;
this.is = is;
this.bis = bis;
this.dis = dis;
}
public void actionPerformed(ActionEvent arg0) {
try {
File file = new File(path);
System.out.println("上传文件为:" + path);
4. pm = new ProgressMonitor(clientFrame,"Monitoring Progress","Test",0,100);
5. pm.setProgress(0);
6. pm.setMillisToPopup(1000);
断点 for(int h = 0;h<=100;h++){
7. pm.setProgress(h);
}
//获取文件后缀名
int str = path.lastIndexOf(".");
String ext = path.substring(str + 1, path.length());
//向服务器端发送文件后缀名
dos.writeUTF(ext);
dos.flush();
//本地文件输入输出流
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis1 = new BufferedInputStream(fis);
byte [] buffer = new byte[10240];
int i = -1;
while((i = bis1.read(buffer)) > 0){
dos.write(buffer,0,i);
System.out.println("333333333");
dos.flush();
}
System.out.println("文件上传完毕");
bis1.close();
fis.close();
dos.close();
bos.close();
os.close();
dis.close();
bis.close();
is.close();
clientSocket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}