0

I hava a small web service which is running on tomcat server and i want to open my html file on my tomcat server.

My web service is:

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("myresource")
public class MyResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() throws Exception {

        String url = "E:/this.html";
        File htmlFile = new File(url);
        Desktop.getDesktop().browse(htmlFile.toURI());
        return "Got it!";
    }
}
hooknc
  • 4,854
  • 5
  • 31
  • 60
Qandeel Haider
  • 77
  • 2
  • 15

1 Answers1

0

These answers are based on the potential questions in the comments section of the question.

Q: Which url do I put in your browser to see the message "Got it!"?

A: http://localhost:8080/myresource

However, that might not be the exact case for you. There are lots of ways of setting up a web container (like tomcat) and you will have to verify what port you're running on and what your servlet context is. The above answer assumes that you are running on your local machine, under port 8080 (which tends to be the default for java web servers) and under the ROOT context.

A more complete answer would be:

http://<host_name>:<port>:<context>/myresource

Q: How do I allow the End User to say which file they want to download?

A: This is a HUGE security risk. Allowing an End User to enter a path to a file on the server is just not smart. No offense...

The End User could just enter {/etc/passwd} and depending on which system user was used to start your web container, the web container will serve that file. Not a good situation.

Q: Ok, great, so how do I allow a user to download a file from my web container?

A: There are several ways of doing this and I won't go into great detail, but, you could allow the container to serve the files themselves directly. Meaning place the files inside of the /webapp directory itself. Then your web container will serve them automajically.

You could set a directory in your MyResource classes constructor and only serve requested files from that particular directory. You would need to do some serious validation on End User input to verify that they aren't asking for a file like this: ../../../../etc/passwd

Q: FINE, got it, so I'll do validation, NOW, how do I actually serve the file to the End User? I promise I'll be careful...

Well, that is easy, just go take a peek at this question here: what's the correct way to send a file from REST web service to client?

Community
  • 1
  • 1
hooknc
  • 4,854
  • 5
  • 31
  • 60