将通过 DatagramPackets 接收的文件保存在 Java 中
Saving files received through DatagramPackets in Java
我正在尝试使用 Java 中的 DatagramPackets 以片段形式发送文件(分配的一部分。)当我试图保存传入的文件时,我收到访问被拒绝的错误,但我相信它不是权限问题。
这是首当其冲的:
我让发送文件的用户使用 FileChooser 选择它。并创建一个新的 Message 对象。
//....
File f = content.showFileChooser();
byte type = Byte.parseByte("4");
Message m;
try {
if (mode == 1){
m = new Message(f, content.getServerData().getFragmentSize(), (short) (sentMessages.size()+1), type);
serverThread.send(m);
}
//...
在消息创建过程中,文件被分成字节数组,其中每个数组的大小由用户预先确定。代码很长,所以我不打算 post 切碎过程,但这就是我将 File 对象转换成一个大字节 [] 然后被切碎的方法
Path p = Paths.get(file.getAbsolutePath());
this.rawData = Files.readAllBytes(p);
创建消息并将其分割成字节数组后,我使用 DatagramPackets 发送它们。另一方然后使用这些来创建一个新的 Message 对象。一旦所有片段都到达,rawData 将再次从 Message 对象中提取。相信问题出在这里:
Message m = receivedMessages.get(msgIndex-1);
byte[] fileData = m.getFile();
if (fileData != null){
System.out.println("All file fragments received.");
content.append("Received a file in" + m.getFragmentCount()+" fragments. Choose directory. " ,1);
//I BELIEVE THIS TO BE THE CRITICAL POINT
String filePath = content.chooseDirectory();
if (filePath == null)
return;
FileOutputStream fos;
try {
fos = new FileOutputStream(filePath);
fos.write(fileData);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
一旦所有片段都到达,我让用户select 使用具有DIRECTORY_ONLY 选择模式的FileChooser 的目录。据我了解,FileOutputStream 需要新文件的完整路径。我必须单独发送文件名和扩展名还是可以从收到的文件数据中提取?
您正在写入 filePath
的目录路径,然后尝试使用 FileOutputStream
打开 那个目录 。难怪不行,你也必须指定文件名。
String filename = "myfile.txt"; //Or receive and set this some other way
File outFile = new File(filePath, filename);
fos = new FileOutputStream(outFile);
不过,我在任何地方都没有看到您 sending/receiving 文件名。您需要使其保持不变或将其与文件内容一起传输。
我正在尝试使用 Java 中的 DatagramPackets 以片段形式发送文件(分配的一部分。)当我试图保存传入的文件时,我收到访问被拒绝的错误,但我相信它不是权限问题。 这是首当其冲的:
我让发送文件的用户使用 FileChooser 选择它。并创建一个新的 Message 对象。
//....
File f = content.showFileChooser();
byte type = Byte.parseByte("4");
Message m;
try {
if (mode == 1){
m = new Message(f, content.getServerData().getFragmentSize(), (short) (sentMessages.size()+1), type);
serverThread.send(m);
}
//...
在消息创建过程中,文件被分成字节数组,其中每个数组的大小由用户预先确定。代码很长,所以我不打算 post 切碎过程,但这就是我将 File 对象转换成一个大字节 [] 然后被切碎的方法
Path p = Paths.get(file.getAbsolutePath());
this.rawData = Files.readAllBytes(p);
创建消息并将其分割成字节数组后,我使用 DatagramPackets 发送它们。另一方然后使用这些来创建一个新的 Message 对象。一旦所有片段都到达,rawData 将再次从 Message 对象中提取。相信问题出在这里:
Message m = receivedMessages.get(msgIndex-1);
byte[] fileData = m.getFile();
if (fileData != null){
System.out.println("All file fragments received.");
content.append("Received a file in" + m.getFragmentCount()+" fragments. Choose directory. " ,1);
//I BELIEVE THIS TO BE THE CRITICAL POINT
String filePath = content.chooseDirectory();
if (filePath == null)
return;
FileOutputStream fos;
try {
fos = new FileOutputStream(filePath);
fos.write(fileData);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
一旦所有片段都到达,我让用户select 使用具有DIRECTORY_ONLY 选择模式的FileChooser 的目录。据我了解,FileOutputStream 需要新文件的完整路径。我必须单独发送文件名和扩展名还是可以从收到的文件数据中提取?
您正在写入 filePath
的目录路径,然后尝试使用 FileOutputStream
打开 那个目录 。难怪不行,你也必须指定文件名。
String filename = "myfile.txt"; //Or receive and set this some other way
File outFile = new File(filePath, filename);
fos = new FileOutputStream(outFile);
不过,我在任何地方都没有看到您 sending/receiving 文件名。您需要使其保持不变或将其与文件内容一起传输。