用Socket传输文件的问题 (很多分,想要吗?)

wes109 2002-11-15 12:21:28
//Server.java

package test;

import java.net.*;
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 {
//Server server1 = new Server();

if(args.length != 1){
System.out.println("Usage:java Server file.name");
System.exit(1);

}

File inFile = new File(args[0]);
if(inFile.canRead() != true){
System.out.println("Can't read File:" + args[0]);
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];
while ((in.read(c)) != -1) {
out.write(c);
}
System.out.println("Writing done!");
System.out.println(new Date());
in.close();
out.close();
socket.close();
System.out.println("Socket closed.");

}

}

//Client.java

package test;

import java.net.*;
import java.io.*;
import java.util.zip.*;
import java.util.*;

public class Client {

public static void main(String[] args) throws IOException{
//Client client1 = new Client();

if(args.length != 1){
System.out.println("Usage:java Client hostName");
System.exit(1);
}

File outFile = new File("out");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));

Socket client = new Socket(args[0], 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];
while ((in.read(c)) != -1) {
out.write(c);
}
System.out.println("Reading down.");
System.out.println(new Date());
in.close();
out.close();
client.close();
System.out.println("Closed.");
}

}

//---------------------------------------------------------------
传163M的文件用了204秒,文件没错误,可以使用

传1k的就坏了,文件变大了,知道原因,但不知道怎么解决
...全文
258 点赞 收藏 8
写回复
8 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
wes109 2002-11-15
问题已解决,谢谢各位
核心代码:

byte[] c = new byte[4096];
int count = in.read(c);
while (count != -1) {
out.write(c,0,count);
count = in.read(c);
}
回复
zfjas 2002-11-15
错误就出再这里,
byte[] c = new byte[4096];
while ((in.read(c)) != -1) {
out.write(c);
}

当所传的数据不到4K时程序也传了4K,不足的部分程序补上了空值,应该在这里加上所传数据的大小,按实际大小传送。
用int count=c.length()查出数据的大小,再用out.write(c,ount);
回复
eyeieye 2002-11-15
有这么麻烦?

byte[] buf = new byte[1024];
int len;
while((len=in.read(buf)) != -1){
out.write(buf,0,len);
}
回复
qljsd 2002-11-15
我建议传文件前先传文件长度,再传文件内容。

前面我还没写完就错点了提交按钮,sorry!
下面是我写的代码,希望对你有用。呵呵

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;
}
}
}
回复
qljsd 2002-11-15
我建议传文件前先传文件长度,再传文件内容。
int length = in.readInt();
int count = 0;
byte[] c = new byte[4096];
int len = in.read(c);
while (count != -1) {

out.write(c, count);
}
回复
masterz 2002-11-15
import java.net.*;
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.");
}

}
回复
arefe 2002-11-15
对不起,错了:(
给你提供一个思路
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);
}
也就是判断一下到底读了多少字节
读的时候也一样
回复
arefe 2002-11-15
给你提供一个思路
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);
}
也就是判断一下到底读了多少字节
读的时候也一样
回复
相关推荐
发帖
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2002-11-15 12:21
社区公告
暂无公告