Java 如何将两个AFP文件连接在一起
我有两个文件,我想把它们连接在一起,我如何才能做到这一点。我使用BufferedInputStream和BufferedOutputStream编写了java代码来连接它们,结果AFP的格式不正确。我甚至尝试使用LinuxCAT,但却得到了同样错误的结果。请帮忙。我不认为问题出在我的java代码上,但我在下面发布代码以防万一 注意:一件奇怪的事情是,如果我切换串联的顺序,那么它会产生正确的格式输出。例如,如果先连接A.afp,然后连接B.afp,则输出会出错,但如果先连接B.afp,然后连接A.afp,则会生成正确的格式结果。但是我需要A.afp出现在B.afp之前Java 如何将两个AFP文件连接在一起,java,concatenation,afp,Java,Concatenation,Afp,我有两个文件,我想把它们连接在一起,我如何才能做到这一点。我使用BufferedInputStream和BufferedOutputStream编写了java代码来连接它们,结果AFP的格式不正确。我甚至尝试使用LinuxCAT,但却得到了同样错误的结果。请帮忙。我不认为问题出在我的java代码上,但我在下面发布代码以防万一 注意:一件奇怪的事情是,如果我切换串联的顺序,那么它会产生正确的格式输出。例如,如果先连接A.afp,然后连接B.afp,则输出会出错,但如果先连接B.afp,然后连接A.
public static void main(String[] args) {
String filePath1 = "C:\\dev\\harry\\ETCC_data\\3199_FI_20_20110901143009.afp";
String filePath2 = "C:\\dev\\harry\\ETCC_data\\3643_FI_49_20110901143006.afp";
ConcatenateMain cm = new ConcatenateMain();
cm.concate(filePath1, filePath2);
}
private void concate(String filePath1, String filePath2){
BufferedInputStream bis1 = null;
BufferedInputStream bis2 = null;
FileInputStream inputStream1 = null;
FileInputStream inputStream2 = null;
FileOutputStream outputStream = null;
BufferedOutputStream output = null;
try{
inputStream1 = new FileInputStream(filePath1);
inputStream2 = new FileInputStream(filePath2);
bis1 = new BufferedInputStream(inputStream1);
bis2 = new BufferedInputStream(inputStream2);
List<BufferedInputStream> inputStreams = new ArrayList<BufferedInputStream>();
inputStreams.add(bis1);
inputStreams.add(bis2);
outputStream = new FileOutputStream("C:\\dev\\harry\\ETCC_data\\output.afp");
output = new BufferedOutputStream(outputStream);
byte [] buffer = new byte[BUFFER_SIZE];
for(BufferedInputStream input : inputStreams){
try{
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) != -1)
{
output.write(buffer, 0, bytesRead);
}
}finally{
input.close();
}
}
}catch(IOException e){
}finally{
try {
output.close();
} catch (IOException e) {
}
}
}
publicstaticvoidmain(字符串[]args){
String filePath1=“C:\\dev\\harry\\ETCC\u data\\3199\u FI\u 20\u 20110901143009.afp”;
String filePath2=“C:\\dev\\harry\\ETCC\u data\\3643\u FI\u 49\u 20110901143006.afp”;
ConcatenateMain cm=新的ConcatenateMain();
cm.concate(文件路径1、文件路径2);
}
私有void concate(字符串文件路径1、字符串文件路径2){
BufferedInputStream bis1=null;
BufferedInputStream bis2=null;
FileInputStream inputStream1=null;
FileInputStream inputStream2=null;
FileOutputStream outputStream=null;
BufferedOutputStream输出=null;
试一试{
inputStream1=新文件InputStream(文件路径1);
inputStream2=新文件InputStream(文件路径2);
bis1=新的BufferedInputStream(inputStream1);
bis2=新的BufferedInputStream(inputStream2);
List inputStreams=new ArrayList();
inputStreams.add(bis1);
inputStreams.add(bis2);
outputStream=新文件outputStream(“C:\\dev\\harry\\ETCC\u data\\output.afp”);
输出=新的BufferedOutputStream(outputStream);
字节[]缓冲区=新字节[缓冲区大小];
for(BufferedInputStream输入:inputStreams){
试一试{
int字节读取=0;
while((bytesRead=input.read(buffer,0,buffer.length))!=-1)
{
输出写入(缓冲区,0,字节读取);
}
}最后{
input.close();
}
}
}捕获(IOE异常){
}最后{
试一试{
output.close();
}捕获(IOE异常){
}
}
}
从example depot,这里有一个从文件中获取字节的快速函数:
public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}
默认情况下,Xenos D2e软件生成的AFP在页面顶部包含内联资源,如下所示
AFP file 1 resources AND AFP file 2 resources
AFP file 1 content AFP file 2 content
但是,当我尝试将这两个文件连接在一起时,一些资源将位于连接文件的中间,因此会弄乱结果
AFP file 1 resources
AFP file 1 content
AFP file 2 resources ------> resources should not be in the middle page
AFP file 2 content
因此,解决方案是将所有资源导出到一个外部文件,然后可以按如下方式连接
AFP file resources
AFP file 1 content
AFP file 2 content
这将解决这个问题。根据上面关于重新排序文件内容的答案,这里是我在DST Output(一家非常大的打印和邮件公司)工作时建议使用的工具
我制作了一个名为“afp_dd”的实用程序,它的工作原理与unix“dd”命令类似,允许我在命令行上指定记录跳过和计数值,以提取打破记录边界的子文件(标准的dd程序要求固定大小的记录,而不是在每个记录的开始处带有长度指示器的可变大小的记录)。我可以通过我们的AFP转储程序来检查子文件,然后使用输出子文件作为输入来创建修改过的文件。如果您查看我的代码,它确实读入
字节[]
,然后写出字节[]
,这与您的代码所做的非常相似。谢谢。如何获取afp文件的文件计数?这样我们可以在合并两个文件后进行验证。
AFP file resources
AFP file 1 content
AFP file 2 content