java - 通过 java 套接字传输大文件

标签 java android sockets large-files

<分区>

我编写了一个用于传输小文件的小型客户端-服务器代码。它使用数据输出流和数据输入流的 readFully() 方法。由于显而易见的原因,此代码不适用于较大的文件。在将大文件发送给客户端之前,我正在考虑将大文件分成每个 1Kb 的小块。但我想不出任何解决方案(比如如何以正确的偏移量在数据输出流上写入多个 block ,以及如何在接收端重新组装它们。任何人都可以提供解决方法吗?如果你能修改我的代码,那将非常有帮助:

发件人(服务器):

public void sendFileDOS() throws FileNotFoundException {
    runOnUiThread( new Runnable() {
          @Override
          public void run() {
              registerLog("Sending. . . Please wait. . .");
          }
        });
    final long startTime = System.currentTimeMillis();
    final File myFile= new File(filePath); //sdcard/DCIM.JPG
    byte[] mybytearray = new byte[(int) myFile.length()];
    FileInputStream fis = new FileInputStream(myFile);  
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);
    try {
        dis.readFully(mybytearray, 0, mybytearray.length);
        OutputStream os = socket.getOutputStream();
        //Sending file name and file size to the client  
        DataOutputStream dos = new DataOutputStream(os);     
        dos.writeUTF(myFile.getName());     
        dos.writeLong(mybytearray.length);     
        int i = 0;
        final ProgressBar myProgBar=(ProgressBar)findViewById(R.id.progress_bar);
        while (i<100) {
            dos.write(mybytearray, i*(mybytearray.length/100), mybytearray.length/100);
            final int c=i;
            runOnUiThread( new Runnable() {
                  @Override
                  public void run() {
                      myProgBar.setVisibility(View.VISIBLE);
                      registerLog("Completed: "+c+"%");
                      myProgBar.setProgress(c);
                      if (c==99)
                          myProgBar.setVisibility(View.INVISIBLE);
                  }
                });
            i++;
        }    
        dos.flush();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    runOnUiThread( new Runnable() {
          @Override
          public void run() {
              long estimatedTime = (System.currentTimeMillis() - startTime)/1000;
              registerLog("File successfully sent");
              registerLog("File size: "+myFile.length()/1000+" KBytes");
              registerLog("Elapsed time: "+estimatedTime+" sec. (approx)");
              registerLog("Server stopped. Please restart for another session.");
              final Button startServerButton=(Button)findViewById(R.id.button1);
              startServerButton.setText("Restart file server");
          }
        });
}

接收方(客户端):

public class myFileClient {
final static String servAdd="10.141.21.145";
static String filename=null;
static Socket socket = null;
static Boolean flag=true;

/**
 * @param args
 */
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    initializeClient();
    receiveDOS();      
}
public static void initializeClient () throws IOException {
    InetAddress serverIP=InetAddress.getByName(servAdd);
    socket=new Socket(serverIP, 4444);
}
public static void receiveDOS() {
    int bytesRead;
    InputStream in;
    int bufferSize=0;

    try {
        bufferSize=socket.getReceiveBufferSize();
        in=socket.getInputStream();
        DataInputStream clientData = new DataInputStream(in);
        String fileName = clientData.readUTF();
        System.out.println(fileName);
        OutputStream output = new FileOutputStream("//home//evinish//Documents//Android//Received files//"+ fileName);
        long size = clientData.readLong();
        byte[] buffer = new byte[bufferSize];
        while (size > 0
                && (bytesRead = clientData.read(buffer, 0,
                        (int) Math.min(buffer.length, size))) != -1) {
            output.write(buffer, 0, bytesRead);
            size -= bytesRead;
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

求助!提前致谢! :)

最佳答案

你是对的,这是一种糟糕的方法。它既浪费内存又浪费时间;它假定文件大小为 32 位;它假定整个文件都适合内存;它假设整个文件被一次读取;在读取整个文件之前,它不会发送任何内容。

在 Java 中复制流的规范方法是这样的:

while ((count = in.read(buffer)) > 0)
{
  out.write(buffer, 0, count);
}

它适用于您喜欢的任何大小的缓冲区,因此适用于您能想到的任何大小的文件。在两端使用相同的代码,尽管您不必在两端使用相同大小的缓冲区。当您通过网络进行复制时,您可能认为 1k 或 1.5k 是最佳大小,但这忽略了内核中套接字发送和接收缓冲区的存在。考虑到它们时,最好使用 8k 或更多。

关于java - 通过 java 套接字传输大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17285846/

相关文章:

Java - 如何获取类调用方法

java - JPA/Hibernate 无法确定类型

android - 是否可以通过 firebase auth 仅使用短信服务和离线注册用户,即没有互联网

android - 为 Android 模拟器 x86 构建的 ZeroMQ

linux - 在进程退出时自动清理 PF_UNIX 套接字?

java - 线程化java服务器只发送一个文件

java - 通过 JDBC 执行长插入语句

javascript - 通过远程网站读取智能手机传感器数据

c - 如何在 MSVC 上通过 socket() 创建套接字?

java - Web 服务传输异常 : Not Found [404]