通过套接字进行PC到Android的文件传输

通过套接字进行PC到Android的文件传输,android,client-server,pc,Android,Client Server,Pc,如何使用套接字编程将文件(未知扩展名,例如pdf/word/jpeg)从PC传输到Android手机。Android手机应该能够检测到文件传输的类型,并且应该能够在之后打开它。此外,该文件应放入外部存储器中以应用程序名称命名的特定文件夹中 我尝试了以下解决方案。但是在这种情况下,当我选择要传输的文件时,在Android端传输的文件具有任意大小,并且与所选文件的大小不同,它具有一些随机大小。此外,传输的文件无法打开,因为它没有固定的扩展名 任何帮助都将不胜感激 Android代码(接收文件) 而她

如何使用套接字编程将文件(未知扩展名,例如pdf/word/jpeg)从PC传输到Android手机。Android手机应该能够检测到文件传输的类型,并且应该能够在之后打开它。此外,该文件应放入外部存储器中以应用程序名称命名的特定文件夹中

我尝试了以下解决方案。但是在这种情况下,当我选择要传输的文件时,在Android端传输的文件具有任意大小,并且与所选文件的大小不同,它具有一些随机大小。此外,传输的文件无法打开,因为它没有固定的扩展名

任何帮助都将不胜感激

Android代码(接收文件)

而她是发送文件的Java代码。假设我想发送文件temp.jpg

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class send {

  public final static int SOCKET_PORT = 8991;  // you may change this
  public final static String FILE_TO_SEND = "temp.jpg";  // you may change this

  public static void main (String [] args ) throws IOException {
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    OutputStream os = null;
    ServerSocket servsock = null;
    Socket sock = null;
    try {
      servsock = new ServerSocket(SOCKET_PORT);
     // while (true) {
        System.out.println("Waiting...");
        try {
          sock = servsock.accept();
          System.out.println("Accepted connection : " + sock);

      // send file
      File myFile = new File (FILE_TO_SEND);
      byte [] mybytearray  = new byte [(int)myFile.length()];
      fis = new FileInputStream(myFile);
      bis = new BufferedInputStream(fis);
      bis.read(mybytearray,0,mybytearray.length);
      os = sock.getOutputStream();
      System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
      os.write(mybytearray,0,mybytearray.length);
      os.flush();
      System.out.println("Done.");
    }
    finally {
      if (bis != null) bis.close();
      if (os != null) os.close();
      if (sock!=null) sock.close();
    }
 // }
}
finally {
  if (servsock != null) servsock.close();
    }
  }
}

那为什么文件不能打开呢?请详细解释。因为传输的文件没有固定的扩展名。在Android Phone中创建的文件具有任意大小。例如,如果temp.jpg的大小为300kb,则手机中的文件输出大小为7MB。它不必有固定的扩展名。只要它有一个扩展。我不明白你的意思。如果生成的文件与原始文件的长度不同,那么这就是您的第一个问题!你为什么一开始不在帖子中提到这一点?请先编辑您的帖子并告诉我哪里出了问题。
请帮助。不要做这种事。您最好将其删除。
传输的文件具有任意大小
。当然,您的代码应该能够传输任意大小的文件。如果你的意思不同,请改写。
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class send {

  public final static int SOCKET_PORT = 8991;  // you may change this
  public final static String FILE_TO_SEND = "temp.jpg";  // you may change this

  public static void main (String [] args ) throws IOException {
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    OutputStream os = null;
    ServerSocket servsock = null;
    Socket sock = null;
    try {
      servsock = new ServerSocket(SOCKET_PORT);
     // while (true) {
        System.out.println("Waiting...");
        try {
          sock = servsock.accept();
          System.out.println("Accepted connection : " + sock);

      // send file
      File myFile = new File (FILE_TO_SEND);
      byte [] mybytearray  = new byte [(int)myFile.length()];
      fis = new FileInputStream(myFile);
      bis = new BufferedInputStream(fis);
      bis.read(mybytearray,0,mybytearray.length);
      os = sock.getOutputStream();
      System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
      os.write(mybytearray,0,mybytearray.length);
      os.flush();
      System.out.println("Done.");
    }
    finally {
      if (bis != null) bis.close();
      if (os != null) os.close();
      if (sock!=null) sock.close();
    }
 // }
}
finally {
  if (servsock != null) servsock.close();
    }
  }
}