2013-08-27 46 views
-1

我收到来自客户端的XML文件。我有另一个文件,其中包含Base-64编码的数据,我将其嵌入到XML文件中的一个元素中。完成所有这些合并后,我需要将文件的内容返回为stringDOM对象,返回InputStream将不起作用。如何从java中的文件内容中删除空字符

但是最终生成的合并文件末尾有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; 
    } 

请帮助我明白,我怎么能解决我的代码产生空字符

回答

3

修复产生,也许通过去除部分或全部的它的代码。要知道这一点,您应该问自己以下问题:

  1. 从客户端接收到的原始XML文件中是否存在空字符?
  2. 在XML文档的哪个位置,包含base-64数据的元素出现?
  3. XML文档在什么位置显示空字符?
  4. 您是否以任何形式解码base-64文件?
  5. base-64文件是否包含空字符?
  6. 如果是,为什么?
  7. 使用什么方法将base-64编码的数据“合并”到XML文档中?

按照后来由OP产生的信息,并且如果该文件总是包含空字符,最简单的解决办法是更换线:

String afterAttachmentData = fileAsString.substring(fileAsString.indexOf("</AttachmentData>")); 

String afterAttachmentData = fileAsString.substring(fileAsString.indexOf("</AttachmentData>"),fileAsString.length()-1); 

然而,从长远来看,最好是检查客户端是否在其末尾生成了空字符,如果是,则建议他们纠正生成它的代码o XML文档是有效的。

+1

+1,*删除/修复*会更好。 –

+0

请参阅我的问题的更新。我添加了代码 –

+0

@MartijnCourteaux感谢您的建议。 –