What is the equivalent of PHP's $_SERVER['DOCUMENT_ROOT'] in Java - java

I need to know the Java equivalent of PHP's $_SERVER['DOCUMENT_ROOT'].
I am writing a Java backend program that will take an uploaded image as a byte[] and save the image to the server where my Java program is running. Then I want to send the image url to the user (front-end) or as email; so that the user can click on the url I send to view the image.
In PHP, I use $_SERVER['DOCUMENT_ROOT'] plus the relative path of the file. How do I do that in Java?
I am not using servet. I am using Jersey to return the url. (If that's important)
I need the returned path to be "http://localhost:8080/mypics/pic1.jpg" when the server is localhost and "http://www.mysite.com/mypics/pic1.jpg" when running on a live server. The new File("./") code is not returning "http..."; netbeans cannot find the method ServletContext.getContextPath(), after importing
import javax.servlet.ServletContext;

You can use ServletContext.getContextPath() to get context path or use ServletContext.getRealPath(String path) to get real path for a given virtual path.

The simplest way if you not use servlet API is to create File object point to the ./:
System.out.println(new File("./").getAbsolutePath());
In this case you should see in console path to your application directory. This could be your $_SERVER['DOCUMENT_ROOT'] for this application.

Related

IOException send pdf by Javamail via webservice

Guys I got a problem to send a PDF on java mail on webservice. When I try to run my class for test the email is send sucessful, but somehow when I try to send via webservice the same method can't find the File and shows IOException (The path is not found). So how shall I describe the file path on the method to run it on web service?
Project Build and code.
It look like you have use wrong path.
You should try use absolute path instead of relations path,
Use following code to debug:
String tHomeArquiovo = ...
System.out.println(new File(tHomeArquiovo).getAbsoluteFile());

Get absolute filepath

I have org.springframework.web.multipart.commons.CommonsMultipartFile object and i need to get absolute file path on client device. Thanks
I need to show picture to user, without saving on server.
You are not allowed to get the full path of client device, maybe you can spy the user's username or OS what he dont like you to know.
Use FileApi from Javascript instead (How can I draw an image from the HTML5 File API on Canvas?).
Most browsers will not include the path information on the client.
For example if you have a CommonsMultipartFile you can call getFileItem() to receive a FileItem and then from the FileItem call getName().
For example:
commonsMultipartFile.getFileItem().getName();
However as the documentation states:
Returns the original filename in the client's filesystem, as provided
by the browser (or other client software). In most cases, this will be
the base file name, without path information. However, some clients,
such as the Opera browser, do include path information.

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.

Java http server and web folder

does anyone know how to create HTTP server in Java, but set default folder for web and than load files from it? I want to use com.sun.net.httpserver class.
For example, I have folder named abc next to my java file. The java file runs HTTP server under port 8080. And if I open address http://123.123.123.123:8080/ I want to see list of files from folder abc. In folder abc are some files, eg. image.jpg. So I want to open in my browser address to image file, like http://123.123.123.123:8080/image.jpg. This way I can open all other files from folder abc (also subfolders, files in subfolders etc.).
Is it possible to create this HTTP server?
Would it be somehow possible to run PHP files in the folder?
Thank you very much for your answers.
Why not using embedded Jetty? I am pretty sure that with it you can accomplish what you are looking for.
If you want to execute PHP from within Jetty, refer to http://docs.codehaus.org/display/JETTY/Jetty+and+PHP
Once you have created your server object, you need to register some handlers for the path you want the user to use to fetch documents.
HttpServer server = HttpServer.create(new InetSocketAddress("localhost",8080));
HttpHandler myDocsHandler = new MyDocsHandler();
server.createContext("/abc", myDocsHandler);
There are no built in default handlers, so you will need to write the MyDocsHandler class that implements the HttpHandler interface to handle any requests coming into your server at http://localhost:8080/abc.
The handler requires a single handle method that takes an HttpExchange argument that gives access to the request data and the response stream. It is your responsibility at this point to do what needs doing. So if you wanted the actual files to be located on your hard driver at /usr/local/abc your handler would need to open the requested file using standard file io and stream it back to the user.

Reading a remote file with java on linux

I need to read a remote file using a java app, but the file is in apache server on linux.
I tried with "\\" but doesn't work like windows.
How can i do that?
You'll need to use the URL class:
http://download.oracle.com/javase/1.4.2/docs/api/java/net/URL.html
This is the standard way of reading files from a URL.
Try accessing your file via web browser using url formatted like:
http://server-name-or-ip/path/filename
When you see your file in browser, use that url from your Java app, too.
This depends on a number of things. But we don't really know what question you're asking. Are you asking how to retrieve a document over HTTP? How to do a file copy from Linux? Network shares?
If the file is served by the webserver (in the docroot), the easiest way is probably to request it over HTTP using the URL class as stated above.
If the file is NOT under the webroot (i.e. can't be specified as http://webserver.name/some/path/to/file) then you'll need to use some other method. I'm assuming this is what you meant - you mention \\, the Windows file-sharing (SMB) protocol prefix. The easiest way is to use SSH and scp/sftp, which is probably already installed on the Linux machine - you may need to enable it, and you'll need a login. Then it's as simple as scp user#host:/remote/file/path local/path. You can set up SSH keys to avoid a password.

Categories

Resources