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.
Related
In my web application I have a link which, when clicked, invokes an external web service to retrieve a download URL for a file.
I need to send back to client the file which is beyond this URL, instead of the download URL retrieved from the web service. If possible, I would also like to do it without having to download the file on my server beforehand.
I've found this question about a similar task, but which used PHP with the readfile() function.
Is there a similar way to do this in Java 8?
If you doesn't even want to handle that file you should answer the request with a redirect (eg HTTP 301 or 302). If you want to handle the file you should read the file in a byte buffer and send it to the client which would make the transfer slower.
Without seeing your implementation so far, this is my best suggest.
Ok, I'm a beginner so this maybe stupid but i afraid that clients can modify static resources (css/javascript files) on server if they can load them directly through URL path (Of course I have to put css/javascript files outside of WEB-INF folder).
If my hypothesis is wrong, could you give me links or quotes to help me expand my knowledge ? Thank you :)
When a user's browser requests resources from your server, they are performing a GET request. This request will not directly change any file on your server. The request will go through your web server and will be processed. In the case of the resources such as css/javascript files, the web server sees the user is requesting the file and sends the contents of the file back. There is no way the user can update the contents of those files on the server unless you write code on the server to allow them to update the files. If the user has direct access to the server via ssh or other protocol and has permissions on the folder that holds the resources, they would be able to change them.
The whole process is much more complex for going through the web server, but for brevity left out here. Here is a good article that explains what really happens when you go to an address in a browser:
https://medium.com/#maneesha.wijesinghe1/what-happens-when-you-type-an-url-in-the-browser-and-press-enter-bb0aa2449c1a
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.
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" />
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.