开发者社区> 问答> 正文

java 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();
        }

}
}

客户端:

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();
    }

}
}

展开
收起
蛮大人123 2016-05-27 14:09:15 2685 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    你的代码没有问题,我测试了你的代码,传输的文件是几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();
        }
    2019-07-17 19:17:45
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载