得到:
private void getDocArchive(){
try {
InputStream in = socket.getInputStream();
BufferedInputStream buffer = new BufferedInputStream(in, 1024);
byte[] b = new byte[1024];
int len = 0;
int bytcount = 1024;
FileOutputStream out = new FileOutputStream("docs/atual.doc");
while ((len = buffer.read(b, 0, 1024)) != -1) {
bytcount = bytcount + 1024;
out.write(b, 0, len);
}
out.flush();
//out.close();
//buffer.close();
} catch (IOException e) {
System.out.println("Ocorreu um erro no recebimento do arquivo");
}
}
回复:
private void reponse() {
try {
OutputStream out = socket.getOutputStream();
InputStream in = new FileInputStream(pdf);
BufferedInputStream buffer = new BufferedInputStream(in, 1024);
byte[] b = new byte[1024];
int len = 0;
int bytcount = 1024;
int i = 0;
while ((len = buffer.read(b, 0, 1024)) != -1) {
bytcount = bytcount + 1024;
out.write(b, 0, len);
}
out.flush();
out.close();
buffer.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
我的客户:
public static void main(String[] args) throws Exception {
try {
Socket client = new Socket("127.0.0.1", 5000);
OutputStream out = client.getOutputStream();
InputStream in = new FileInputStream("C:\\autistmo.docx");
BufferedInputStream buffer = new BufferedInputStream(in, 1024);
byte[] b = new byte[1024];
int len = 0;
int bytcount = 1024;
while ((len = buffer.read(b, 0, 1024)) != -1) {
bytcount = bytcount + 1024;
out.write(b, 0, len);
}
// resposta
BufferedInputStream buffer2 = new BufferedInputStream(in, 1024);
FileOutputStream out2 = new FileOutputStream("docs/final.doc");
while ((len = buffer2.read(b, 0, 1024)) != -1) {
bytcount = bytcount + 1024;
out2.write(b, 0, len);
}
out2.close();
buffer2.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
运行方法:
@Override
public void run() {
getDocArchive();
converter();
reponse();
}