Java 套接字服务器正在下载包含但不包含特定文件的文件夹
我在这里遇到了一个问题,我在这里构建了这个套接字服务器来下载文件,它下载文件夹中的文件,但它不下载指定的文件名,返回null,你们能帮我在这个服务器中的什么地方进行更改吗Java 套接字服务器正在下载包含但不包含特定文件的文件夹,java,android,sockets,Java,Android,Sockets,我在这里遇到了一个问题,我在这里构建了这个套接字服务器来下载文件,它下载文件夹中的文件,但它不下载指定的文件名,返回null,你们能帮我在这个服务器中的什么地方进行更改吗 sOutput = new ObjectOutputStream(socket.getOutputStream()); sInput = new ObjectInputStream(socket.getInputStream()); // read the userna
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
// read the username
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
String Request = (String) sInput.readObject();
System.out.println("request is:"+Request);
String[] todoname=Request.split("\\@reza-hp");
String name=todoname[0];
System.out.println("Connecting...");
File filcheck = new File("D://Users//ProfileImages//"+name+"
//"+"ProfileImage,imagechange_1,"+name+",.jpg");
System.out.println(filcheck);
if (filcheck.exists()){
fil = new File("D://Users//ProfileImages//"+name+"
//"+"ProfileImage,imagechange_1,"+name+",.jpg");
}else{
fil = new File("D://Users//Default//");
}
System.out.println(fil);
File[] Files=fil.listFiles();
System.out.println(Files);
for (int count=0;count < Files.length;count ++){
System.out.println(Files[count].getName());
}
os = socket.getOutputStream();
dos = new DataOutputStream(os);
dos.writeInt(Files.length);
for (int count=0;count<Files.length;count ++){
dos.writeUTF(Files[count].getName());
}
for (int count=0;count<Files.length;count ++){
int filesize = (int) Files[count].length();
dos.writeInt(filesize);
}
for (int count=0;count<Files.length;count ++){
int filesize = (int) Files[count].length();
byte [] buffer = new byte [filesize];
FileInputStream fis = new FileInputStream(Files[count].toString());
BufferedInputStream bis = new BufferedInputStream(fis);
//Sending file name and file size to the server
bis.read(buffer, 0, buffer.length); //This line is important
dos.write(buffer, 0, buffer.length);
fis.close();
dos.flush();
//close socket connection
// socket.close();
}
// Toast.makeText(getApplicationContext(),"Transfer file is
completed!!", Toast.LENGTH_LONG).show();
dos.close();
os.close();
//socket.close();
sOutput=newObjectOutputStream(socket.getOutputStream());
sInput=newObjectInputStream(socket.getInputStream());
//读取用户名
OutputStream os=socket.getOutputStream();
DataOutputStream dos=新的DataOutputStream(os);
字符串请求=(字符串)sInput.readObject();
System.out.println(“请求为:”+请求);
字符串[]todoname=Request.split(“\\@reza hp”);
字符串名称=todoname[0];
System.out.println(“连接…”);
File filcheck=新文件(“D://Users//ProfileImages//”+name+”
//“+”ProfileImage,imagechange_1,“+name+”,.jpg”);
系统输出打印项次(filcheck);
if(filcheck.exists()){
fil=新文件(“D://Users//ProfileImages//”+name+”
//“+”ProfileImage,imagechange_1,“+name+”,.jpg”);
}否则{
fil=新文件(“D://Users//Default//”);
}
系统输出打印项次(fil);
File[]Files=fil.listFiles();
System.out.println(文件);
对于(int count=0;count 对于(int count=0;count很好地修复了它,我在这里发布了singe文件下载程序,问题是该文件包含从任何类型、图像、音乐、视频下载的内容,这两种方式都非常适合需要使用的人,祝你好运
String Type;
Socket socket;
ObjectInputStream sInput;
ObjectOutputStream sOutput;
// my unique id (easier for deconnection)
int id;
// Constructore
ClientThread(Socket socket) throws InterruptedException {
// a unique id
id = ++uniqueId;
this.socket = socket;
/* Creating both Data Stream */
System.out.println("Thread trying to create Object Input/Output
Streams");
while (!jobdone){
try
{
// create output first
sOutput = new ObjectOutputStream(socket.getOutputStream());
sInput = new ObjectInputStream(socket.getInputStream());
// read the username
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
String Request = (String) sInput.readObject();
System.out.println("request is:"+Request);
String[] todoname=Request.split("\\@reza-hp");
String name=todoname[0];
System.out.println("Connecting...");
File filcheck = new File("D://Users//ProfileImages//"+name+"
//"+"ProfileImage,imagechange_1,"+name+",.jpg");
System.out.println(filcheck);
if (filcheck.exists()){
fil = new File("D://Users//ProfileImages//"+name+"
//"+"ProfileImage,imagechange_1,"+name+",.jpg");
}else{
fil = new File("D://Users//Default//");
}
System.out.println(fil);
// File[] Files=fil.listFiles();
// for (int count=0;count < Files.length;count ++){
System.out.println(fil.getName());
// }
os = socket.getOutputStream();
dos = new DataOutputStream(os);
dos.writeInt(1);
// for (int count=0;count<Files.length;count ++){
dos.writeUTF(fil.getName());
// }
// for (int count=0;count<Files.length;count ++){
int filesize = (int) fil.length();
dos.writeInt(filesize);
// }
// for (int count=0;count<Files.length;count ++){
byte [] buffer = new byte [filesize];
FileInputStream fis = new FileInputStream(fil.toString());
BufferedInputStream bis = new BufferedInputStream(fis);
//Sending file name and file size to the server
bis.read(buffer, 0, buffer.length); //This line is important
dos.write(buffer, 0, buffer.length);
fis.close();
dos.flush();
//close socket connection
// socket.close();
// Toast.makeText(getApplicationContext(),"Transfer file is
completed!!", Toast.LENGTH_LONG).show();
dos.close();
os.close();
//socket.close();
}
字符串类型;
插座;
目标输入流输入;
ObjectOutputStream sOutput;
//我的唯一id(便于断开连接)
int-id;
//建设者
ClientThread(套接字)抛出InterruptedException{
//唯一的id
id=++唯一id;
this.socket=socket;
/*创建两个数据流*/
System.out.println(“试图创建对象输入/输出的线程
溪流);
而(!作业完成){
尝试
{
//首先创建输出
sOutput=newObjectOutputStream(socket.getOutputStream());
sInput=newObjectInputStream(socket.getInputStream());
//读取用户名
OutputStream os=socket.getOutputStream();
DataOutputStream dos=新的DataOutputStream(os);
字符串请求=(字符串)sInput.readObject();
System.out.println(“请求为:”+请求);
字符串[]todoname=Request.split(“\\@reza hp”);
字符串名称=todoname[0];
System.out.println(“连接…”);
File filcheck=新文件(“D://Users//ProfileImages//”+name+”
//“+”ProfileImage,imagechange_1,“+name+”,.jpg”);
系统输出打印项次(filcheck);
if(filcheck.exists()){
fil=新文件(“D://Users//ProfileImages//”+name+”
//“+”ProfileImage,imagechange_1,“+name+”,.jpg”);
}否则{
fil=新文件(“D://Users//Default//”);
}
系统输出打印项次(fil);
//File[]Files=fil.listFiles();
//对于(int count=0;count