Reading a pdf file created using iText in java - 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

Related

File written through FileOutputStream in Java is not being accessed by python using AudioSegment.from_file

I am writing one file using Java as following.
Path path = Paths.get("C:\\Temp\\MyFile.mp4");
File f = new File(path.toString());
FileOutputStream fout = new FileOutputStream(f);
IOUtils.copy(stream, fout);
fout.close();
And consuming file in Python as following.
my_file = Path("C:\\Temp\\MyFile.mp4")
song = AudioSegment.from_file(my_file)
And it gives me following error.
'The system cannot find the file specified'
What wrong am I doing? It seems something is holding file from java side and because of that I am not able to access it in python. or May be something I am doing wrong in python itself.
Never mind.
Actually I was using video file for AudioSegment in python "mp4" and that was giving this error somehow.
With wav file it is working fine.

How to create a file from classpath

I'm trying to create a new file in:
project/src/resources/image.jpg
as follows:
URL url = getClass().getResource("src/image.jpg");
File file = new File(url.getPath());
but I get error:
java.io.FileNotFoundException: file:\D:\project\dist\run560971012\project.jar!\image.jpg (The filename, directory name, or volume label syntax is incorrect)
What I'm I doing wrong?
UPDATE:
I'm trying to create a MultipartFile from it:
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "image/jpeg", IOUtils.toByteArray(input));
You are not passing the image data to the file!! You're trying to write an empty file in the path of the image!!
I would recommend our Apache friends FileUtils library (getting classpath as this answer):
import org.apache.commons.io.FileUtils
URL url = getClass().getResource("src/image.jpg");
final File f = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
FileUtils.copyURLToFile(url, f);
This method downloads the url, and save it to file f.
Your issue is that "image.jpg" is a resource of your project.
As such, it's embedded in the JAR file. you can see it in the exception message :
file:\D:\project\dist\run560971012\project.jar!\image.jpg
You cannot open a file within a JAR file as a regular file.
To read this file, you must use getResourceAsStream (as detailed in this this SO question).
Good luck

Programmatically uploading a file?

I'm developing a small program that uploads and downloads files from my box account.
I looked at the docs about uploading files and I found this code:
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
FileInputStream stream = new FileInputStream("My File.txt");
rootFolder.uploadFile(stream, "My File.txt");
stream.close();
I don't really understand how it works. Where can I put the path to the file I want to upload? Or should I use different code?
The constructor for FileInputStream takes in a path to a file. The example in the documentation is uploading a file with the path "./My File.txt" relative to the current directory.
To make it a bit clearer, here's an example using a full path:
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
FileInputStream stream = new FileInputStream("/path/to/My File.txt");
rootFolder.uploadFile(stream, "My File.txt");
stream.close();

My Java Web client not accessing server folders which are shared

I am giving image path as ://serverIp/image/xyz.jpg but I am not showing image on my client application.
For this what i have to do?
I am writting my code as: FileInputStream fin = new FileInputStream(path);
I assume you're using Windows network shares? If so, you should use UNC paths. Your path isn't a valid UNC path, so your file will not be found. Also, for reading files from shares, you could use an URI.
Try the following:
File filetoRead = new File(new URI("\\\\serverIp\\image\\xyz.jpg"));
FileInputStream fin = new FileInputStream(fileToRead);
Note that all backslashes are used twice, this is done to escape the backslashes.

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

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.

Categories