I use Eclipse + Vaadin framework.
At first I save my image into the WEB-INF folder.
Then I load this image into the FileResource variable, create Image variable and then add Image variable into Layout.
The page URL to the image resource looks like: http://servername/sitename/APP/connector/0/16/source/qwerty1.png
How to get URL in that format for use image externally?
Variable basepath returned local path: "..../WEB-INF/qwerty1.png"
String str = (String) VaadinService.getCurrent().getBaseDirectory().
getAbsolutePath() +"/WEB-INF/qwerty1.png";
File temp = new File(str);
FileOutputStream fos = new FileOutputStream(temp);
fos.write(os.toByteArray());
fos.flush();
fos.close();
String basepath = VaadinService.getCurrent().getBaseDirectory().
getAbsolutePath() +"/WEB-INF/qwerty1.png";
FileResource resource = new FileResource(new File(basepath));
Image image2 = new Image("Image from file", resource);
addComponent(image2);
If you put the file in ...src/main/webapp/VAADIN/image.png, it should be available using for example localhost:8080/VAADIN/image.png.
Related
I'm trying to access a resource from a jar file. The resource is located in the same directory where is the jar.
my-dir:
tester.jar
test.jpg
I tried different things including the following, but every time the input stream is null:
[1]
String path = new File(".").getAbsolutePath();
InputStream inputStream = this.getClass().getResourceAsStream(path.replace("\\.", "\\") + "test.jpg");
[2]
File f = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
InputStream inputStream = this.getClass().getResourceAsStream(f.getParent() + "test.jpg");
Can you give me some hints? Thanks.
If you are sure, that your application's current folder is the folder of the jar, you can simply call InputStream f = new FileInputStream("test.jpg");
The getResource methods will load stuff using the classloader, not through filesystem. This is why your approach (1) failed.
If the folder containing your *.jar and image file is in the classpath, you can get the image resource as if it was on the default-package:
class.getClass().getResourceAsStream("/test.jpg");
Beware: The image is now loaded in the classloader, and as long as the application runs, the image is not unloaded and served from memory if you load it again.
If the path containing the jar file is not given in the classpath, your approach to get the jarfile path is good.
But then simply access the file directly through the URI, by opening a stream on it:
URL u = this.getClass().getProtectionDomain().getCodeSource().getLocation();
// u2 is the url derived from the codesource location
InputStream s = u2.openStream();
Use this tutorial to help you create a URL to a single file in a jar file.
Here's an example:
String jarPath = "/home/user/myJar.jar";
String urlStr = "jar:file://" + jarPath + "!/test.jpg";
InputStream is = null;
try {
URL url = new URL(urlStr);
is = url.openStream();
Image image = ImageIO.read(is);
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
is.close();
} catch(Exception IGNORE) {}
}
I'm trying to access a resource from a jar file. The resource is located in the same directory where is the jar.
my-dir:
tester.jar
test.jpg
I tried different things including the following, but every time the input stream is null:
[1]
String path = new File(".").getAbsolutePath();
InputStream inputStream = this.getClass().getResourceAsStream(path.replace("\\.", "\\") + "test.jpg");
[2]
File f = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
InputStream inputStream = this.getClass().getResourceAsStream(f.getParent() + "test.jpg");
Can you give me some hints? Thanks.
If you are sure, that your application's current folder is the folder of the jar, you can simply call InputStream f = new FileInputStream("test.jpg");
The getResource methods will load stuff using the classloader, not through filesystem. This is why your approach (1) failed.
If the folder containing your *.jar and image file is in the classpath, you can get the image resource as if it was on the default-package:
class.getClass().getResourceAsStream("/test.jpg");
Beware: The image is now loaded in the classloader, and as long as the application runs, the image is not unloaded and served from memory if you load it again.
If the path containing the jar file is not given in the classpath, your approach to get the jarfile path is good.
But then simply access the file directly through the URI, by opening a stream on it:
URL u = this.getClass().getProtectionDomain().getCodeSource().getLocation();
// u2 is the url derived from the codesource location
InputStream s = u2.openStream();
Use this tutorial to help you create a URL to a single file in a jar file.
Here's an example:
String jarPath = "/home/user/myJar.jar";
String urlStr = "jar:file://" + jarPath + "!/test.jpg";
InputStream is = null;
try {
URL url = new URL(urlStr);
is = url.openStream();
Image image = ImageIO.read(is);
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
is.close();
} catch(Exception IGNORE) {}
}
In DynamicWebProject - Eclipse, How to get File Object for file present in src folder, i mean to say class path.
Below program works fine but new update to file is present in wtpwebapps, instead i want to get changed in file present in src folder of Eclipse only(How to get FileOutputStream for file present in classpath).
String filename1 = "/WEB-INF/classes/server123.properties";
ServletContext context = req.getSession().getServletContext();
String path1 = context.getRealPath(filename1);
System.out.println("PATH 1 IS :"+path1);
FileInputStream fis1 = new FileInputStream(new File(path1));
int ch1;
while ((ch1 = fis1.read())!=-1) {
System.out.print((char)ch1);
}
fis1.close();
FileOutputStream fos = new FileOutputStream(path1);
fos.write("jayesh".getBytes());
fos.close();
you can use getServletContext()/filename in your code i.e.
properties.load(new FileInputStream(getServletContext().getRealPath("/filename")));
or
properties.load(new FileInputStream(getServletContext().getRealPath(/filename")));
I have successfully manage to upload my images to c:/images
How can I upload the in my root folder?
String fileName = getFileName(filePart);
FileOutputStream os = new FileOutputStream("C:/images" + fileName);
os.write(b);
You can use ServletContext.getRealPath(relativePath) to obtain the real path of file.
String relativePath="/images/" + fileName;
String realPath=getServletContext().getRealPath(relativePath);
FileOutputStream stream=new FileOutputStream(realPath);
I get the exception: "URI scheme is not file"
What I am doing is trying to get the name of a file and then save that file (from another server) onto my computer/server from within a servlet.
I have a String called "url", from thereon here is my code:
url = Streams.asString(stream); //gets the URL from a form on a webpage
System.out.println("This is the URL: "+url);
URI fileUri = new URI(url);
File fileFromUri = new File(fileUri);
onlyFile = fileFromUri.getName();
URL fileUrl = new URL(url);
InputStream imageStream = fileUrl.openStream();
String fileLoc2 = getServletContext().getRealPath("pics/"+onlyFile);
File newFolder = new File(getServletContext().getRealPath("pics"));
if(!newFolder.exists()){
newFolder.mkdir();
}
IOUtils.copy(imageStream, new FileOutputStream("pics/"+onlyFile));
}
The line causing the error is this one:
File fileFromUri = new File(fileUri);
I have added the rest of the code so you can see what I am trying to do.
The URI "scheme" is the thing that comes before the ":", for example "http" in "http://stackoverflow.com".
The error message is telling you that new File(fileUri) works only on "file:" URI's (ones referring to a pathname on the current system), not other schemes like "http".
Basically, the "file:" URI is another way of specifying a pathname to the File class. It is not a magic way of telling File to use http to fetch a file from the web.
Your assumption to create File from URL is wrong here.
You just don't need to create a File from URL to the file in the Internet, so that you get the file name.
You can simply do this with parsing the URL like that:
URL fileUri = new URL("http://local.wasp.uwa.edu.au/~pbourke/miscellaneous/domefisheye/ladybug/fish4.jpg");
int startIndex = fileUri.toString().lastIndexOf('/');
String fileName = fileUri.toString().substring(startIndex + 1);
System.out.println(fileName);