问题描述
- java socket 通信 服务端接收的文件大于客户端发出的文件 是什么鬼
- //服务端
package Socket;import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;public class Server2User extends Thread{
private Socket socket;
private InputStream is;
private BufferedInputStream bis;
private FileOutputStream fos;
private File file;
private byte name[] = new byte[1024];
private byte transport[] = new byte[1024];
private File path;public Server2User(Socket socket){ this.socket= socket;}public void run(){ try { is = socket.getInputStream(); bis= new BufferedInputStream(is); bis.read(name); String filename = new String(name); filename = filename.trim(); System.out.println(""得到文件名""+filename); path = this.createFile(filename); System.out.println(path.getName()); int length; fos = new FileOutputStream(path); int i = 0; while((length=bis.read(transport))!=-1) { this.write2file(transport); i++; } this.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}private void write2file(byte[] transport){ try { fos.write(transport); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}private File createFile(String filename){ try { file = new File(""f:/test/""+filename); file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return file;}private void close(){ try { if(is!=null) is.close(); if(bis!=null) bis.close(); if(fos!=null) fos.close(); if(is!=null) is.close(); if(socket!=null) socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}
}
//客户端
package Socket;import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.Socket;
import java.net.UnknownHostException;public class ClientSocket_upload {
private Socket socket;
private OutputStream os;
private BufferedOutputStream bos;
private String path;
private FileInputStream fis;
private File file;
public ClientSocket_upload(String path)
{
this.path = path;
}
public void uploadfile(){
try {
//1.创建客户端Socket,指定服务器地址和端口
socket= new Socket(""10.104.77.108"" 1080);
//2.获取输出流,向服务器端发送信息
os = socket.getOutputStream();//字节输出流
bos = new BufferedOutputStream(os);
file = new File(path);
fis= new FileInputStream(file);
String name = file.getName();
byte[] temp = name.getBytes();
byte[] bname = new byte[1024];
byte[] by = new byte[1024];
for(int i = 0 ;i<=bname.length-1;i++)
{
if(i<=temp.length-1)
{
bname[i] = temp[i];
}
else
{
bname[i]=0;
}
}
bos.write(bname);while((fis.read(by))!=-1) { bos.write(by); } fis.close(); bos.close(); os.close(); socket.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}
}
解决方案
你的代码没有问题,我测试了你的代码,传输的文件是几k的小文件没有问题,又传输一个300MB的大文件时也没有问题。
我的测试代码是:自定义一个Server类.
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(1080);
while(true){
Socket socket = server.accept();
new Server2User(socket).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端的main方法是:
public static void main(String[] args) { ClientSocket_upload u = new ClientSocket_upload(""D:/WoodWang/JavaSoft/eclipse-jee-mars-R-win32-x86_64.zip""); u.uploadfile(); }
不知道你的测试代码是怎么写的呢?
解决方案二:
文件接受处理的时候数据编码大小是否控制对了
解决方案三:
大于,大了多少?
先检查一下接收过程,统计一下接收到的大小是否正确?如果不正确,可能是发送端就出错了;如果正确,则是可能是写文件出错了。