如何解决如何将下载的 mp4 文件附加合并到一个 mp4 文件中?
我正在尝试附加两个或更多我从服务器检索(下载)的 mp4 文件。问题是最后下载的 mp4 文件覆盖了所有继续下载的 mp4 文件。
这是我的代码:
public class Main {
static Socket socket;
static PrintWriter out;
static List<String> filenamesForDownloads = new ArrayList<>();
static List<String> deviceResponsesForDownloads = new ArrayList<>();
static atomicreference<FileOutputStream> fileOutputStream = new atomicreference<>();
static File filetoSave;
static AtomicInteger count = new AtomicInteger(1);
public static void main(String... args) {
filenamesForDownloads.add("2021_07_04_02_20_14_F_47.mp4");
filenamesForDownloads.add("2021_07_04_13_15_43_F_47.mp4");
if (establishSocketConnection()) {
ExecutorService executorServiceRead = Executors.newFixedThreadPool(1);
executorServiceRead.execute(readFromSocket());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printstacktrace();
}
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.execute(() -> {
String strAppDirectoryPath = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
filetoSave = new File(strAppDirectoryPath,"my_video.mp4");
if (filetoSave.exists()) {
filetoSave.delete();
}
try {
filetoSave.createNewFile();
} catch (IOException e) {
e.printstacktrace();
}
try {
fileOutputStream.set(new FileOutputStream(filetoSave,true));
} catch (FileNotFoundException e) {
e.printstacktrace();
}
for (String file : filenamesForDownloads) {
if (socket != null && socket.isConnected()) {
out.println("FRequiest," + file);
}
}
});
} else {
System.out.println("not connected");
}
}
private static Boolean establishSocketConnection() {
try {
socket = new Socket();
socket.connect(new InetSocketAddress("192.168.43.1",5050),8000);
out = new PrintWriter(new OutputStreamWriter(socket.getoutputStream()),true);
return true;
} catch (IOException e) {
// Todo: Add logging
return false;
}
}
private static Runnable readFromSocket() {
return () -> {
String response;
try {
BufferedReader inputStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((response = inputStream.readLine()) != null) {
if (response.startsWith("FReceive")) {
downloadAllFiles(response);
}
}
} catch (IOException e) {
e.printstacktrace();
}
};
}
private static void downloadAllFiles(String response) {
System.out.println(response);
String filename = "";
int fileSize = 0;
String dataServerIp = "";
int dataServerPort = 3001;
String[] fileParams;
int bufferLength;
Socket socket = null;
String[] serverIPs;
InetAddress serveraddr = null;
InputStream inputstram = null;
fileParams = response.split(",");
if (fileParams.length >= 6) {
filename = fileParams[4];
fileSize = Integer.valueOf(fileParams[5]);
dataServerIp = fileParams[2];
dataServerPort = Integer.valueOf(fileParams[3]);
}
serverIPs = dataServerIp.split(";");
try {
serveraddr = InetAddress.getByName(serverIPs[0]);
socket = new Socket(serveraddr,dataServerPort);
inputstram = socket.getInputStream();
byte[] buffer = new byte[1024];
while ((bufferLength = inputstram.read(buffer,buffer.length)) > 0) {
fileOutputStream.get().write(buffer,bufferLength);
}
System.out.println(count.get());
if(count.get() == filenamesForDownloads.size()) {
fileOutputStream.get().close();
System.out.println("finish all");
}
count.set(count.get() + 1);
socket.close();
} catch (Exception exc) {
//Todo: return downloadStatus
System.out.println(exc.getMessage());
}
}
}
fileOutputStream.set(new FileOutputStream(filetoSave,true));
应该允许将字节附加到文件中,但在我的情况下,它总是一遍又一遍地覆盖 mp4。如何将实际追加应用于文件?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。