Files is stored in this location.
D:/uploads/component.png
This is the HTML code to download a file.
<a target="_blank" href="../{{att.filePath}}"><strong>{{att.fileName}}</strong></a>
But when I click on the link it opens up as in this path, which is wrong as it is not in the server.
http://localhost:9190/D:/uploads/component.png
How can I see the file in web browser, what am I doing wrong here.
Though you are not showing your variables' contents, it seems your variable contains the absolute path for the file. Therefore you don't need to add relative parts (e.g. ../) to the path.
Also, if you want to host a file for downloading, you must make it available inside the hosted directory (document root). You can achieve this in multiple ways:
A) Simply copy the file/directory into the document root. Then you can also use relative links if you wish (you have to change the variable!).
For example, you may have a structure like this:
public_html/
uploads/
component.png
index.html
Then you can link the file in index.html using the absolute path /uploads/component.png, or using the relative path uploads/component.png.
B) Create a symbolic link to the file/directory inside the document root.
For example if you create a symbolic named uploads in your document root for your D:/uploads/ directory, you will have this structure:
public_html/
uploads/ -> D:/uploads/
index.html
This way you can still have the file physically at D:/uploads/component.png but it will be available also in public_html/uploads/ and you can use the same paths as in method A).
C) If you are developing a webapplication (it seems so, as you tagged Java and Tomcat), there is another option. You can define a controller method which maps requests like /uploads/* and implement it so it will read the file specified by the URI from your D:/uploads directory.
Related
I want to use the relative path in xml files in our project. I have the files in the following location.
D:/SDC-Builds/SRDM2.3.0/SRDM/Svr/IdP/IdPserver/conf/attribute-r.xml
I have other xml file which needs to ref the above location, I use the following relative path to be independent of machines and folder names.
In D:/SDC-Builds/SRDM2.3.0/SRDM/Svr/IdP/IdPserver/others/service.xml, i am using the code like below
service.xml
<srv:ConfigurationResource="../../../../../../IdP/IdPserver/conf/attribute-r.xml">
</srv>
Please tell me am i using proper convention to refer the attribute-r.xml ?
If your Project Root Directory is SRDM, then you need to get back from your executable path.
Say you have your exe file at SRDM/Svr/bin/EXECUTABLE.exe then,
you need to mention in xml as
<srv:ConfigurationResource="../IdP/IdPserver/conf/attribute-r.xml"></srv>
ie. Current working directory is SRDM/Svr/bin/ and from that you need to get back up to common junction[Svr] in your case.
I am making a web application using jsp and servlets .But am facing two problem that i have no ides how to remove :
Problem 1 : I am creating new folders in my WEB-INF folder .But what i want is that instead of giving full paths .I just provide relevant path Like :
File tempfilesstore = new File("C:\\Users\\admin\\Desktop\\SharedCrpto1\\web\\RetrievedFiles\\"+fileid+"-"+personname);
if(!tempfilesstore.exists())
tempfilesstore.mkdirs();
Can this full path be avoided as only path from web folder of the application is required.
Problem 2 : I keep a image in this folder by performing some operation on original image being browsed by the client on browser.
Now when i see the image in folder then it is present their But if i try to see the same image in browser it does not display the image .When i refresh my page for 3-4 times than sometimes it get displayed and sometimes after manually opening it by going to specified location.What can be reason for it ?Please help.
Here is how am trying to get image on browser :
<img src="RetrievedFiles/<%=path%>/<%=sharedfilee%>" alt="Image Preview Not Availablee" width="300" height="300" />
Here ,
String path=presentfileid+"-"+personname;
String sharedfilee=rs.getString("FILE_NAME");
First of all, in a JEE point of view, all files within the WEB-INF folder are not meant to be accessed by anyone but your server. It means that images, CSS files, javascript files, etc. in this directory will not be rendered by your web browser. Your JEE server will prevent that to happen.
So, in order to access files from your web browser, you need to put them outside the WEB-INF folder (at the same level, in a "images" folder, for instance).
For your first problem : Yes, you can use relative paths to instantiate files, using your classLoader.
this.getClass().getClassLoader().getResource("resourcePath")
or
this.getClass().getClassLoader().getResourceAsStream("resourcePath")
depending on your needs. The first one returns a URI (http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)) whereas the second returns an InputStream (http://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)) that you can use with a FileInputStream.
The ressources are located in the classes folder, and the path is relative to the class your get the resource from. For example, you can use "/myImage.png" as a path to get the image at the root level.
Putting all your resources files in the "classes" folder (within subfolder if you want to) is a good architecture design.
For your second problem, you have mainly 2 solutions :
if the image is rendered without any transformation, put it in a folder outside WEB-INF (see the beginning of my comment), and it will be visible from outside. In you JSP, you can access it like that :
request.getContextPath() + "/" + sharedfile
if the image needs a transformation, use a servlet instead
I hope that helps you.
Regards,
Alexandre FILLATRE
I have some stuff under src/main/resources path.
Specifically I have a folder with report templates called reports.
I understand that when the application is deployed/run all files and folders under src/main/resources go to the classpath, namely my project's WEB-INF/classes.
This means that a folder WEB-INF/classes/reports will be created in my server.
Now I want to access my reports as paths, not as inputstream, because my reporting code in java supports a filepath and not an inputstream. So I have to be able to get the WEB-INF/classes/reports absolute path (or relative, I don't care as long as it is right).
Reading some answers regarding similar questions, I have already tried the following things:
getClass().getResource(".").getPath(); --> this returns the exact path of the class I am currently at in my classpath, namely: C:\Tools\JBoss Application Server 7.1.1\standalone\deployments\myProject.war\WEB-INF\classes\aaa\bbb\ccc\ddd
getClass().getClassLoader().getResource(".").getPath(); --> this returns: C:\Tools\JBoss Application Server 7.1.1\modules\sun\jdk\main\service-loader-resources, which is completely irrelevant.
I want something to return C:\Tools\JBoss Application Server 7.1.1\standalone\deployments\myProject.war\WEB-INF\classes
If it is not possible, I will get the first path and go as many folders back as needed to reach classes folder.
Thank you.
You need ServletContext.getRealPath(String) method.
getServletContext().getRealPath("/WEB-INF")
I am using JSP on NetBeans.
In the java code, I am trying to read data from a file. First I open the file by specifying its path.
Because my code runs on a server (GlassFish), I would like to have my file path independent of the machine where it runs. Therefore, I want to start the path with the folder name that contains the file which is saved on the root of the project directory. I tried so hard to achieve that but I couldn't.
I read online and I found this way, but it still doesn't work:
<%
//building the tree here.
GraphBuilder tree = new GraphBuilder("${pageContext.request.contextPath}\\src\\java\\Database\\OptimizedFullTermFile.pad");
%>
Can anyone help? Thank you.
Just read it from the classpath. Given that you're using the typical src folder representing the Java source code (the Java package/class structure), I assume that the file OptimizedFullTermFile.pad is placed in the Java package java.Database (eek, a capital in package name? lowercase it all). In that case, it's already in the classpath and thus you can just get it straight from there as follows:
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("java/Database/OptimizedFullTermFile.pad");
// ...
As to your failed attempt: the EL ${pageContext.request.contextPath} isn't ever going to work in a scriptlet. Even if it did, it's not the right thing, it returns the context path in the webapp URL which is absolutely not part of the local disk file system path, let alone the classpath. Using scriptlets is strongly discouraged since a decade, by the way.
See also:
getResourceAsStream() vs FileInputStream
Where to place and how to read configuration resource files in servlet based application?
You can try the following code to get your directory file in project from the following code.
this.getServletContext().getRealPath("")+[your file path in project web directory]
Can anybody tell me how do i give absolute path of the img tag's src attribute?
The following doesn't work
<img alt="NO IMAGE" src="/home/administrator/tiger-info0[1].gif"/>
I am working On Ubuntu and i am very sure that image exists on this path.
This is probably happening because the image is located outside the web server's document root.
Your web server will not be able to serve anything from outside the document root. One possible workaround is to use a scripting language that has access to the file system, and route the images through the script. For example, you may want to check out the following implementation in php:
Serving Images Outside Document Root Via PHP
You can also create a symbolic link of /home/administrator/ into the document root:
ln -s /www/yoursite /home/administrator
hmm why don't you copy the image to your web directory and give it the relative path? you server (apache?) may not be able to access the file to serve the browser.
if you are making a local html page you can use that path but if you are creating a website you have to use the absolute path to the document root. And make sure the image path is correct (use firebug)
Give your path correctly with domain or use ../ or ./ is for to represent correct relative path.
You cannot access files that are not in your document root. Get Java application server to not delete your folder. You can probably do this by having one folder into which your users can upload files, and add that folder to your project. You can let users create subfolders inside that main folder, and since the main folder is a part of your project, cleaning the build will not automatically delete it or its subfolders.