Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.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
Java 是否可以通过编程方式将文件从android智能手机发送到计算机/PC?_Java_Android_Usb_File Transfer - Fatal编程技术网

Java 是否可以通过编程方式将文件从android智能手机发送到计算机/PC?

Java 是否可以通过编程方式将文件从android智能手机发送到计算机/PC?,java,android,usb,file-transfer,Java,Android,Usb,File Transfer,我试图找到有关本案例的信息/方法,我找到了3种方法,有: 使用亚洲开发银行 usb主机 usb附件 但是,我仍然没有找到解决办法。有人能帮我处理这个案子吗 您可以使用套接字: 服务器端: import java.io.*; import java.net.*; public class FileServer { public static void main(String[] args) throws IOException { int filesize=602238

我试图找到有关本案例的信息/方法,我找到了3种方法,有:

  • 使用亚洲开发银行

  • usb主机

  • usb附件
  • 但是,我仍然没有找到解决办法。有人能帮我处理这个案子吗

    您可以使用套接字:

    服务器端:

    import java.io.*;
    import java.net.*;
    
    public class FileServer {
    
        public static void main(String[] args) throws IOException {
            int filesize=6022386; // filesize temporary hardcoded
    
            long start = System.currentTimeMillis();
            int bytesRead;
            int current = 0;
    
            // create socket
            ServerSocket servsock = new ServerSocket(1149);
            while (true) {
              System.out.println("Waiting...");
    
              Socket sock = servsock.accept();
              System.out.println("Accepted connection : " + sock);
    
           // receive file
                byte [] mybytearray  = new byte [filesize];
                InputStream is = sock.getInputStream();
                FileOutputStream fos = new FileOutputStream("C:\\Users\\Arwa\\Documents\\WebOffice.jpg"); // destination path and name of file
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                bytesRead = is.read(mybytearray,0,mybytearray.length);
                current = bytesRead;
    
                // thanks to A. Cádiz for the bug fix
                do {
                   bytesRead =
                      is.read(mybytearray, current, (mybytearray.length-current));
                   if(bytesRead >= 0) current += bytesRead;
                } while(bytesRead > -1);
    
                bos.write(mybytearray, 0 , current);
                bos.flush();
                long end = System.currentTimeMillis();
                System.out.println(end-start);
                bos.close();
    
    
    
              sock.close();
              }
        }
    
    }
    
    客户端(Android):

    XML代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tvStatus"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    
        <TextView
            android:id="@+id/tvPath"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Path: " />
    
        <Button
            android:id="@+id/bBrowse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Browse" >
        </Button>
    
        <Button
            android:id="@+id/bSend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send" />
    
        <ImageView
            android:id="@+id/ivPic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ImageView>
    
    </LinearLayout>
    
    
    
    运行adb pull-in系统终端,其中remote是系统路径,local是文件的设备路径。adb pull在cmd(windows)中执行,但是我希望当设备连接到PC时,传输文件过程由android应用程序执行。你有什么想法/解决方案吗?我将如何运行此程序,先生?记住这将是一个单独的程序。我的意思是关于服务器的java代码,你能告诉我如何运行服务器端代码吗。。。??
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tvStatus"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />
    
        <TextView
            android:id="@+id/tvPath"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Path: " />
    
        <Button
            android:id="@+id/bBrowse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Browse" >
        </Button>
    
        <Button
            android:id="@+id/bSend"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send" />
    
        <ImageView
            android:id="@+id/ivPic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ImageView>
    
    </LinearLayout>