简体   繁体   English

将数据直接从浏览器发送到Java Desktop应用程序

[英]Sending data directly from Browser to Java Desktop application

I'm making a web application using TypeScript as the client-side scripting language. 我正在使用TypeScript作为客户端脚本语言来制作Web应用程序。 Further technologies I use are Spring Boot , PostgreSQL and Angular . 我使用的其他技术是Spring BootPostgreSQLAngular When the user clicks a button on the website, my already running Java desktop application ("independent" from the web application, not the web application's server) should receive data sent from the web application and then prompt the user to import it into the Desktop application. 当用户单击网站上的按钮时,我已经运行的Java桌面应用程序(与Web应用程序“独立”,而不是Web应用程序的服务器的“独立”)应接收从Web应用程序发送的数据,然后提示用户将其导入到Desktop中应用。

How can this be done? 如何才能做到这一点? Any example codes available? 有可用的示例代码吗?

Can I start a simple listener server on my Desktop application waiting for the client (the browser) to send the payload of import data to the listening server (in a platform-independent way)? 是否可以在桌面应用程序上启动一个简单的侦听器服务器,以等待客户端(浏览器)将导入数据的有效负载发送到侦听服务器(以与平台无关的方式)?

Note that this is different from the Desktop application to download data from an Internet resource since the resource should be determined by a click in the browser itself , not through the Desktop application's interface. 请注意,这不同于从Internet资源下载数据的桌面应用程序,因为该资源应由浏览器本身中的单击确定 ,而不是通过桌面应用程序的界面来确定。

Registering my Java Desktop application to an URI scheme only allows me to launch the Desktop application with a custom URI as parameter and it's Windows only as well which is therefore not a satisfying solution. 将我的Java Desktop应用程序注册到URI方案仅允许我以自定义URI作为参数启动Desktop应用程序,并且它也仅是Windows ,因此也不是令人满意的解决方案。

I understand your setup as follows: 我了解您的设置如下:

Browser <--HTTP/HTTPS via Internet-->SpringBoot 浏览器<-通过Internet的HTTP / HTTPS-> SpringBoot

A webpage rendered in the browser should communicate with a local desktop application. 浏览器中呈现的网页应与本地桌面应用程序通信。

The solution is to establish a HTTP connection between your webpage and the desktop application as follows: 解决方案是按照以下步骤在网页和桌面应用程序之间建立HTTP连接:

  • your desktop application must include an embedded servlet container as for example undertow that binds a server socket to 127.0.0.1:yourPort 您的桌面应用程序必须包括嵌入式servlet容器,例如将服务器套接字绑定到127.0.0.1:yourPort的undertow
  • the embedded servlet container requires a CORS Filter as the webpage's origin is certainly different from 127.0.0.1. 嵌入式Servlet容器需要一个CORS过滤器,因为网页的来源肯定不同于127.0.0.1。
  • The webpage simply opens a connection with the help of XMLHttpRequest, eg 该网页只是借助XMLHttpRequest打开一个连接,例如
     var req = new XMLHTTPRequest(); req.open("POST", " http://127.0.0.1:yourPort "); req.setRequestHeader("content-type", "application/x-www-form-urlencoded"); req.send("payload="+ myPayload); 

When the browser has established a HTTPS connection with the SpringBoot server, you have the problem that the browser may display a "Mixed Content Warning" as a secured web context (your web page) accesses a resource via HTTP. 当浏览器与SpringBoot服务器建立HTTPS连接时,由于安全的Web上下文(您的网页)通过HTTP访问资源,浏览器可能会显示“混合内容警告”。

Fortunately, lately modern browsers like Chrome, Firefox, Edge and Safari implement the W3C Secure Contexts working draft and thus consider access to 127.0.0.1 as potentially trustworthy: no Mixed Content warning. 幸运的是,最近的现代浏览器(例如Chrome,Firefox,Edge和Safari)实现了W3C Secure Contexts工作草案 ,因此将对127.0.0.1的访问视为潜在可信任:没有混合内容警告。

Still if you bring this into production you must be aware that your desktop application is now vulnerable against XSS attacks. 但是,如果将其投入生产,则必须知道您的桌面应用程序现在容易受到XSS攻击。

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

相关问题 将php数据发送到Java桌面应用程序 - Sending php data to java desktop application 带浏览器的JAVA桌面应用程序和HTML - JAVA Desktop Application & HTML with browser 从Java桌面应用程序向Google Analytics发送统计信息 - Sending statistics to Google Analytics from java desktop application 将数据从android传输到Java桌面应用程序 - Transfer data from android to java desktop application 从GPRS模块向桌面应用程序中的MySQL数据库发送数据 - Sending data to a MySQL database in a desktop application from a GPRS module 我们可以从Java桌面应用程序读取浏览器保存的cookie吗? - Can we read browser saved cookies from a java desktop application? 如何从 Java 桌面应用程序代码打开新的浏览器窗口? - how to open new browser window from java desktop application code? 在java中将推送通知发送到桌面应用程序 - Sending Push Notification to Desktop application in java 将数据从Java程序发送到油脂应用程序? - Sending data to greasemonkey application from java program? 将数据从树莓派发送到 java 应用程序? - Sending data from a raspberry pi to a java application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM