How to get server local drive path in web application? - java

I am developing a jsf web application in which i have store some image files on my local computer lets say D:\images. The server running on this computer only. How can i access my local drive files on my web application.
I tried
<p:graphicImage value="D:\\Temp\tec0178.jpg">
or
`<p:graphicImage value="D:/Temp/tec0178.jpg">`
this not work for me.
If i place the images in my web application
<p:graphicImage value="resources/images/Male.png"/>
its working.

If you need to store it on a physical location on your server where your application is deployed, you could just work with basic I/0 operations in JAVA using File class.
Local drives are directly accessible.
http://docs.oracle.com/javase/tutorial/essential/io/
You could also explore Google Guava API for the same.
A simple example using Google Guava API is:
File imageFile = new File("D:\\images", imgFileName);
FileOutputStream outputStream = new FileOutputStream(newAttachment);
ByteStreams.copy(inputStream, outputStream);
Also, to access images over URL you'll have to add the directory as context. Check out the blog post to access image file as URL:
http://th1rty7.blogspot.in/2009/05/tomcat-is-often-considered-to-be-too.html

This is not exactly a JSF answer, but I like easy ideas: why don't you write a servlet called Images, for example, that receives as a parameter relative routes to your local directory? That servlet would know exactly where to look (you could have d:\Images stored in some properties) and would be as simple as:
<img src="http://yourserver/yourapp/Images?route=someImage.jpg"></img>
would lead to
ImagesServlet.java
...
// assume 'properties' is some way to access your application properties, be it in database,
// or .properties file, or whatever suits you best. In this case, it points to d:\Images
File imageFile = new File(properties.getRoute(), request.getParameter("route"));
InputStream is = new FileInputStream(imageFile);
ByteStreams.copy(inputStream, request.getOutputStream());

It is better to store your images in web application itself. you can call it like
<img src="\images\appimage.jpg">

Related

Unable to access file in hard drive using Apache Tomcat server

I'm developing local-only JSP application using Apache Tomcat server. I would like to put a promotion videos on my intro page, but I don't want to move them to webapp folder or anywhere else.
The promotion videos are located:
E:\data\videos\2018...
But writing a JSP/HTML like this wont launch the video, but however it works off-server (launching html from desktop for example, so the path may not be issue?)
<video src="file:///E:/data/videos/2018/promotion1.mp4" controls></video>
Local file links from remote resources are disabled by almost all browsers by default. There are certain possibilities to overcome that, e.g.:
http://kb.mozillazine.org/Links_to_local_pages_do_not_work
https://discourse.mozilla.org/t/opening-links-to-local-files-file/16449/2
To access your static media files from remote page you need to configure your Tomcat server as described here: http://teknosrc.com/access-local-files-static-content-images-videos-media-files-outside-web-application-system-apache-tomcat-server/
solution:
a.) make sure your server is on the same system where the media files are.
b.) If so, you have to create a folder (ex. media) in your application folder inside /src/main/webapp/ and have to put all media files inside a media folder. After that, you can surely access the media files through a server.

How to get client files in Java bean?

I develop a web application using JDeveloper. Then, my scenario is I want to get a file from client directory (e.g. C://Image.jpg). What I want to achieve is the client's directory defined programmatically. So, I used InputStream, but it will search a file in server directory. if I used UploadedFile, I don't know how to define it. Note that I don't want to use InputFile.
Does anyone have a solution for me?
Search for HTTP File Upload. You need an <input type='file'> control on your webpage, and form encoding set as enctype='multipart/form-data'.
Generally, you can't control the default directory where the browser is going to open a file chooser -- it normally starts from the "user home" directory, but other dirs can be navigated to.

How to get the direct url of a binary file in the Google Blobstore?

My application generates binary data (compressed xml) and saves it to the Google App Engine Blobstore. I want to return a public URL that clients can use to access this binary file from anywhere, without needing to go through AppEngine and my Java servlets. I know it's possible to get this kind of URL for images using ImagesService#getServingUrl, but what about for other types of files?
I create my file like this:
FileService#createNewBlobFile("application/octet-stream", "myfile.bin");
When I call AppEngineFile#getFullPath() I get something like:
/blobstore/writable:a2c0noo4_LNQ0mS6wFdCMA
I can see the file created in my dev filesystem with a different random name. What's its URL?
You can use Google Cloud Storage for your file.
You can then provide a public cloud storage URL for you file that will allow you clients to download it without going through AppEngine. There are a whole lot of different ACL scenarios you can use to tighten down the access as well, it's in the docs.
Using Google Cloud Storage with the Files API.
Cloud Storage Docs.
According to the docs, there's no public URL for objects in the blobstore. You can only access it through the Blobstore API from your app. So if you want your blobstore objects to each have a URL, you'd have to create your own handler for that, which of course will have to pass through AppEngine and your Java servlets.
I think the ImagesService#getServingUrl doesn't generate a Public URL for the images in blobstore. That URL doesn't really point directly to your blobstore data. It points to an image service that is able to access your blobstore, and serves your image for you through its own high performance infrastructure.

How to retrieve images stored in file server?

I have a java web service through which I upload images to a file server. I want to access these images from my java web app. How can I make the image files (and eventually other static files) available from this file server?
The only thing I could think of was to use Apache Http server as a proxy to my web app for these images, but that circumvents the security measures of the web app.
UPDATE:
Servlet container: Tomcat
Web app is on separate server from images.
Web service is on same server as images and has direct access to file system.
Both web app and service use spring security for authentication/authorization, I want to continue to use this security framework to for image access.
How are the files stored?
If security is a concern the best option might be to create a Servlet (or something similar) which will load up the image and serve it to the user, once it has checked their credentials.
How you load the image depends on exactly how they're stored, if you can access them via HTTP you can always open up a URLConnection to the file from the Servlet and serve it directly that way (i.e. using the Servlet as a sort of proxy server).
Without more details it's difficult to be specific.
I'm not sure if this will solve your problem, but it sounds like you should set up a context path that will map a URL to the path on your server. This can be done with tomcat's context files.
For a good explanation of the solution, check out a post on How to Program with Java
Sounds similar to Apache Hadoop.
Once image/file is requested, you have to make API call and pull the file out and do one of the following:
Store the temp file to the "temp" directory on web accessible server. You will need, some kind of cleaner/gc running in the background to clean those temp files. This is how Facebook does it with photos.
Instead of storing file on the server check the file type and set HTTP Content-type header to the appropriate file type. Image source will look like this <img src="getPicture.jsp?id=1234" />

Is it possible to write files to a remote directory using java?

I have the directory mapped on my machine so that I can browse and write to it via Windows explorer. I would like to write files via java.
File f = new File("http://dev1:8080/data/xml/myTestFile123.xml");
f.createNewFile();
I am getting the following error:
Exception in thread "main" java.io.IOException: The filename, directory name, or volume label syntax is incorrect
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at MainTest.createTestFile(MainTest.java:156)
at MainTest.main(MainTest.java:72)
Is there any way to write files to a mapped directory that has the http:// in front? Because thats the way the directory is provided to me. It is a virtual directory that an oracle database is creating.
My understanding is that you are trying to write to an Oracle XML DB Repository. Oracle XML DB Repository is a feature that has been introduced by Oracle9i Database Release 2 for XML storage and that can be accessed through FTP or HTTP/WebDAV. In your case, it looks like you're trying to use HTTP/WebDAV.
As explained in the WedDAV page on Wikipedia:
WedDAV is a set of extensions on
top of HTTP that allows users to edit
and manage files collaboratively on
remote World Wide Web servers.
In other words, adding files, deleting them, renaming them, etc in a WebDAV repository is done using HTTP words: PUT, DELETE, MOVE, etc (see RFC 4918 for more details).
Consequently, interacting with a WebDAV server can be done using classes from java.net.
Or you could use a higher level API like Jakarta Commons HttpClient.
Or you could use a Java WebDAV client like the one provided by the Slide project. This article shows how to do it and it looks simple. However, as the Slide project is now retired, I wouldn't recommend it.
Luckily (or not), the Apache Jackrabbit project is an alternative to Slide... but AFAIK the WebDAV support in Jackrabbit is more focused on server-side implementations than clients. Anyway, you'll find some code samples in this thread on the jackrabbit-users mailing list.
I think I'd choose HttpClient and use the Tutorial or the Sample Code as starting points.
I'm not really sure what I'm talking about here (not a Java guy) but although you may "have it mapped" you're passing in a URL instead of an expected file system path. If (for example) you have a mapped drive under Windows, use the drive letter assigned.
Your trying to pass the location URI with a protocol. You need to pass location sans protocol:
\\dev1\data\xml\myTestFile123.xml
Instead of trying to using a mapped drive letter (seems very weak), have a look at JCIFS:
JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java. CIFS is the standard file sharing protocol on the Microsoft Windows platform (e.g. Map Network Drive ...). This client is used extensively in production on large Intranets.
This piece of code shows how to Logon to a Remote Machine and Write File using jCifs (credits to Muneeb Ahmad):
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
public class Logon {
public static void main( String argv[] ) throws Exception {
String user = "user:password";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
String path = "smb://my_machine_name/D/MyDev/test.txt";
SmbFile sFile = new SmbFile(path, auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write("Muneeb Ahmad".getBytes());
System.out.println("Done");
}
}
Edit: As mentioned in a comment added to the original question, my understanding is now that you are trying to write to a WebDAV directory. I'll cover the WebDAV topic in another answer for more clarity.
How have you mapped the file in Windows? I suspect it is not using the HTTP protocol, because no such mechanism exists for creating files. So you will not get anywhere using "http" as your protocol.
Find the mapped drive letter, you probably want something more like:
File f = new File("F:\\dir\\file.ext");
If you are using Samba you might want to take a look at JCIFS then you can use:
smb://server/share/
Use the local path
If you can see myTestFile123.xml in windows explorer, then right-click it and copy the Location: property value. Then use exactly this as the new File() argument, but either double up the backslashes or change them to forward slashes.

Categories

Resources