I'm attempting to use the transferTo method on a MultipartFile, but I'm having issues in doing so. Below is my code:
String videoName = UUID.randomUUID().toString();
File tempVideoFile = new File("src/main/resources/static/campaigns/" + videoName);
//multipart file is video
video.transferTo(tempVideoFile); //this line is what causes an error
This ends up throwing an IOException saying the JVM cannot find the path specified. Here's the error:
Invalid file directoryjava.io.IOException: java.io.FileNotFoundException: C:\Users\Jake\workspace\Test App\src\main\resources\static\temp\work\Tomcat\localhost\ROOT\Test App\src\main\resources\static\campaigns\db95aad3-a3ba-489a-a02e-83f5beece9a7 (The system cannot find the path specified)
Not 100% certain as to why it's not working but I believe the proper directory should be src\main\resources\static\campaigns\db95aad3-a3ba-489a-a02e-83f5beece9a7 instead of the one inside of the Tomcat directory, but I'm not sure.
Any ideas as to why this won't work? The path src\main\resources\static\campaigns\ exists so I'm not sure why.
Related
I found many similar questions on this topic in this forum, but none of the solutions of those questions working for me and this problem is really making me frustrated.
I have the following method which should play a wav file when I call it.
Directory of the wav file is: ProjectFolder/src/resources/Sounds/click.wav
public static void Click()
{
String clickSound = "/resources/Sounds/click.wav";
Media hit = new Media(new File(clickSound).toPath().toString());
MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.play();
}
But it results in the following exception:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: java.net.URISyntaxException: Illegal character in path at index 0: \resources\Sounds\click.wav
at javafx.scene.media.Media.<init>(Media.java:385)
When I set the value of the string clickSound "resources/Sounds/click.wav" instead of "/resources/Sounds/click.wav", the exception says illegal character in path at index 9.
So I am guessing that it is considering '/' character as an illegal character. I tried using '\' instead of '/', but the result was same.
I do not want to change the location of the wav file for certain reason. How can access I access that wav file from ProjectFolder/src/resources/Sounds/click.wav and play it without any exception?
Any kind of help is appreciated.
Thanks in advance.
As stated in the documentation:
The source must represent a valid URI and is immutable. Only HTTP, HTTPS, FILE, and JAR URLs are supported. If the provided URL is invalid then an exception will be thrown.
You are passing in the path to a file, instead of a URI.
You almost certainly don't want a file here anyway; for example, when you deploy your application it will typically be deployed as a jar file, and the media will be an entry in that jar file (so it won't be a file in its own right at all). On top of that, the resources folder is typically part of the source code structure, and for obvious reasons the source code is not usually available at runtime.
Assuming your resources are being deployed to the root of the classpath, which is the usual setup, you need something like
String clickSound = "/Sounds/click.wav";
Media hit = new Media(getClass().getResource(clickSound).toExternalForm());
You listed the file as in directory "ProjectFolder/src/resource/Sounds/click.wav" but when trying to access you have "resources" instead of "resource"
The following code works fine on my Eclipse IDE.
private void getLayersAndDisplay() throws Exception {
URL imageURL = ImageLab.class.getResource("earthlights.jpg");
File imageFile = new File(imageURL.toURI());
URL shapeFileURL = ImageLab.class.getResource("countries.shp");
File shapeFile = new File(shapeFileURL.toURI());
URL shapeFileURL2 = ImageLab.class.getResource("Brasil.shp");
File shapeFile2 = new File(shapeFileURL2.toURI());
displayLayers(imageFile, shapeFile,shapeFile2);
}
However, when compiling to a jar, it gives me a null pointer exception. I thought that since I am getting it as a class.getResource, it would work. Can't I use the File class in a jar? Not even in a cast?
Thank you
An entry of a zip file (that's what a jar file is) is not a file existing in your file system. So you can't use a File, which represents a path on your filesystem, to refer to a zip entry. And you can't use file IO to read its content, since it's not a file.
I have no idea what you want to do, but if you want to read the content of the jar resource, just use ImageLab.class.getResourceAsStream() to get an InputStream back, reading from the entry.
My app downloads a zip file from a remote webserver, then extracts it.
The javascript successfully calls FileTransfer, which logs:
FileTransfer Saved file: file:///data/data/com.yadda.yadda/update.zip
As part of the success function, javascript calls my custom update plugin which immediately tests for the file:
Context ctx = this.cordova.getActivity().getBaseContext();
File update = new File(ctx.getFilesDir(),"update.zip");
if(!update.exists()) Log.w("File not found",update.getAbsolutePath());
The log for that last line is:
File Not Found /data/data/com.yadda.yadda/update.zip
Later in a try/catch block I have an InputStream variable created and one of the catch blocks is a FileNotFoundException which is firing every time.
Begin edit - more info
The FileNotFoundException has an interesting bit. The file path is wrong - even though I'm sending the same "update" variable to create the FileInputStream
InputStream fileis = new FileInputStream(update);
And the interesting bit of the exception:
Caused by: java.io.FileNotFoundException: /data/data/com.yadda.yadda/files/update.zip
End edit
What is going wrong here? Cordova logs that the file transfer completed and the file was saved, but then the file doesn't exist when I test for it! When I create the FileInputStream, why is the path different, causing the exception?
What am I missing? Everything works fine in the IOS version of the app.
Edit 2: per request, I browsed the device filesystem and found that update.zip does indeed exist in /data/user/0/com.yadda.yadda
OK, somewhere there is a bug. I'm inclined to believe it's a bug in getAbsolutePath() because I'm seeing consistent operation elsewhere.
When I create the "File update" variable, then immediately test and log the update.getAbsolutePath() - it shows the correct path. But when I attempt to create the FileInputStream, the path is different (+ /files/)
So, a little searching and I found that in order to access the application data directory (without /files) I must send a different directory with the new File command. Here's what it looks like:
File update = new File(ctx.getApplicationInfo().dataDir,"update.zip");
Obtaining the dir with getFilesDir()
ctx.getFilesDir() = /data/data/com.yadda.yadda/files
Obtaining the correct dir
ctx.getApplicationInfo().dataDir = /data/data/com.yadda.yadda
I am trying to understand "How to get to file by passing relative path of a file or folder?" . Here is the example:
CODE:
public class somex {
public static void main {
String fileName = System.getProperty("user.dir"); <---This gives me path for the current working directory.
File file = new File(fileName + "../../xml_tutorial/sample.xlsx" );
System.out.println(file.getCanonicalPath()); <---This gives me path for the file that is residing in folder called "xml_tutorial".
}
}
>>>>
Here, I know the file location so i was able to pass correct relative path. And, managed to print the file path. I have deleted the "sample.xlsx" and executed the above code; With no failing it gives me the path name and it is same path as when the file exists (i.e. before deleting). How it is possible ? I am expecting EXCEPTION here. why it is not throwing exception ?
Two, I want to use regular expression for the file name, such as: "../../xml_tutorial/samp.*". But this doesn't do the job and it gives me IOException. Why it is not able to identify the file sample.xlsx ? (NOTE: this is when the file exist and one hundred precent sure there is only one file with the name "sample.xlsx")
I have deleted the "sample.xlsx" and executed the above code; With no failing it gives me the path name and it is same path as when the file exists (i.e. before deleting). How it is possible ? I am expecting EXCEPTION here. why it is not throwing exception ?
File doesn't care whether the file actually exists. It just resolves the path. There's no need for the file to exist in order to take the path
/home/tjc/a/b/c/../../file.txt
...and turn it into the canonical form
/home/tjc/a/file.txt
If you want to know whether the file on that path actually exists, you can use the exists() method.
On your second, unrelated question:
Two, I want to use regular expression for the file name, such as: "../../xml_tutorial/samp.*". But this doesn't do the job and it gives me IOException. Why it is not able to identify the file sample.xlsx ?
There's nothing in the File documentation saying that it supports wildcards. If you want to do searches, you'll want to use list(FilenameFilter) or listFiles(FilenameFilter) and a FilenameFilter implementation, or listFiles(FileFilter) and a FileFilter implementation.
I 'm trying to create new file in windows 7 using
file.createNewFile()
but the file is not created and I got the following exception
Message:
The system cannot find the path specified
Stack Trace:
[java.io.IOException: The system cannot find the path specified,
at java.io.WinNTFileSystem.createFileExclusively(Native Method),
at java.io.File.createNewFile(File.java:883),
at com.mercury.mtf.actions.file.CreateEmptyFileTask.execute(CreateEmptyFileTask.java:56),
at com.mercury.mtf.actions.file.CreateEmptyFileAction.execute(CreateEmptyFileAction.java:42),
at com.mercury.mtf.core.AbstractAction.run(AbstractAction.java:50),
at com.mercury.mtf.core.Unit.runUnitAction(Unit.java:347),
at com.mercury.mtf.core.Unit.executeUnitAction(Unit.java:176),
at com.mercury.mtf.core.Unit.run(Unit.java:121),
at com.mercury.mtf.core.execution.DefaultUnitExecutor.call(DefaultUnitExecutor.java:24),
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303),
at java.util.concurrent.FutureTask.run(FutureTask.java:138),
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98),
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:207),
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886),
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908),
at java.lang.Thread.run(Thread.java:619)]
I'm sure that the path exists, but I realized that the folder marked as read only. I tried to remove the read only flag but I can't get this to work.
Make sure your path separator character is proper.. You can use single forward slash or double back slashes. For example,
File f = new File("C:\\Documents and Settings\\thandasoru\\My Documents\\temp.txt");
f.createNewFile();
If the file is temporary you can use this function and you can forget all permissions problems:
File.createTempFile("prefix", "suffix")
Use File newFile=new File(folderName+chipItems[i]); rather than using File newFile=new File(folderName+chipItems[i], "w");. That will be OK. Avoid File Mode when you like to give functionality like Unix touch command.