William W
William W

Reputation: 1806

Prevent applet from caching downloaded files

I have an applet that retrieves files for printing from the network, and I occasionally have a problem with it returning a cached version of the file instead of the actual if it has changed.

For example:

URL http = new URL(url +"/"+ m_printfile.get(i));
Doc myDoc = new SimpleDoc(http, myFormat, das);
DocPrintJob job = service.createPrintJob();
job.print(myDoc, aset);

Since I'm using SimpleDoc to print the file, I haven't found a way to use the URLConnection object, which is the only way I've seen to disable caching a file.

Is there a way to do it with the URL object, or is there a way I can pass the URLConnection to SimpleDoc?

Upvotes: 0

Views: 163

Answers (1)

MeNa
MeNa

Reputation: 1487

1) You can pass URLConnection to SimpleDoc like this:

URL http = new URL(url +"/"+ m_printfile.get(i));
URLConnection conn = http.openConnection();
conn.setUseCaches(false);
conn.connect();
Doc myDoc = new SimpleDoc(conn.getInputStream(), myFormat, das);
DocPrintJob job = service.createPrintJob();
job.print(myDoc, aset);

2) Dirty way to avoid caching is to add a random-irrelevant data like this:

URL http = new URL(url +"/"+ m_printfile.get(i) +"?z="+Math.random() );

such, each time it's looks like different URL, so the cache not impact...

P.S. Your second line seems to me wierd.. (Doc myDoc = new SimpleDoc(http, myFormat, das)). it's really works for you? i mean, http is not an InputStream object.. am i wrong?

Upvotes: 1

Related Questions