简体   繁体   中英

Java Applet - Download File with Browser

I'm trying create a new file with a Java Applet, but I don't know how send this file to the response output of the browser, such as any typical webpage.

With a Servlet it is easy with javax.servlet.http.HttpServletResponse , but is this possible with a applet?

I'm trying do this without sign the applet or use any servlet.

Don't use an applet for this. Go with the Servlet.

Why do you want to do this inside the applet? It will never be able to write anything to disk if you don't sign it, and it can only communicate with the browser through some Javascript API, not send a file directly. You can combine the functionality in your applet perfectly with Servlets, and direct the browser to any relevant page:

AppletContext a = getAppletContext();
URL url = new URL(link_to_your_servlet);
a.showDocument(url,"_blank");

That will open a new window in the browser, and download the file.

An applet is basically just a JAR file which you put on your web server and then you add a JNLP description so the browser knows what to do. Think of it as a complex HTML page because it doesn't go into WEB-INF but besides the other files for the browser (HTML, external JavaScript, images, CSS, ...)

This article from Oracle describes the steps.

An applet can request resources from the web server it came from, eg images - Applet.getImage() or fetch other files like so:

URL url = new URL("myfile.txt");
URLConnection uc = url.openConnection();
InputStream in = new BufferedInputStream(uc.getInputStream());

int d;
while ((c = in.read()) != -1) {
  // do something with d (remember to cast to byte!)
}

You may, or may not, want to use LiveConnect to modify the DOM of the current page. It depends what you are generating. Typically you would want to generate on the server.

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