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

时间:2016-12-30 12:15:38

标签: android client-server pc

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

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

任何帮助都将不胜感激。

Android代码(接收文件)

package minor.subham.com.pccontrol;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

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

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;




public class getFile extends ActionBarActivity {

    ImageView getFile;
    public final static int SOCKET_PORT = Constants.SERVER_PORT;      // you may change this
    public final static String SERVER = Constants.SERVER_IP;  // localhost

    public final static int FILE_SIZE = 6022386; // file size temporary hard coded
    // should bigger than the file to be downloaded



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_file);

        getFile = (ImageView) findViewById(R.id.getFile);





        getFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        while(true) {


                            int bytesRead;
                            int current = 0;
                            FileOutputStream fos = null;
                            BufferedOutputStream bos = null;
                            Socket sock = null;
                            final File file;
                            file = new File(Environment.getExternalStorageDirectory(), "PcControl");

                            try {
                                sock = new Socket(SERVER, SOCKET_PORT);
                                System.out.println("Connecting...");

                                // receive file
                                byte [] mybytearray  = new byte [FILE_SIZE];
                                InputStream is = sock.getInputStream();
                               // fos = new FileOutputStream(FILE_TO_RECEIVED);
                                try {

                                    fos = new FileOutputStream(file);

                                } catch (IOException e) {
                                    e.printStackTrace();
                                }



                                bos = new BufferedOutputStream(fos);
                                bytesRead = is.read(mybytearray,0,mybytearray.length);
                                current = bytesRead;

                                do {
                                    bytesRead =
                                            is.read(mybytearray, current, (mybytearray.length-current));
                                    if(bytesRead >= 0) current += bytesRead;
                                } while(bytesRead > -1);

                                bos.write(mybytearray, 0 , current);
                                bos.flush();
                                fos.write(mybytearray);
                                fos.close();
                                System.out.println("File "
                                        + " downloaded (" + current + " bytes read)");
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (UnknownHostException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            } finally {
                                if (fos != null) try {
                                    fos.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                if (bos != null) try {
                                    bos.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                if (sock != null) try {
                                    sock.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }


                        }
                    }
                };

                thread.start();


            }
        });

    }







 }

她是发送文件的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();
    }
  }
}

0 个答案:

没有答案