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
Related
I get the error "java.io.FileNotFoundException: AuthKey_7RHM5B8NS7.p8 (No such file or directory)", the file is clearly in my directory and I am using the relative path for the file. Here is my projects directory.
Project directory Image
final ApnsClient apnsClient = new ApnsClientBuilder()
.setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
.setSigningKey(ApnsSigningKey.loadFromPkcs8File(new File("AuthKey_7RHM5B8NS7.p8"),
"GL87ZNESF6", "7RHM5B8NS7"))
.build();
As you are trying to fetch file from resource folder hence you need to specify path for that.
File file = new File(getClass().getResource("/AuthKey_7RHM5B8NS7.p8").getFile());
or to get the URL
URL res = getClass().getClassLoader().getResource("AuthKey_7RHM5B8NS7.p8");
File file = Paths.get(res.toURI()).toFile();
String absolutePath = file.getAbsolutePath();
You should not use the ApnsSigningKey.loadFromPkcs8File method but instead use the loadFromInputStream method.
The reason is that you are using a resource - and if you build a JAR file from your code, as is often done, your resource will be inside the JAR file and you will not be able to get a File object that points to it.
Code:
InputStream in = getClass().getResourceAsStream("/AuthKey_7RHM5B8NS7.p8");
final ApnsClient apnsClient = new ApnsClientBuilder()
.setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
.setSigningKey(ApnsSigningKey.loadFromInputStream(in, "GL87ZNESF6", "7RHM5B8NS7"))
.build();
in.close();
I put a file to be read into my resources folder
src
|_main
|_resources
|_graphqls
|_test.graphqls
The following snippet reads the file
final String pathToSchemaFile = this.getClass().getClassLoader().getResource("graphqls/test.graphqls").getFile();
final File = new File(pathToSchemaFile);
this is the result I get when I evaluate the File object returned by .getFile() from the preceding snippet.
file:\C:\maven_repository\com\...\app.jar!\graphqls\test.graphqls
When running the following code new FileReader(file) this exception is being thrown
Method threw 'java.io.FileNotFoundException' exception.
file:\C:\maven_repository\com\...\app.jar!\graphqls\test.graphqls (The filename, directory name, or volume label syntax is incorrect)
java.io.FileNotFoundException: file:\C:\maven_repository\com\...\app.jar.jar!\graphqls\test.graphqls (The filename, directory name, or volume label syntax is incorrect)
You're accessing a file that is actually inside a JAR file (like a ZIP).
If your jar is on the classpath:
InputStream is = YourClass.class.getResourceAsStream("1.txt");
If it is not on the classpath, then you can access it via:
URL url = new URL("jar:file:/absolute/location/of/yourJar.jar!/1.txt");
InputStream is = url.openStream();
You can avoid the file by creating an InputStreamReader.
InputStreamReader reader = new InputStreamReader(
this.getClass().getResourceAsStream("/graphqls/test.graphqls"),
StandardCharsets.UTF_8
);
It is advised to use YourClassName.class.getResourceAsStream() unless you have some specific reason to use the .getClass && .getClassLoader.
I'm using camel to create some routes.
I have a file in the resources folder and I only want the file path + name. Not the content.
When I use:
from(URI)
.log("resource:classpath:llave.txt")
I got the content of llave.txt but I need something like
C:\something\llave.txt
Thanks!!
Edit for clarity: I do not need the file information from a File endpoint (also, is easy get that info using file language or the exchange's header).
I need the info of a file located in the resource folder in the project.
Get the file:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("llave.txt").getFile());
Get the path:
//full path ( including the file )
String absolutePath = file.getAbsolutePath();
// path only
String filePath = absolutePath.
substring(0, absolutePath.lastIndexOf(File.separator));
You can access the path using the headers that Camel sets after calling the File component:
from(URI)
.log("$simple{headers.CamelFileAbsolutePath}")
You can read more about the File component here:
http://camel.apache.org/file2.html
With java.nio you can get the full path:
String fullPath = Paths.get(getClass().getResource("llave.txt").toURI()).toString();
And the file name:
String fileName = Paths.get(getClass().getResource("llave.txt").toURI()).getFileName();
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
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();