我从客户那里收到一个XML
文件。我有另一个文件,其中包含Base-64
我嵌入在 XML 文件中的一个元素中的编码数据。完成所有这些合并后,我需要将文件的内容作为对象string
或DOM
对象返回,返回 asInputStream
将不起作用。
但是生成的合并文件null character
最后会在文件被处理为XML
. 我怎样才能摆脱它。这就是我合并文件的方式。
public Object merge(List<File> files) throws Exception {
System.out.println("merge with arguments is called");
if(files == null || files.isEmpty() || files.size()<2){
throw new IllegalArgumentException("File list cannot be null/empty and minimum 2 files are expected");
}
File imageFile = getImageFile(files);
File indexFile = getIndexFile(files);
File inProcessFile = new File("temp/" + indexFile.getName().replaceFirst("[.][^.]+$", "") + ".xml");
File base64EncodedFile = toBase64(imageFile);
/* Write from index file everything till attachment data to inProcess file*/
Scanner scanner = new Scanner(indexFile).useDelimiter("\\s*<AttachmentData>\\s*");
FileWriter writer = new FileWriter(inProcessFile);
writer.append(scanner.next());
/* Write <AttachmentData> element into inProcess file */
writer.append("<AttachmentData>");
/* Write base64 encoded image data into inProcess file */
IOUtils.copy(new FileInputStream(base64EncodedFile), writer);
/* Write all data from </AttachmentData> element from index file into inProcess file */
String fileAsString = IOUtils.toString(new BufferedInputStream(new FileInputStream(indexFile)));
String afterAttachmentData = fileAsString.substring(fileAsString.indexOf("</AttachmentData>"));
InputStream input = IOUtils.toInputStream(afterAttachmentData);
IOUtils.copy(input, writer);
/* Flush the file, processing completed */
writer.flush();
writer.close();
System.out.println("Process completed");
}
private File getIndexFile(List<File> files) {
for(File file:files){
String extension = FilenameUtils.getExtension(file.getName());
if(extension.equalsIgnoreCase(IDX_FILE_EXT))
return file;
}
throw new IllegalArgumentException("Index file doesn't exist or cannot be read.");
}
private File getImageFile(List<File> files) {
for(File file:files){
String extension = FilenameUtils.getExtension(file.getName());
if(extension.equalsIgnoreCase(IMG_FILE_EXT))
return file;
}
throw new IllegalArgumentException("Image file doesn't exist or cannot be read.");
}
private File toBase64(File imageFile) throws Exception {
System.out.println("toBase64 is called");
Base64InputStream in = new Base64InputStream(new FileInputStream(imageFile), true);
File f = new File("/temp/" + imageFile.getName().replaceFirst("[.][^.]+$", "") + ".txt");
Writer out = new FileWriter(f);
IOUtils.copy(in, out);
return f;
}
请帮助我了解如何修复产生空字符的代码