java套接字文件传输输出路径
我正在从事一个java项目,该项目在服务器和客户端之间传输文件,我已成功地将文件发送到所需的输出位置,但唯一的问题是我必须在输出路径中包含完整的文件名才能成功保存。我的程序是这样运行的: 首先,它获取要作为输入传输到控制台的文件的路径,然后获取输出路径,再次作为输入传输到控制台 以下是相应文件名导入和导出的代码(我认为问题出在这里的某个地方,发布此部分就足够了) 服务器端java套接字文件传输输出路径,java,sockets,file-transfer,Java,Sockets,File Transfer,我正在从事一个java项目,该项目在服务器和客户端之间传输文件,我已成功地将文件发送到所需的输出位置,但唯一的问题是我必须在输出路径中包含完整的文件名才能成功保存。我的程序是这样运行的: 首先,它获取要作为输入传输到控制台的文件的路径,然后获取输出路径,再次作为输入传输到控制台 以下是相应文件名导入和导出的代码(我认为问题出在这里的某个地方,发布此部分就足够了) 服务器端 .... String in_filePath = null; System.out.print("enter the fi
....
String in_filePath = null;
System.out.print("enter the file name: ");
in_filePath = sc.nextLine();
File myFile = new File( in_filePath );
System.out.println("The file chosen is being sent...");
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(myFile);
sc.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
.....
int bufferSize = clientSocket.getReceiveBufferSize();
is = clientSocket.getInputStream();
DataInputStream clientdata = new DataInputStream(is);
String fileName = clientdata.readUTF();
System.out.println("file to be transferred is: " + fileName );
System.out.print("file output path: ");
String out_filePath;
out_filePath = sc.nextLine();
File file = new File( out_filePath );
fos = new FileOutputStream( file );
bos = new BufferedOutputStream(fos);
bytesRead = is.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesRead = is.read(aByte);
} while (bytesRead != -1);
bos.write(baos.toByteArray());
System.out.println(fileName + " transferred successfully");
客户端
....
String in_filePath = null;
System.out.print("enter the file name: ");
in_filePath = sc.nextLine();
File myFile = new File( in_filePath );
System.out.println("The file chosen is being sent...");
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(myFile);
sc.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
.....
int bufferSize = clientSocket.getReceiveBufferSize();
is = clientSocket.getInputStream();
DataInputStream clientdata = new DataInputStream(is);
String fileName = clientdata.readUTF();
System.out.println("file to be transferred is: " + fileName );
System.out.print("file output path: ");
String out_filePath;
out_filePath = sc.nextLine();
File file = new File( out_filePath );
fos = new FileOutputStream( file );
bos = new BufferedOutputStream(fos);
bytesRead = is.read(aByte, 0, aByte.length);
do {
baos.write(aByte);
bytesRead = is.read(aByte);
} while (bytesRead != -1);
bos.write(baos.toByteArray());
System.out.println(fileName + " transferred successfully");
起初,我的程序中没有包含输出路径;正如预期的那样,输出路径是根项目文件夹,它工作得很好,因为它读取文件名并以相同的名称发送文件,没有任何问题。但是当我实现输出路径查询时,我选择的输出路径如“C:\”或“C:\blabla\”会给我一个异常,正如我前面所说的。此外,将输出路径指定为“C:\image.jpg”或“blabla\image.jpg”效果很好(假设要复制的文件名为image.jpg),读取文件名会有问题吗?任何帮助都将不胜感激
编辑:现在,如果我将“c:\”(或类似的任何类型的路径)作为输出路径,我会收到一个套接字写入错误,但是如果输出路径是“c:\image.jpg”假设您正在目录“c:\”上执行程序,并且您的文件“image.jpg”位于“c:\images\image.jpg”上,那么它仍然可以正常工作。 如果您以“image.jpg”的形式提供输入,它将无法工作。您必须提供“images\image.jpg” 通过在服务器端代码中添加以下行,您可以轻松检查是否已命中实际文件:
System.out.println(myFile.isFile());
异常发生在哪一行?在服务器还是客户端?标题是输出路径,但摘要表明它可能在客户端?我添加stacktraces是为了查看它,但现在得到了一个不同的错误,如果我将输出路径指定为c:\则服务器端会出现套接字写入错误,而且如果我指定完整的文件名,它也会正常工作。我没有尝试在某个文件夹中直接搜索image.jpg,我将完整的路径作为输入。问题不在输入部分,而是在输出部分,正如msdo所示,您的意思是您为服务器和客户端都提供了完整的路径作为输入?对于服务器端,可以提供像“images\image.jpg”这样的相对路径,但客户端代码可能希望相对路径没有子目录(如“image.jpg”)或完整路径(如“C:\images\image.jpg”)。我不知道客户端希望保存“image.jpg”,我觉得这是出乎意料的,事实上我对它了解不多,我觉得只显示一些路径,比如c:\images\应该就足够了(比如保存从internet下载的文件)