Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
发送zip文件java c#应用程序套接字_Java_C#_Sockets - Fatal编程技术网

发送zip文件java c#应用程序套接字

发送zip文件java c#应用程序套接字,java,c#,sockets,Java,C#,Sockets,嗨,伙计们,我尝试通过套接字发送txt,它使用以下源代码工作,但当我想发送zip文件时出现异常。任何帮助都将不胜感激,谢谢 Java服务器 import java.net.*; import java.io.*; public class Server { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub} sen

嗨,伙计们,我尝试通过套接字发送txt,它使用以下源代码工作,但当我想发送zip文件时出现异常。任何帮助都将不胜感激,谢谢 Java服务器

import java.net.*;
import java.io.*;

public class Server {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub}
        sendFile("FileTest.rar");
    }
    public static Boolean sendFile(String strFileToSend) {
        try {
            ServerSocket serverSocket = new ServerSocket(1592);
            Socket socket = serverSocket.accept();
            System.out.println("Connection accepted from " + socket);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            File file = new File(strFileToSend);
            // send file length
            out.println(file.length());
            // read file to buffer
            byte[] buffer = new byte[(int) file.length()];
            DataInputStream dis = new DataInputStream(new FileInputStream(file));
            dis.read(buffer, 0, buffer.length);
            // send file
            BufferedOutputStream bos = new BufferedOutputStream(
                    socket.getOutputStream());
            bos.write(buffer);
            bos.flush();
            // added
            dis.close();
            serverSocket.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
客户

问题是当我想处理rar或zip文件时。 提前谢谢

我得到了下面的示例。它出现在客户端应用程序的这一行
int length=int.Parse(sr.ReadLine())


例外情况是什么?它发生在哪里?@SpaceUser7448请检查更新。ThanksFirst off-ReadLine()将无法处理二进制文件。它设计用于从文本文件中读取单个以CRLF结尾的行。对于二进制文件,您需要使用read读取数据。第二,您无法预先获得文件的大小,您需要读取流,直到到达流的末尾。查看StreamReader类的帮助以了解更多详细信息和示例。
public static bool receiveFile(string ip,string strFilePath)
        {   

            try
            {
                TcpClient tcpClient = new TcpClient();
                tcpClient.Connect(ip, 1592);
                NetworkStream networkStream = tcpClient.GetStream();
                StreamReader sr = new StreamReader(networkStream);
                //read file length
                int length = int.Parse(sr.ReadLine());

                //read bytes to buffer
                byte[] buffer = new byte[length];
                networkStream.Read(buffer, 0, (int)length);
                //write to file
                BinaryWriter bWrite = new BinaryWriter(File.Open(strFilePath, FileMode.Create));
                bWrite.Write(buffer);
                bWrite.Flush();
                bWrite.Close();
                return true;
            }
            catch(Exception exException)
            {
                MessageBox.Show(exException.Message);
                return false;
            }
            }