This question already has answers here:
Get real file extension -Java code
(3 answers)
Closed 7 years ago.
I know I can get this by doing this
String ext = FilenameUtils.getExtension("/path/to/file/foo.txt");
But what if someone tries to upload a foo.exe file by just changing the extension type to foo.doc. Is there any way through which I can get the actual extension type without reading the content of file
File file = new File("filename.asgdsag");
InputStream is = new BufferedInputStream(new FileInputStream(file));
String mimeType = URLConnection.guessContentTypeFromStream(is);
From this post : Get real file extension -Java code
Also using java7, you should check out this :
public static String probeContentType(Path path)
throws IOException
http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#probeContentType%28java.nio.file.Path%29
Related
This question already has answers here:
How to solve the java.nio.file.NoSuchFileException?
(6 answers)
Closed 9 months ago.
I try to read a file from the resources and it tells me
java.io.FileNotFoundException: file:/home/simon/IdeaProjects/KTMBlockChain/build/resources/main/certificate_template.docx (No such file or directory)
Note that the file is blue and clickable. When I click on it it also opens the file so it definietly exists in the place expected.
Code:
InputStream in = null;
try {
File file = new File(this.getClass().getClassLoader().getResource("certificate_template.docx").toString());
in = new FileInputStream(file);
IXDocReport report = XDocReportRegistry.getRegistry().
loadReport(in, TemplateEngineKind.Freemarker);
Options options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.ODFDOM);
IContext ctx = report.createContext();
ctx.put("re_wo", pdfData.getReifen());
/*ctx.put("to", invoice.getTo());
ctx.put("sender", invoice.getInvoicer());
FieldsMetadata metadata = report.createFieldsMetadata();
ctx.put("r", invoice.getInvoiceRows());*/
report.convert(ctx, options, new FileOutputStream("result.pdf"));
I dont know what to do anymore...
EDIT 1: Changed code, still not working, another error code but same problem
There are
Disk files, class File;
Resource files (read-only), on the class path, possibly packed in a jar or war.
So here is you should use a resource, and Path is a generalisation of File, and all other kind of URL paths. This caused the error.
URL url = getClass().getClassLoader().getResource("certificate_template.docx");
Path path = Paths.get(url.toURI());
List<String> x = Files.readAllLines(path); // Reading a UTF-8 text.
But docx is not text, but a binary format (actually a zip format).
You either need to use a library or just handle the file as-is, like let the operating system open it.
You could use Files.readAllBytes(); reading UTF-8 will probably cause an error as the bytes are not in UTF-8 format.
After edit of question:
in = getClass().getClassLoader().getResourceAsStream("certificate_template.docx");
This question already has answers here:
Jar can't access txt files
(2 answers)
Java JAR can't find file
(1 answer)
Closed 2 years ago.
I have the following code for check if a file exists on some path, if not, that file is copy from the project, and it works.
public static void verificarDB() throws IOException {
File cygnus_db = new File(System.getProperty("user.home")+File.separator+".local"+File.separator+"cygnus"+File.separator+"cygnus_db_local.db");
if(!cygnus_db.exists()) {
File source = new File("src/assets/bd/cygnus_db_local.db");
File target = new File(System.getProperty("user.home")+File.separator+".local"+File.separator+"cygnus"+File.separator+"cygnus_db_local.db");
FileUtils.copyFile(source, target);
}
}
But when i export my project to a runnable jar i get this exception:
java.io.FileNotFoundException: Source 'src/assets/bd/cygnus_db_local.db' does not exist
at org.apache.commons.io.FileUtils.checkFileRequirements(FileUtils.java:1383)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1060)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1028)
at pruebas.Main.verificarDB(Main.java:123)
at pruebas.Main.main(Main.java:30)
What im doing wrong?
This question already has answers here:
How do I check if a file exists in Java?
(19 answers)
Closed 3 years ago.
In Java using Maven project we may read file's content as a stream by knowing only file's name, for example:
InputStream in = getClass().getResourceAsStream("/" + fileName);
But is there way to check if the file exists without indicating the whole path, just passing file name?
file.exists() can be used to check whether such a file exists or not.Like following:
File file = new File("filepath");
if(file.exists()){
// Do your stuff
}
This question already has answers here:
How to handle ~ in file paths
(5 answers)
Closed 3 years ago.
File f = new File("~/NetBeansProjects/ChatApp/src/chatapp/Server.java");
if(f.exists()) {
System.out.println("File exist");
}
cat ~/NetBeansProjects/ChatApp/src/chatapp/Server.java, prints the content of the file.
But the above program doesn't print "File exist".
The ~ is resolved by the shell, whereas Java do not resolve it. Try something like this:
File f = new File(System.getProperty("user.home"), "NetBeansProjects/ChatApp/src/chatapp/Server.java");
The "home" wildcard (~) cannot be resolved in the JVM. You need to load that property via the Java API:
File f = new File(System.getProperty("user.home"), "NetBeansProjects/ChatApp/src/chatapp/Server.java");
if(f.exists()) {
System.out.println("File exist");
}
This question already has answers here:
How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?
(14 answers)
Closed 4 years ago.
How to get a full native path of the file in String format which is being uploaded using and being used in a manged bean for its validation and upload? I also don't really want to use simple mode for p:fileUpload.
For example, if a file is being uploaded is from desktop then I want the path of the file as "C:\Users\\Desktop\" in a String format.
Any help would be really appreciated...
you can use java.nio.file.Path library
Path path = Paths.get("test.txt");
log("Path: " + path);
log("Absolute: " + path.isAbsolute());