简体   繁体   English

在 HTML 中调用 Java 方法

[英]Call Java method in HTML

This is what I am trying to do:这就是我想要做的:

I have written a Java program which requires two file paths as input parameters.我写了一个 Java 程序,它需要两个文件路径作为输入参数。 The code is working fine and accurate.该代码工作正常且准确。 The problem here is that every time I need to give the complete file path in command prompt.这里的问题是每次我需要在命令提示符下提供完整的文件路径。 So what I thought is to link this with a HTML page which will give you two browse button in order to browse file.所以我的想法是将其与 HTML 页面链接,该页面将为您提供两个浏览按钮以浏览文件。

Now my problem is how to give the two browsed file path in a Java method.现在我的问题是如何在 Java 方法中给出两个浏览的文件路径。 I had written entire Java code in batch file as of now.到目前为止,我已经在批处理文件中编写了整个 Java 代码。 I tried using <input type=file> in HTML, but how to pass this value in Java that I am unaware of?我尝试在 HTML 中使用<input type=file> ,但是如何在我不知道的 Java 中传递这个值?

You can build a servlet for this.您可以为此构建一个servlet Once you browse for files in html submit your html form to that servlet which does operation on that files.一旦您浏览 html 中的文件,请将您的 html 表单提交给对该文件执行操作的 servlet。 To point to that servlet give its relative url in action parameter of form tag.要指向该 servlet,请在表单标签的 action 参数中给出其相关 url。 Or if you do not want to go in web development using server then use Applet they can also be a medium for communication between java and browser.或者,如果您不想在 web 开发中使用服务器 go,那么使用 Applet 它们也可以作为 java 和浏览器之间的通信媒介。

Make an entirely Java UI, using Swing/AWT.使用 Swing/AWT 制作一个完整的 Java UI。

You cannot use <input type="file" .您不能使用<input type="file" My initial thought was that an applet will do, but check this answer .我最初的想法是一个小程序就可以了,但请检查这个答案 You can still use an applet, but you should browse the file from java, not from the browser.您仍然可以使用小程序,但您应该从 java 而非浏览器中浏览文件。

Why not replace the web page with a Java GUI - Swing perhaps?为什么不用 Java GUI - Swing 替换 web 页面? You could use a javax.swing.JFileChooser to browse the file system, get a filepath, and run the rest of your program with that.您可以使用javax.swing.JFileChooser来浏览文件系统,获取文件路径,然后运行程序的 rest。 It sounds like this would satisfy your requirements.听起来这将满足您的要求。

If this application is to be used on the desktop you don't need servlets or HTML, simply pop up two JFileChoosers and select the files using these.如果要在桌面上使用此应用程序,则不需要 servlets 或 HTML,只需使用这些文件弹出两个 JFileChoosers 和 select 文件。

The code to do this is:执行此操作的代码是:


import java.io.File;

import javax.swing.JFileChooser;

public class Main {
    public static void main(String[] args) {
        JFileChooser fc = new JFileChooser();
        fc.showOpenDialog(null);
        File f1 = fc.getSelectedFile();
        fc.showOpenDialog(null);
        File f2 = fc.getSelectedFile();
        System.out.println(f1.getName());
        System.out.println(f2.getName());
        /* make sure to make some checks to the files f1, f2 */
    }
}

Have fun: :D玩得开心::D

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

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