问题描述
我是android开发的初学者,我对如何修改我的代码以通过套接字发送 多个文件 有疑问,使用我当前的代码我只能发送一次一个文件,我有一个ArrayList,它是从其他活动中获得的,其中包含要发送的文件的完整路径,但是当我向数组中添加多个文件时,它不会发送任何内容。整整一个月困扰着我 任何帮助或建议将不胜感激
这是我的发件人代码
@Override
public void run() {
Intent intent = getIntent();
Bundle bundle = intent.getExtras(); //Receiving Intent from other Activity
ArrayList send = (ArrayList) bundle.getParcelableArrayList("send"); //Full path of file in ArrayList
ArrayList name = (ArrayList) bundle.getParcelableArrayList("name"); //Name of the file if Available
String con = send.toString();
String convert = con.replaceAll("\\[","").replaceAll("\\]","");
file = new File(convert);
byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(file));
DataInputStream dis = new DataInputStream(bis);
OutputStream os = socket.getoutputStream();
DataOutputStream dos = new DataOutputStream(os);
if (name == null){
dos.writeUTF(file.getName());
}else {
String conname = name.toString();
String convertname = conname.replaceAll("\\[","");
dos.writeUTF(convertname + ".apk");
}
int read;
while ((read = dis.read(bytes)) != -1){
dos.write(bytes,read);
}
socket.close();
}
这是接收者的
@Override
public void run() {
Socket socket = null;
int bytesRead;
InputStream in;
int bufferSize = 0;
try {
socket = new Socket(dstAddress,dstPort);
bufferSize = socket.getReceiveBufferSize();
in = socket.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileName = clientData.readUTF();
File file = new File(Environment.getExternalStorageDirectory() + fileName); //Name and place of file to be save
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[bufferSize];
int read;
while ((read = clientData.read(buffer)) != -1) {
output.write(buffer,read);
}
socket.close();
}
新发件人的代码
public void run() {
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
ArrayList send = (ArrayList) bundle.getParcelableArrayList("send");
ArrayList name = (ArrayList) bundle.getParcelableArrayList("name");
BufferedInputStream bis;
byte[] bytes = new byte[8192];
try {
//bis = new BufferedInputStream(new FileInputStream(file));
OutputStream os = socket.getoutputStream();
DataOutputStream dos = new DataOutputStream(os);
for (int i =0; i<send.size();i++){
String filePath = send.get(i).toString();
File file = new File(filePath);
if (name!=null){ //check if name Array is empty or not
dos.writeUTF(String.valueOf(name.get(i)));
}else {
dos.writeUTF(file.getName());
}
dos.writeInt(file.size());
};
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(new BufferedInputStream(fis));
int read;
while ((read = dis.read(bytes)) != -1){
dos.write(bytes,read);
}
fis.close();
dos.flush();
os.flush();
socket.close();
新接收方的代码
try {
socket = new Socket(dstAddress,dstPort);
bufferSize = socket.getReceiveBufferSize();
in = socket.getInputStream();
DataInputStream clientData = new DataInputStream(in);
String fileName = clientData.readUTF();
int filelength = clientData.readInt();
File file = new File(Environment.getExternalStorageDirectory() + fileName); //Name and place of file to be save
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[8192];
int read;
while ((read = clientData.read(buffer)) != -1) {
output.write(buffer,read);
}
socket.close();
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)