简体   繁体   English

为什么我的applet中的CSV文件不起作用?

[英]Why doesn't the CSV file in my applet work?

I created an applet that requires a CSV file for information. 我创建了一个小程序,需要一个CSV文件来获取信息。 The way the applet works, is that there is a text field in which you type in your zip code, then you press a button. 小程序的工作方式是,有一个文本字段,您可以在其中键入您的邮政编码,然后按一个按钮。 That causes the program to parse through the CSV file which contains a latitude and longitude, then display the latitude and longitude on a JLabel in the applet. 这导致程序解析包含纬度和经度的CSV文件,然后在applet中的JLabel上显示纬度和经度。

When I created it, I debugged it and tested it, so I know it works on my desktop (when running in eclipse). 当我创建它时,我调试它并测试它,所以我知道它适用于我的桌面(在eclipse中运行时)。 The problem is when I put in on the web, it displays but can't do anything, meaning it is just an applet with a text field and a button, but when you press the button, nothing happens. 问题是,当我放入网络时,它显示但无法执行任何操作,这意味着它只是一个带有文本字段和按钮的小程序,但是当您按下按钮时,没有任何反应。 I know that it is not my ActionListener , because it works on the desktop, but I must be doing something wrong with the HTML of it. 我知道它不是我的ActionListener ,因为它可以在桌面上运行,但我必须对它的HTML做错。 The name of the CSV file is zips.csv . CSV文件的名称是zips.csv The name of the main class is main.class (or main.java ) and the action listener is myActionListener.class (or myActionListener.java ). 主类的名称是main.class (或main.java ),动作监听器是myActionListener.class (或myActionListener.java )。

Here is the HTML that I am using for it right now: 这是我现在使用的HTML:

<applet archive="sites/default/files/myApplet.jar" code="main.class" width="500" height="200"> 
</applet>

Revision: 修订:

Romething else that someone recommended to me was to create a php script that will parse the csv file, and than have that return a value to the java applet. 有人向我推荐的其他东西是创建一个解析csv文件的php脚本,而不是将该值返回给java applet。 My knowledge of PHP ls limited, so I was wonder if someone could tell me how I could go about doing this, or telling me where I can learn how to do this. 我对PHP的了解有限,所以我很想知道是否有人可以告诉我如何做到这一点,或者告诉我在哪里可以学习如何做到这一点。

CsvReader products = new CsvReader("zips.csv");

My crystal ball tells me that CsvReader presumes the String to represent a File object. 我的水晶球告诉我, CsvReader假定String代表一个File对象。 It might also have another constructor that accepts an URL . 它可能还有另一个接受URL构造函数。

A sand-boxed applet cannot access File objects, and a trusted applet can only access File objects on the computer of the end user. 沙盒小程序无法访问File对象,受信任的applet只能访问最终用户计算机上的File对象。 That is useless to this applet. 这对这个小程序没用。 If the API has a constructor that accepts an URL , that is the one to use here. 如果API有一个接受URL的构造函数,那么就是这里使用的URL Something like: 就像是:

URL url = this.getClass().getResource("zips.csv");
//CsvReader products = new CsvReader(url);
InputStream is = url.openStream();
CsvReader products = new CsvReader(is);

A constructor that accepts an InputStream is even more versatile, and only a line longer. 接受InputStream构造函数更加通用,只有一行更长。

If the CsvReader accepts neither of URL or InputStream , I suggest you find another API. 如果CsvReader既不接受URL CsvReader接受InputStream ,我建议您找到另一个API。 One that was not written by amateurs. 一个不是业余爱好者写的。

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

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