????3.?????

 

package sterning;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class ClientTest {
private ClientSocket cs = null;
private String ip = "localhost";// ???ó??????IP
private int port = 8821;
private String sendMessage = "Windwos";
public ClientTest() {
try {
if (createConnection()) {
sendMessage();
getMessage();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private boolean createConnection() {
cs = new ClientSocket(ip?? port);
try {
cs.CreateConnection();
System.out.print("????????????!" + " ");
return true;
} catch (Exception e) {
System.out.print("????????????!" + " ");
return false;
}
}
private void sendMessage() {
if (cs == null)
return;
try {
cs.sendMessage(sendMessage);
} catch (Exception e) {
System.out.print("??????????!" + " ");
}
}
private void getMessage() {
if (cs == null)
return;
DataInputStream inputStream = null;
try {
inputStream = cs.getMessageStream();
} catch (Exception e) {
System.out.print("?????????????? ");
return;
}
try {
//???????·????????????????????????ж?????
String savePath = "E:\";
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
int passedlen = 0;
long len=0;
savePath += inputStream.readUTF();
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(savePath))));
len = inputStream.readLong();
System.out.println("?????????:" + len + " ");
System.out.println("??????????!" + " ");
while (true) {
int read = 0;
if (inputStream != null) {
read = inputStream.read(buf);
}
passedlen += read;
if (read == -1) {
break;
}
//??????????????ν????prograssBar??????????????????????????????????Щ????????
System.out.println("?????????" +  (passedlen * 100/ len) + "% ");
fileOut.write(buf?? 0?? read);
}
System.out.println("??????????????" + savePath + " ");
fileOut.close();
} catch (Exception e) {
System.out.println("???????????" + " ");
return;
}
}
public static void main(String arg[]) {
new ClientTest();
}
}

??????????????????????????????????????????????????????.???в??.?????ж???????????????????????????????????????.