Java套接字-在Android中使用AsyncTask上载多个文件

Java套接字-在Android中使用AsyncTask上载多个文件,java,android,sockets,Java,Android,Sockets,如何将多个文件(尤其是图像文件)和一个特殊字符串作为客户端套接字从android发送到java服务器套接字 文件在字符串数组中描述,其中字符串是存储文件的路径 对于一个特殊的字符串,它将用作服务器上的父目录,上传的文件将放在那里 我试过很多教程,比如 这是我做的骨架 已更新 我已成功传输了这些文件,但无法将特殊字符串传递给父级 private void handlingUpload(String vehicleNumber) { System.out.println("PathImage

如何将多个文件(尤其是图像文件)和一个特殊字符串作为客户端套接字从android发送到java服务器套接字

文件在字符串数组中描述,其中字符串是存储文件的路径

对于一个特殊的字符串,它将用作服务器上的父目录,上传的文件将放在那里

我试过很多教程,比如

这是我做的骨架

已更新

我已成功传输了这些文件,但无法将特殊字符串传递给父级

private void handlingUpload(String vehicleNumber) {
    System.out.println("PathImage: " + vehicleNumber);
    ArrayList<String> list = new ArrayList<String>();

    // Get all path each object and sign into list
    for (Image image : images) {
        list.add(image.getPath());
    }

    // just prove if list is not emtpty
    for (String temp : list) {
        System.out.println("FilePath" + temp);
    }

    // The rules is sending a String vehicleNumber as parent folder
    // element in list uploaded Async
    Client client = new Client("192.168.8.34", 59090, vehicleNumber);
    client.execute();
}
package com.tsurumaru.dzil.clientwarehouse.controllers;

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;

public class Client extends AsyncTask<Void, Void, Void> {

    private String address;
    private int port;
    private String textResponse;

    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;
    OutputStream os = null;
    DataOutputStream dos = null;
    Context context;
    ArrayList<String> list;

    public Client(Context context, String address, int port, ArrayList<String> list, String textResponse) {
        this.context = context;
        this.address = address;
        this.port = port;
        this.textResponse = textResponse;
        this.list = list;
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        Socket socket = null;

        try {
            socket = new Socket(address, port);

            //Execute them
            DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

            //write the number of files to the server
            dos.writeInt(list.size());
            System.out.println("Do In Background TO Server" + list.size());
            dos.flush();

            //write file names to the server
            for (String temp : list) {
                dos.writeUTF(temp);
                dos.flush();
            }

            // write file size to the server
            int n = 0;
            byte[]buf = new byte[4092];
            for (String temp : list) {
                File file = new File(temp);
                dos.writeLong(file.length());
                dos.flush();
            }

            // write the file
            for (String temp : list) {
                File file = new File(temp);
                FileInputStream fis = new FileInputStream(file);
                while ((n = fis.read(buf)) != -1) {
                    dos.write(buf, 0, n);
                    dos.flush();
                }
            }


            dos.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute() {
        Log.i("ThreadPost", "onPost");
    }
}
Android客户端

private void handlingUpload(String vehicleNumber) {
    System.out.println("PathImage: " + vehicleNumber);
    ArrayList<String> list = new ArrayList<String>();

    // Get all path each object and sign into list
    for (Image image : images) {
        list.add(image.getPath());
    }

    // just prove if list is not emtpty
    for (String temp : list) {
        System.out.println("FilePath" + temp);
    }

    // The rules is sending a String vehicleNumber as parent folder
    // element in list uploaded Async
    Client client = new Client("192.168.8.34", 59090, vehicleNumber);
    client.execute();
}
package com.tsurumaru.dzil.clientwarehouse.controllers;

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;

public class Client extends AsyncTask<Void, Void, Void> {

    private String address;
    private int port;
    private String textResponse;

    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;
    OutputStream os = null;
    DataOutputStream dos = null;
    Context context;
    ArrayList<String> list;

    public Client(Context context, String address, int port, ArrayList<String> list, String textResponse) {
        this.context = context;
        this.address = address;
        this.port = port;
        this.textResponse = textResponse;
        this.list = list;
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        Socket socket = null;

        try {
            socket = new Socket(address, port);

            //Execute them
            DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

            //write the number of files to the server
            dos.writeInt(list.size());
            System.out.println("Do In Background TO Server" + list.size());
            dos.flush();

            //write file names to the server
            for (String temp : list) {
                dos.writeUTF(temp);
                dos.flush();
            }

            // write file size to the server
            int n = 0;
            byte[]buf = new byte[4092];
            for (String temp : list) {
                File file = new File(temp);
                dos.writeLong(file.length());
                dos.flush();
            }

            // write the file
            for (String temp : list) {
                File file = new File(temp);
                FileInputStream fis = new FileInputStream(file);
                while ((n = fis.read(buf)) != -1) {
                    dos.write(buf, 0, n);
                    dos.flush();
                }
            }


            dos.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute() {
        Log.i("ThreadPost", "onPost");
    }
}
private void handlingUpload(字符串vehicleNumber){
System.out.println(“路径图像:+车辆编号”);
ArrayList=新建ArrayList();
//获取每个对象的所有路径并登录到列表中
用于(图像:图像){
添加(image.getPath());
}
//只要证明这个列表不是emtpty就行了
用于(字符串临时:列表){
System.out.println(“文件路径”+temp);
}
//规则正在将字符串vehicleNumber作为父文件夹发送
//异步上载列表中的元素
客户机=新客户机(“192.168.8.34”,59090,车辆编号);
client.execute();
}
类来处理Android套接字

private void handlingUpload(String vehicleNumber) {
    System.out.println("PathImage: " + vehicleNumber);
    ArrayList<String> list = new ArrayList<String>();

    // Get all path each object and sign into list
    for (Image image : images) {
        list.add(image.getPath());
    }

    // just prove if list is not emtpty
    for (String temp : list) {
        System.out.println("FilePath" + temp);
    }

    // The rules is sending a String vehicleNumber as parent folder
    // element in list uploaded Async
    Client client = new Client("192.168.8.34", 59090, vehicleNumber);
    client.execute();
}
package com.tsurumaru.dzil.clientwarehouse.controllers;

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;

public class Client extends AsyncTask<Void, Void, Void> {

    private String address;
    private int port;
    private String textResponse;

    FileInputStream fis = null;
    BufferedInputStream bis = null;
    DataInputStream dis = null;
    OutputStream os = null;
    DataOutputStream dos = null;
    Context context;
    ArrayList<String> list;

    public Client(Context context, String address, int port, ArrayList<String> list, String textResponse) {
        this.context = context;
        this.address = address;
        this.port = port;
        this.textResponse = textResponse;
        this.list = list;
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        Socket socket = null;

        try {
            socket = new Socket(address, port);

            //Execute them
            DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

            //write the number of files to the server
            dos.writeInt(list.size());
            System.out.println("Do In Background TO Server" + list.size());
            dos.flush();

            //write file names to the server
            for (String temp : list) {
                dos.writeUTF(temp);
                dos.flush();
            }

            // write file size to the server
            int n = 0;
            byte[]buf = new byte[4092];
            for (String temp : list) {
                File file = new File(temp);
                dos.writeLong(file.length());
                dos.flush();
            }

            // write the file
            for (String temp : list) {
                File file = new File(temp);
                FileInputStream fis = new FileInputStream(file);
                while ((n = fis.read(buf)) != -1) {
                    dos.write(buf, 0, n);
                    dos.flush();
                }
            }


            dos.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute() {
        Log.i("ThreadPost", "onPost");
    }
}
package com.tsurumaru.dzil.clientwarehouse.controllers;
导入android.content.ContentResolver;
导入android.content.Context;
导入android.net.Uri;
导入android.os.AsyncTask;
导入android.util.Log;
导入java.io.BufferedInputStream;
导入java.io.BufferedOutputStream;
导入java.io.DataInputStream;
导入java.io.DataOutputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.ObjectOutputStream;
导入java.io.OutputStream;
导入java.net.Socket;
导入java.util.ArrayList;
公共类客户端扩展异步任务{
私有字符串地址;
专用int端口;
私有字符串文本响应;
FileInputStream fis=null;
BufferedInputStream bis=null;
DataInputStream dis=null;
OutputStream os=null;
DataOutputStream dos=null;
语境;
数组列表;
公共客户端(上下文上下文、字符串地址、int端口、ArrayList列表、字符串textResponse){
this.context=上下文;
this.address=地址;
this.port=端口;
this.textResponse=textResponse;
this.list=列表;
}
@凌驾
受保护的Void doInBackground(Void…arg0){
套接字=空;
试一试{
套接字=新套接字(地址、端口);
//处决他们
DataOutputStream dos=新的DataOutputStream(新的BufferedOutputStream(socket.getOutputStream());
//将文件数写入服务器
dos.writeInt(list.size());
System.out.println(“后台到服务器的Do”+list.size());
dos.flush();
//将文件名写入服务器
用于(字符串临时:列表){
dos.writeUTF(临时);
dos.flush();
}
//将文件大小写入服务器
int n=0;
字节[]buf=新字节[4092];
用于(字符串临时:列表){
文件文件=新文件(临时文件);
writeLong(file.length());
dos.flush();
}
//写文件
用于(字符串临时:列表){
文件文件=新文件(临时文件);
FileInputStream fis=新的FileInputStream(文件);
而((n=fis.read(buf))!=-1){
dos.write(buf,0,n);
dos.flush();
}
}
dos.close();
}捕获(例外情况除外){
例如printStackTrace();
}
返回null;
}
受保护的void onPostExecute(){
Log.i(“螺纹柱”、“onPost”);
}
}
Java服务器

public class MainWindow extends javax.swing.JFrame {

    public static void main(String args[]) {

        // Create a Socket Server
        Thread starter = new Thread(new ServerStart());
        starter.start();

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainWindow().setVisible(true);
            }
        });
    }


    public class ServerStart implements Runnable {
        @Override
        public void run() {
            try {
                ServerSocket serverSocket = new ServerSocket(59090);
                while (true) {
                    Socket clientSocket = null;
                    clientSocket = serverSocket.accept();

                    // create each thread
                    Thread listener = new Thread(new ClientHandler(clientSocket));
                    listener.start();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public class ClientHandler implements Runnable {

        Socket socket;

        public ClientHandler(Socket clientSocket) {
            try {
                socket = clientSocket;
            } catch (Exception ex) {
                textAreaLog.append(ex.getMessage());
            }
        }

        @Override
        public void run() {
            try {

                DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

                //read the number of files from the client
                int number = dis.readInt();
                textAreaLog.append("Number of Files to be received: " + number + "\n");

                ArrayList<File> files = new ArrayList<File>(number);
                ArrayList<Long> size = new ArrayList<Long>(number);

                //read file names, add files to arraylist
                for (int i = 0; i < number; i++) {
                    File file = new File(dis.readUTF());
                    files.add(file);

                }

                long longSize;
                for (int i = 0; i < number; i++) {
                    longSize = dis.readLong();
                    size.add(longSize);
                }
                int n = 0;
                byte[] buf = new byte[4092];

                for (int i = 0; i < files.size(); i++) {

                    long fileSize = size.get(i);
                    textAreaLog.append("Receiving file: " + files.get(i).getPath() + " size: " + fileSize + "\n");

                    //read file
                    try (
                            //create a new fileoutputstream for each new file
                            FileOutputStream fileOutputStream = new FileOutputStream("D:\\Server Warehouse\\incoming_lokal\\" + files.get(i).getName())) {
                        //read file
                        while (fileSize > 0 && (n = dis.read(buf, 0, (int) Math.min(buf.length, fileSize))) != -1) {
                            fileOutputStream.write(buf, 0, n);
                            fileSize -= n;
                        }

                        fileOutputStream.close();
                    }
                }
            } catch (IOException ex) {
                textAreaLog.append(ex.getMessage());
            } finally {
                textAreaLog.append("Thread Is Done " + "\n");
            }
        }
    }
}
public类主窗口扩展了javax.swing.JFrame{
公共静态void main(字符串参数[]){
//创建套接字服务器
线程启动程序=新线程(newserverstart());
starter.start();
invokeLater(new Runnable()){
公开募捐{
新建主窗口().setVisible(true);
}
});
}
公共类ServerStart实现可运行{
@凌驾
公开募捐{
试一试{
ServerSocket ServerSocket=新的ServerSocket(59090);
while(true){
套接字clientSocket=null;
clientSocket=serverSocket.accept();
//创建每个线程
线程侦听器=新线程(新ClientHandler(clientSocket));
listener.start();
}
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
公共类ClientHandler实现Runnable{
插座;
公共客户端句柄(套接字客户端套接字){
试一试{
socket=clientSocket;
}捕获(例外情况除外){
textAreaLog.append(例如getMessage());
}
}
@凌驾
公开募捐{
试一试{
DataInputStream dis=新的DataInputStream(新的BufferedInputStream(socket.getInputStream());
//从客户端读取文件数
int number=dis.readInt();
textAreaLog.append(“要接收的文件数:“+Number+”\n”);
ArrayList文件=新的ArrayList(编号);
ArrayList size=新的ArrayList(编号);
//读取文件名,将文件添加到arraylist
for(int i=0;i