给你提供一个思路
byte[] c = new byte[4096];
while ((in.read(c)) != -1) {
out.write(c);
}
==〉
byte[] c = new byte[4096];
int count = in.read(c);
while (count != -1) {
out.write(c, count);
}
也就是判断一下到底读了多少字节
读的时候也一样
byte[] c = new byte[4096];
while ((in.read(c)) != -1) {
out.write(c);
}
==〉
byte[] c = new byte[4096];
int count = in.read(c);
while (count != -1) {
out.write(c, count);
}
也就是判断一下到底读了多少字节
读的时候也一样
给你提供一个思路
byte[] c = new byte[4096];
while ((in.read(c)) != -1) {
out.write(c);
}
==〉
byte[] c = new byte[4096];
int count = in.read(c);
while (count != -1) {
out.write(c, 0,count);
}
也就是判断一下到底读了多少字节
读的时候也一样
import java.io.*;
import java.util.zip.*;
import java.util.*;public class Server { static int PORT = 9999; public static void main(String[] args) throws IOException {
String filename = "Client.java";
if(args.length >= 1){
filename=args[0];
}
File inFile = new File(filename);
if(inFile.canRead() != true){
System.out.println("Can't read File:" + filename);
System.exit(1);
}
BufferedInputStream in = new BufferedInputStream(new FileInputStream(inFile));
ServerSocket server = new ServerSocket(Server.PORT);
System.out.println("Waiting connection ...");
Socket socket = server.accept();
System.out.println("Socket connected in");
BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(socket.getOutputStream()),4096); System.out.println("Writing ...");
System.out.println(new Date());
byte[] c = new byte[4096];
int count=1;
while (count>0) {
count=in.read(c);
if(count>0)
out.write(c,0,count);
else
System.out.println("stop here,invalid read count:"+count);
}
System.out.println("Writing done!");
System.out.println(new Date());
in.close();
out.close();
socket.close();
System.out.println("Socket closed."); }}import java.net.*;
import java.io.*;
import java.util.zip.*;
import java.util.*;public class Client { public static void main(String[] args) throws IOException
{
String hostname;
if(args.length == 1)
{
hostname=args[0];
}
else
{
hostname = InetAddress.getLocalHost().getHostName();
}
File outFile = new File("out");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile)); Socket client = new Socket(hostname, Server.PORT);
System.out.println("Connect ...");
System.out.println(new Date()); BufferedInputStream in = new BufferedInputStream(new GZIPInputStream(client.getInputStream()),4096);
System.out.println("Reading ..."); byte[] c = new byte[4096];
int readnum=1;
while (readnum>0)
{
readnum=in.read(c);
if(readnum>0)
out.write(c,0,readnum);
else System.out.println("stop here,invalid read count:"+readnum); }
System.out.println("Reading down.");
System.out.println(new Date());
in.close();
out.close();
client.close();
System.out.println("Closed.");
}}
int length = in.readInt();
int count = 0;
byte[] c = new byte[4096];
int len = in.read(c);
while (count != -1) { out.write(c, count);
}
下面是我写的代码,希望对你有用。呵呵 int length = in.readInt();
int count = 0;
byte[] c = new byte[4096];
while (true) {
int len = in.read(c);
if(len != -1) {
count += len;
if(count < length) {
out.write(c, 0, count);
}
else {
out.write(c, 0, length-count);
break;
}
}
}
int len;
while((len=in.read(buf)) != -1){
out.write(buf,0,len);
}
byte[] c = new byte[4096];
while ((in.read(c)) != -1) {
out.write(c);
}
当所传的数据不到4K时程序也传了4K,不足的部分程序补上了空值,应该在这里加上所传数据的大小,按实际大小传送。
用int count=c.length()查出数据的大小,再用out.write(c,ount);
核心代码: byte[] c = new byte[4096];
int count = in.read(c);
while (count != -1) {
out.write(c,0,count);
count = in.read(c);
}