XMLStreamException while trying to parse an xml file downloaded from a ftp - java

I am downloading a file from and ftp, saving it onto my local filesystem and then reading it with createXMLStreamReader. When I try and parse it I get this error:javax.xml.stream.XMLStreamException: ParseError at [row,col]:[124,316]. When I copy and paste it to another file manually everything works fine. I have tried copying the file using this, the file gets copied but I am still getting the same error. I do realize that this is caused because of binary characters before the <xml node but I am not sure on how to get rid of them.
I have no control over what is being copied from the ftp and I am using java 1.7
My code for retrieving the file:
client.connect("ftp.domain.com");
client.login("user", "password");
String filename = assetsPath + "/ftpExport.xml";
fos = new FileOutputStream(filename);
client.retrieveFile("/Export.xml", fos);
My code to create the StreamReader:
inputFactory = XMLInputFactory.newInstance();
File f = new File(Parser.class.getProtectionDomain().getCodeSource().getLocation().getPath());
assetsPath = f.toString()+"/../assets";
xmlReader = inputFactory.createXMLStreamReader(
new FileReader(assetsPath + "/Export.xml"));

don't use a FileReader for reading xml as this can corrupt the xml. use a FileInputStream.

Related

Java - Why does BufferedReader(Writer) create a corrupted excel(.xls), but BufferedInput(Output)Stream creates a good one

At the company I work, we have a job that retrieves emails, gets their attachments and saves them. Until now it only had to work with .xml and .txt files and it worked well.
We use the JavaMail 1.4.4 package. Existing code(modified to be more simpler. Don't mind the type checks):
Message message = ...;
MultiPart mp = (MultiPart)message.getContent();
File file = new File(newFileName);
Part part = mp.getBodyPart(indexWhereIsAttachement);
InputStream inputStream = part.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
//method that read all from reader and writes to writer
When I use a .xls file, it doesn't work. This creates a corrupted .xls file. I can't open it with LibreOffice, neither can I open it as a Apache WorkBook in code. But it works for .xml and .txt.
But if I do this:
...
File file = new File(newFileName);
Part part = mp.getBodyPart(indexWhereIsAttachement);
((MimeBodyPart)part).saveFile(file);
It works fine. Looking at the "saveFile()" method, it uses a BufferedInput(Output)Stream. So while reading the file, it doesn't convert the data to characters. Is this what's causing the issues? What exactly happens, that breaks everything?

How to download MS-word file in java?

String fileName="raj.doc";
ServletOutputStream stream=null;
BufferedInputStream buf=null;
stream=res.getOutputStream();
String s1=getServletContext().getRealPath("/web-inf/lib/raj.doc");
File doc=new File(s1);
res.setContentType("application/vnd.ms-word");
res.addHeader("Content-Disposition","attachment;filename= "+fileName);
res.setContentLength((int)doc.length());
FileInputStream input=new FileInputStream(doc);
buf=new BufferedInputStream(input);
int readBytes=0;
while((readBytes=buf.read())!=-1)
stream.write(readBytes);
Give me an example of downloading MS-word file in java. Tell me jar files which are needed.
You don't need any jars if you want to only download the file and not work with it.
Just use this code and replace the URL with the URL of your document. Then you should be able to create a new File and just feed everything you read from the URL in the outputstream of the file.

Reading a pdf file created using iText in java

I am using iText libraries to create pdf files using java, the file is created and it opens up using adobe, but when I try to read it i get java.io.FileNotFoundException: ErRecord.pdf (The system cannot find the file specified)
FileInputStream input = null;
File file = new File("ErRecord.pdf");
System.out.println(file.canRead());
input = new FileInputStream(file);
file.canRead() returns false, is there a way to read the file or make it readable using iText?
I used getAbsoluteFile() and the path was wrong..
I just used the absolute path
File file = new File("c:/Users/rawan/workspace-luna/Prototype_3/ErRecord.pdf");
and it worked just fine

smb file writing in local drive from network drive is not working

I am trying to write a file in my local drive which is available in a network drive on a server. I can write this image and I can see even the size of the file available. But, when I'm opening the file it says preview not available. Content of the file is not coming.
Code which I read the network file
SmbFileInputStream sfis = null;
sfis = new SmbFileInputStream(serverFile);
fileBytes = new byte[(int) serverFile.length()];
sfis.read(fileBytes);
Code which I write the file in my local drive
FileOutputStream fos;
fos = new FileOutputStream(tempFile);
fos.write(fileBytes);
I also tried with file.copyTo method by giving my local file like a smbfile.
serverFile.copyTo(ss);
I figured it out and it works fine.
Change first two lines to
InputStream sfis = new SmbFileInputStream(serverFile);
real change is reference of the variable is now
InputStream

Load file dynamically from jar

I am trying to read a .json file I am packaging with my .jar.
The problem - finding the file so that I can parse it in.
The strange bit is that this code works in NetBeans, likely due to the way these methods work and the way NetBeans handles the dev workspace. When I build the jar and run it, however, it throws an ugly error: Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical.
My code for getting the file is as such:
//get json file
File jsonFile = new File(AndensMountain.class.getResource("/Anden.json").toURI());
FileReader jsonFileReader;
jsonFileReader = new FileReader(jsonFile);
//load json file
String json = "";
BufferedReader br = new BufferedReader(jsonFileReader);
while (br.ready()) {
json += br.readLine() + "\n";
}
I have gotten it to work if I allow it to read from the same directory as the jar, but this is not what I want - the .json is in the jar and I want to read it from in the jar.
I've looked around and as far as I can see this should work but it isn't.
If you are interested, this is the code before trying to get it to read out of the jar (which works as long as Anden.json is in the same directory as AndensMountain.jar):
//get json file
String path = AndensMountain.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
File jsonFileBuilt = new File(new File(path).getParentFile(), "Anden.json");
File jsonFileDev = new File(new File(path), "Anden.json");
FileReader jsonFileReader;
try {
jsonFileReader = new FileReader(jsonFileBuilt);
} catch (FileNotFoundException e) {
jsonFileReader = new FileReader(jsonFileDev);
}
Try
Reader reader = new InputStreamReader(AndensMountain.class.getResourceAsStream("/Anden.json"), "UTF-8");
AndensMountain.class.getResource("/Anden.json") URL when ran outside a jar (for example, when the classes are compiled to a "classes/" directory) is a "file://" URL.
That is not the case when ran from inside a jar: it then becomes a "jar://" URL.
The java.io.File doesn't know how to handle this type of URL. It handles only "file://".
Anyway you don't really need to treat it as a File. You can manipulate the URL itself (either to navigate to a parent directory, for example) or to get its contents (via openStream(), or if you need to add headers, via openConnection()).
java.lang.Class#getResourceAsStream() as I suggested is just shorthand to Class#getResource() followed by openStream() on its result.

Categories