Create a file in /public using dist file - java

I'm using Play 2.5 and I have written a code, in Java, to create a file in /app/public folder, I parametrized the path in application.conf. After that I created a dist file and when I tried to call that method from my dist file, the file is not getting generated in /public folder. Is it not able to detect the path?
file.create{
path = "public/"
}
Edit: Java Code -
String filePath = conf.getString("file.create.path") + "Excel_file.xlsx";
FileOutputStream fos = new FileOutputStream(filePath);

Related

File only accessible when placed in a location where it will be erased from the jar

I'm new to NetBeans IDE, and am struggling with accessing a file after building the jar file. After reading through many posts on this topic, I decided to try the following code:
BufferedReader read = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/file.txt")));
This works fine when my file is placed inside the "build" folder of the project where the .class files are, but of course this is a problem because it is erased in the "clean and build" process when the jar file is created. I have tried placing it in the src folder, in a separate "resources" package, and in the root of directory. I have also tried calling getResourceAsStream() with "file.txt" and "/src/file.txt," but it only works in the above configuration when the file is with the .class files. Any tips would be much appreciated!
Why not have your file folder inside the tomcat bin and refer the directory from your code. So maven clean will not alter the files and you can remove, update file without needing to restart the application. ( here i have file inside etc )
Path: /Users/username/Documents/apache-tomcat-8.5.15/bin/etc
ArrayList<String> readList = null;
String workingDir = System.getProperty("user.dir");
String fileName = "File.txt";
File file = new File(workingDir+"/etc/" + fileName);
readList = resourceReader.readFile(file.getAbsolutePath());
I have method readFile to parse some data and build the ArrayList in the above example.
Read about System Properties
Turns out the solution was really simple...I had been trying to manually create a resources folder, but the contents kept being deleted upon building of the jar. Instead, I created a resources package and put the file into the auto-generated folder inside the src folder, which packaged the file into the jar. Thanks everyone!

How to open a file independently of the path of the Java project?

I'm working with Eclipse and there, I have Java Project with the name "Test" which also contains text files. A class in this Project should be able to read in one of these files with a BufferedReader. This is my current code for that:
BufferedReader in = new BufferedReader(new FileReader("C:/Users/workspace/Test/testFile.txt"));
My file is always in the Project, but when I move the Project to another path, the file path changes, too, so I have to adjust the code with the new path.
I dont't want that, because it's impractical, so what can I do? How can I get the path of the Project?
You can add file to resources folder and read like
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());
Try something like this:
File currentDirFile = new File(".");
String fileDir = currentDirFile.getAbsolutePath();

get absolute path of a file which is outside workspace in java

My java file is in harness folder. I need absolute path of folder bif_v3 which is inside parent directory.
Although you didn't ask certainly what you want to do, I assume that you want to open a file programmatically from a file which is in harness. You can 'navigate' programmatically using the following methods:
File currentDir = new File("."); //this will be your current directory
File parentDir = currentDir.getParentFile(); //this is parent directory
File newFile = new File(parentDir,"Example.txt"); //this is file Example.txt placed in parent directory
File newFile = new File(parentDir,"bif/Example.txt"); //this is file Example.txt placed in parent directory's directory bif.

Storing files in project directory using Spring

I am trying to work through Spring tutorials on file uploads. What I'm trying to do is have the file be saved to a folder within the project. The folder is called "files" and is separate from the src folder.
|bin
|build
|src
|files
I have this code which accepts a file upload:
public #ResponseBody String handleFileUpload(#RequestParam("name") String name,
#RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));
stream.write(bytes);
stream.close();
//file.transferTo(); help?
return "You successfully uploaded " + name + "!";
What I want to do is use transferTo()to move the file to the "files" directory. When I try the true path, or try some sort of relative path I get this error which is created in the web window I am uploading files in.
Failed to upload file => "uploadedFileName/directory" does not exist
I am not sure why the file name is being appended to the directory path. Any assistance on this is much appreciated.
When you are trying to save the uploaded file to the file "uploadedFileName/directory", you are using relative file path. It's relative to the current working directory of the java process (java process, running your Tomcat Application Server or whatever appserver you are using). And that current working directory is not your project root. The following code:
System.out.println(new File(name).getAbsolutePath())
will print you the exactly path where you are trying to save your uploaded file.
To fix that issue you have to explicitly specify your project root:
File rootDir = new File("C:/Projects/myTestProject");
File uploadedFile = new File(rootDir, name);
file.transferTo(uploadedFile);
In real project you will want not to hardcode that rootDir path, but to retrieve it from some configuration file.
Don't rely to the current working directory of the application server. It could point to any directory.
PS that's out of scope of this question, but please be careful with saving data to the user-provided file names. Malicious user could post file with name "../../../../../../../../SomeSensitiveDirectory/SomeFile" and will overwrite that file unless you explicitly check that input parameter name for bad characters.
When you upload a file, you have to save it to another file.
You should use File#createTempFile() which takes a directory instead.
<p style="border-style:solid; border-color:#FFFFFF;">
<br>
File file = File.createTempFile("upload-", ".bin", new File("/path/to/your/uploads"));
<br><br>
item.write(file);
<br><br></p>

getResource() to a file at runtime

I put some .txt files under the src folder (in the resources folder).
But I can't create a valid File at runtime from this resource.
String path = this.getClass().getResource("/resources/file.txt").getFile();
File file = new File(path);
if (!file.exists()) {
}
I run my program from eclipse. I didn't put in classpath anything.
I want my text files to be embedded into the .jar file, when I run my app I want to grab those files and copy them into some location.
UPDATE
if I do InputStream is = getClass().getResourceAsStream("/resources/file.txt");
I get the stream!!
As you already discovered yourself soon after posting your question, this works:
InputStream is = getClass().getResourceAsStream("/resources/file.txt");
The reason this works, while your original code doesn't, is because a "file" inside in a zip file (a jar file is a zip file) is not a real file until it has been extracted. But extracting the file is what you are trying to do, so at that point in your program, it's not a real file. So this question is an X-Y problem: you wanted to create a File object, but that wasn't possible - you needed to refer back to what you were originally trying to do, which was read from the zip entry.
You said that you are using eclipse, and that you dragged and dropped your text files into the "src" package. "src" is not a package. It is simply a file system directory. By default in a Java project in eclipse all your source code is stored in a directory called "src" and all your .class files are stored in a directory called "bin". getClass().getResource() resolves to the location of your .class files. You must move the text files into the "bin" directory.
What package is your class in?
I wrote very similar code to yours in the default package and ran it in eclipse.
import java.io.File;
public class ResourceTest {
public static void main(String[] args) {
ResourceTest rt = new ResourceTest();
rt.openFile();
}
public void openFile() {
String path = this.getClass().getResource("/resources/file.txt").getFile();
File file = new File(path);
System.out.println(path);
System.out.println(file.getAbsolutePath());
System.out.println(file.exists());
}
}
I see this output:
/C:/Users/rab29/Documents/eclipse/Overflow/bin/resources/file.txt
C:\Users\rab29\Documents\eclipse\Overflow\bin\resources\file.txt
true

Categories