客户段
====================package org.xpup.hafmis.signjoint.util;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
//import java.text.SimpleDateFormat;
//import java.util.Date;public class Client extends Thread { public Client(InetAddress addr, String sendstr) throws IOException {
Socket sk = null;
// char a=0;
String writeString, filesize = "";
// FileOutputStream fo = null;
// OutputStreamWriter osw = null;
try {
System.out.println("开始读取文件头................");
String path = "d:\\other\\javac";
File file = new File(path);
File myfile = file.listFiles()[0];
sk = new Socket(addr, 10001);
// sk = new Socket("15.26.6.102", 9901);
// sk = new Socket("119.108.13.232", 10001);
String filename = myfile.getName();
filename = filename.substring(0, filename.lastIndexOf("."));
if(myfile.length()%4096!=0){
filesize=(myfile.length()/4096+1)*4096+"";
}else{
filesize=myfile.length()+"";
}
filename = filename + "_" + filesize;
System.out.println("sendstr......" + filename);
OutputStream os = sk.getOutputStream();
InputStream cis = sk.getInputStream();
os.write(filename.getBytes());
System.out.println("给中心传送文件头,说明我准备给中心发数据了");
while (true) {
byte[] buf = new byte[4096];
if(cis.read(buf,0,4096)!=-1){
writeString=new String(buf).trim();
System.out.println("writeString...."+writeString);
if (writeString.equals("READY")) {
FileInputStream fi = new FileInputStream(myfile);
for(int i=0;i<Integer.parseInt(filesize)/4096;i++){
byte[] cbuf = new byte[4096];
if(fi.read(cbuf,0,4096)!=-1){
System.out.println("收到READY,发送第"+(i+1)+"个4096字节......");
System.out.println("cbuf.toString()....."+new String(cbuf));
os.write(cbuf);
}
}
os.write("FILEOVER".getBytes());
System.out.println("发送FILEOVER......");
}else if (writeString.equals("FILEOK")) {
System.out.println("收到FILEOK,结束socket.........");
cis.close();
os.close();
sk.close();
break;
}else if (writeString.equals("FILEERR")) {
System.out.println("收到FILEERR,结束socket.........并准备重新连接......");
cis.close();
os.close();
sk.close();
break;
}else if (writeString.equals("END")) {
System.out.println("收到END,结束socket.........");
cis.close();
os.close();
sk.close();
break;
}
}
} } catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
} public static void main(String[] args) throws UnknownHostException,
IOException {
InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr);
new Client(addr, "");
}
}//ByteArrayOutputStream deposit = new java.io.ByteArrayOutputStream(4096);
//int k;
//while ((k = cis.read()) != -1) {
//deposit.write(buf, 0, k);
//}
//System.out.println("e...."+new String(buf));
//System.out.println("f...."+new String(buf).trim());
//if (new String(buf).trim().equals("READY")) {
//BufferedReader br = new BufferedReader(new FileReader(myfile));
//while ((s = br.readLine()) != null) {
// temp += s;
// temp += "\r\n";
//}
//os.write("OVER".getBytes());
//}
====================package org.xpup.hafmis.signjoint.util;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
//import java.text.SimpleDateFormat;
//import java.util.Date;public class Client extends Thread { public Client(InetAddress addr, String sendstr) throws IOException {
Socket sk = null;
// char a=0;
String writeString, filesize = "";
// FileOutputStream fo = null;
// OutputStreamWriter osw = null;
try {
System.out.println("开始读取文件头................");
String path = "d:\\other\\javac";
File file = new File(path);
File myfile = file.listFiles()[0];
sk = new Socket(addr, 10001);
// sk = new Socket("15.26.6.102", 9901);
// sk = new Socket("119.108.13.232", 10001);
String filename = myfile.getName();
filename = filename.substring(0, filename.lastIndexOf("."));
if(myfile.length()%4096!=0){
filesize=(myfile.length()/4096+1)*4096+"";
}else{
filesize=myfile.length()+"";
}
filename = filename + "_" + filesize;
System.out.println("sendstr......" + filename);
OutputStream os = sk.getOutputStream();
InputStream cis = sk.getInputStream();
os.write(filename.getBytes());
System.out.println("给中心传送文件头,说明我准备给中心发数据了");
while (true) {
byte[] buf = new byte[4096];
if(cis.read(buf,0,4096)!=-1){
writeString=new String(buf).trim();
System.out.println("writeString...."+writeString);
if (writeString.equals("READY")) {
FileInputStream fi = new FileInputStream(myfile);
for(int i=0;i<Integer.parseInt(filesize)/4096;i++){
byte[] cbuf = new byte[4096];
if(fi.read(cbuf,0,4096)!=-1){
System.out.println("收到READY,发送第"+(i+1)+"个4096字节......");
System.out.println("cbuf.toString()....."+new String(cbuf));
os.write(cbuf);
}
}
os.write("FILEOVER".getBytes());
System.out.println("发送FILEOVER......");
}else if (writeString.equals("FILEOK")) {
System.out.println("收到FILEOK,结束socket.........");
cis.close();
os.close();
sk.close();
break;
}else if (writeString.equals("FILEERR")) {
System.out.println("收到FILEERR,结束socket.........并准备重新连接......");
cis.close();
os.close();
sk.close();
break;
}else if (writeString.equals("END")) {
System.out.println("收到END,结束socket.........");
cis.close();
os.close();
sk.close();
break;
}
}
} } catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
} public static void main(String[] args) throws UnknownHostException,
IOException {
InetAddress addr = InetAddress.getLocalHost();
System.out.println(addr);
new Client(addr, "");
}
}//ByteArrayOutputStream deposit = new java.io.ByteArrayOutputStream(4096);
//int k;
//while ((k = cis.read()) != -1) {
//deposit.write(buf, 0, k);
//}
//System.out.println("e...."+new String(buf));
//System.out.println("f...."+new String(buf).trim());
//if (new String(buf).trim().equals("READY")) {
//BufferedReader br = new BufferedReader(new FileReader(myfile));
//while ((s = br.readLine()) != null) {
// temp += s;
// temp += "\r\n";
//}
//os.write("OVER".getBytes());
//}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货