我有一个创建 xml 的代码。
public void createXML(InputStream in, String fileName) throws IOException {
baos = new ByteArrayOutputStream();
byte[] buf = new byte[BUF_SIZE];
int readNum = 0;
Writer writer = new BufferedWriter(new OutputStreamWriter(FileUtil.getOutputStream(fileName, FileUtil.HDD)));
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
writer.write("\t<" + imageFileName + ">\r\n");
writer.write("\t\t");
try {
while ((readNum = in.read(buf)) >= 0) {
baos.write(buf, 0, readNum);
writer.write(baos.toString());
baos.reset();
}
}
finally {
if (in != null) {
in.close();
baos.close();
}
}
writer.write("\r\n\t<" + imageFileName + ">");
writer.close();
baos = null;
buf = null;
}
我想将此 xml 创建为多个部分(每个部分最多 500kb)。我怎样才能做到这一点?我有什么办法可以确定创建的文件已经是 500kb 并将剩余的数据写入另一个文件?
我使用了这个,但是在解码 base64 字符串后的图像,生成的图像在它被剪切的部分被损坏。
for(int i = 1; i <= numberOfFiles; i++){
baos = new ByteArrayOutputStream();
String filePartName = fileName + ".part" + i + ".xml";
Writer writer = new BufferedWriter(new OutputStreamWriter(FileUtil.getOutputStream(filePartName, FileUtil.HDD)));
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
writer.write("\t<" + filePartName + ">\r\n");
writer.write("\t\t");
int size = 0;
while ((readNum = in.read(buf)) >= 0 && size < 512000) {
baos.write(buf, 0, readNum);
size = size + readNum;
writer.write(baos.toString());
baos.reset();
}
writer.write("\r\n\t<" + filePartName + ">");
writer.close();
baos.close();
}
}
in.close();