Java:如何在一个zip文件中的多个xml文件上执行dos2unix?
我有一个表单可以上传一个zip文件,然后将zip中的所有xml文件转换为unix格式(如果它们是dos格式)。现在我以InputStream的形式接收输入。如何处理inputstream中的文件并对其执行(dos2unix)以将其转换为unix格式 我尝试将流转换为文件,然后再转换它们,但没有成功Java:如何在一个zip文件中的多个xml文件上执行dos2unix?,java,inputstream,dos2unix,Java,Inputstream,Dos2unix,我有一个表单可以上传一个zip文件,然后将zip中的所有xml文件转换为unix格式(如果它们是dos格式)。现在我以InputStream的形式接收输入。如何处理inputstream中的文件并对其执行(dos2unix)以将其转换为unix格式 我尝试将流转换为文件,然后再转换它们,但没有成功 public void uploadFile(UploadAuditConfig transaction,String fileType, InputStream in, String delimit
public void uploadFile(UploadAuditConfig transaction,String fileType, InputStream in, String delimiter) {
ZipInputStream zipInputStream = new ZipInputStream(in);
ZipEntry entry = null;
do{
entry = zipInputStream.getNextEntry();
//need to convert this entry to unix format if it is dos before I pass it to processFile method
if(entry != null && !entry.isDirectory()) {
List<Map<String,String>> list =processFile(zipInputStream, delimiter);
zipInputStream.closeEntry();
}
}while(entry!=null);
}
public List<Map<String, String>> processFile(InputStream in, String
delimiter){
List<Map<String,String>> acesList = new ArrayList<>();
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new InputStreamReader(in));
while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
File file = new File("/tmp/" + "out" + i + ".xml");
FileWriter fw = new FileWriter(file);
if (!file.exists())
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file, true);
t.transform(new StAXSource(xsr), new StreamResult(fos));
fos.close();
if (i == 0) {
JSONObject xmlJSONObjHeader = XML.toJSONObject(content);
Object o = JsonPath.parse(xmlJSONObjHeader.toString()).read("$['Header']['BrandAAIAID']");
brandaaiaid = String.valueOf(o);
logger.info("Brand id: " + brandaaiaid);
file.delete();
fw.close();
i++;
}
}
return acesList;
}
public void uploadFile(UploadAuditConfig事务,字符串文件类型,输入流输入,字符串分隔符){
ZipInputStream ZipInputStream=新的ZipInputStream(in);
ZipEntry条目=null;
做{
entry=zipInputStream.getNextEntry();
//如果是dos,则需要将此项转换为unix格式,然后再将其传递给processFile方法
if(entry!=null&&!entry.isDirectory()){
List List=processFile(zipInputStream,分隔符);
zipInputStream.closeEntry();
}
}while(条目!=null);
}
公共列表进程文件(InputStream in,字符串
分隔符){
List acesList=new ArrayList();
XMLInputFactory xif=XMLInputFactory.newInstance();
XMLStreamReader xsr=xif.createXMLStreamReader(新的InputStreamReader(in));
while(xsr.nextTag()==XMLStreamConstants.START_元素){
File File=新文件(“/tmp/”+“out”+i+“.xml”);
FileWriter fw=新的FileWriter(文件);
如果(!file.exists())
createNewFile();
FileOutputStream fos=新的FileOutputStream(文件,true);
t、 转换(新StatxSource(xsr)、新StreamResult(fos));
fos.close();
如果(i==0){
JSONObject xmlJSONObjHeader=XML.toJSONObject(内容);
对象o=JsonPath.parse(xmlJSONObjHeader.toString()).read(“$['Header']['brandaiaid']”);
brandaaiid=String.valueOf(o);
logger.info(“品牌标识:+brandaiaid”);
delete();
fw.close();
i++;
}
}
返回acesList;
}
预期:来自inputstream的Unix格式文件我能够将inputstream转换为文件,然后使用dos2unix将文件转换为Unix,然后再次将文件作为inputstream传递
OutputStream outputStream = null;
File file1 = new File("/tmp/output.txt");
try
{
outputStream = new FileOutputStream(file1);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = in1.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally
{
if(outputStream != null)
{
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ExecuteShell.executeJavaCommand(file1.getAbsolutePath());
logger.info("action=output.txt converted from dos to unix");
InputStream in = null;
try {
in = new FileInputStream(file1);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
你应该如何处理转换的结果?这是完整的代码吗
processFile()
的返回类型为List
,但没有return
语句。为什么?XML不在乎,因此它的任何标准工具或API也不在乎,包括您正在使用的工具或API。你不需要这么做,怎么会失败?这才是真正的问题。你拿锤子敲坚果。这里是否需要XMLStreamReader
还不清楚。并清除exists()/createNewFile()
内容。这完全是浪费时间和空间,而且你在错误的地方做这件事。如果它工作,你将得到空文件。我只能重复。XML不在乎。您引用的错误是因为您已经使用了流的第一个元素,因此SAX解析器无法解析流的其余部分。它与DOS2UNIX没有任何关系。您的代码是错误的,正如我已经说过的,这里根本不需要XMLStreamReader
。只需从zip输入流创建一个StreamSource
。我敦促您研究问题的根本原因,因为XML通常不关心行尾。你作为答案发布的代码太麻烦了。它吞噬例外。它不是线程安全的(可能是也可能不是问题,但如果是,请享受调试的乐趣)。写入和读取临时文件以及转换行尾的额外过程过于复杂。它依赖于外部工具的存在。您的答案也不是一个完全可重用的示例,因此其他人不太可能从中受益;我发现了这个问题,所以基本上,当我尝试使用xmlStreamReader读取标记时,如果文件是dos格式,它就会失败。当我使用上面的代码将inputstream转换为unix格式时,它修复了这个问题。因此,我发布了解决方案,因为我认为它会帮助面临同样问题的人。这不是一个解决方案,这是一个快速而肮脏的黑客,一个变通办法。你没有发现真正的问题。阅读你问题下的评论。我不允许你签入代码。“不过这取决于你,”罗伯特,“我确实发现了真正的问题。该文件具有BOM表格式,转换后删除了前三个字节,并有助于读取标记。