当前位置: 技术问答>java相关
用Socket传输文件的问题 (很多分,想要吗?)
来源: 互联网 发布时间:2015-11-03
本文导语: //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 Ser...
//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的就坏了,文件变大了,知道原因,但不知道怎么解决
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的就坏了,文件变大了,知道原因,但不知道怎么解决
|
对不起,错了:(
给你提供一个思路
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);
}
也就是判断一下到底读了多少字节
读的时候也一样
给你提供一个思路
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);
}
也就是判断一下到底读了多少字节
读的时候也一样
|
有这么麻烦?
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf)) != -1){
out.write(buf,0,len);
}
byte[] buf = new byte[1024];
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];
while ((in.read(c)) != -1) {
out.write(c);
}
当所传的数据不到4K时程序也传了4K,不足的部分程序补上了空值,应该在这里加上所传数据的大小,按实际大小传送。
用int count=c.length()查出数据的大小,再用out.write(c,ount);
|
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.");
}
}
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.");
}
}
|
我建议传文件前先传文件长度,再传文件内容。
前面我还没写完就错点了提交按钮,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
前面我还没写完就错点了提交按钮,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