简体   繁体   中英

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

I created an applet that requires a CSV file for information. 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.

When I created it, I debugged it and tested it, so I know it works on my desktop (when running in 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. The name of the CSV file is zips.csv . The name of the main class is main.class (or main.java ) and the action listener is myActionListener.class (or myActionListener.java ).

Here is the HTML that I am using for it right now:

<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. 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.

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

My crystal ball tells me that CsvReader presumes the String to represent a File object. It might also have another constructor that accepts an 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. That is useless to this applet. If the API has a constructor that accepts an URL , that is the one to use here. 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.

If the CsvReader accepts neither of URL or InputStream , I suggest you find another API. One that was not written by amateurs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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