简体   繁体   English

在Restfull Web服务中的Tomcat服务器上打开html文件

[英]Open html file on Tomcat server in Restfull web service

I hava a small web service which is running on tomcat server and i want to open my html file on my tomcat server. 我拥有一个正在雄猫服务器上运行的小型Web服务,我想在雄猫服务器上打开html文件。

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!";
    }
}

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!"? 问:我应该在浏览器中输入哪个URL来查看消息“知道了!”?

A: http://localhost:8080/myresource 答: 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. 设置Web容器的方式有很多(例如tomcat),您将必须验证正在运行的端口以及servlet上下文是什么。 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. 上面的答案假定您正在本地计算机上,端口8080(通常是Java Web服务器的默认端口)和ROOT上下文下运行。

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. 最终用户只需输入{/ etc / passwd},然后根据使用哪个系统用户启动Web容器,Web容器将提供该文件。 Not a good situation. 情况不好。

Q: Ok, great, so how do I allow a user to download a file from my web container? 问:很好,那么如何允许用户从Web容器下载文件?

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. 意思是将文件放在/webapp目录本身内。 Then your web container will serve them automajically. 然后,您的Web容器将自动为其提供服务。

You could set a directory in your MyResource classes constructor and only serve requested files from that particular directory. 您可以在MyResource类的构造函数中设置目录,并且仅提供该特定目录中的请求文件。 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 您需要对“最终用户”输入进行认真的验证,以验证他们是否不要求以下文件: ../../../../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? 好吧,这很容易,只需在这里看一下这个问题: 从REST Web服务向客户端发送文件的正确方法是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM