Call URL with session

P

Pif

Hello, I want to save a URL in a file. I open the URL and I get it as
an inputstream that I write on disk.

This is done in the servlet. I can get the session in the parameters,
but what should I do to transmit the session to the server. Here is my
current code :

....
URL url = new URL("..... my url ....");
URLConnection connection = url.openConnection();
InputStream is = url.openStream();
....


Thanks a lot.
 
J

John B. Matthews

Pif said:
Hello, I want to save a URL in a file. I open the URL and I get it as
an inputstream that I write on disk.

This is done in the servlet. I can get the session in the parameters,
but what should I do to transmit the session to the server. Here is my
current code :

...
URL url = new URL("..... my url ....");
URLConnection connection = url.openConnection();
InputStream is = url.openStream();
...

Just read from the URLConnection's InputStream and write to disk:

URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
FileWriter out = new FileWriter("filename");
int c;
while ((c = bis.read()) != -1) {
out.write(c);
}
out.close();
 
A

Arne Vajhøj

John said:
Just read from the URLConnection's InputStream and write to disk:
Yes.

URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
FileWriter out = new FileWriter("filename");
int c;
while ((c = bis.read()) != -1) {
out.write(c);
}
out.close();

But I am not so sure about this exact implementation.

c = bis.read() reads bytes and out.write(c) writes chars.

Arne
 
J

John B. Matthews

Arne Vajhøj said:
But I am not so sure about this exact implementation.

c = bis.read() reads bytes and out.write(c) writes chars.

Good point; thanks. I'm guessing it would it be better to read from an
InputStreamReader, perhaps buffered. Would one try to use the
connection's content type to infer the character set?
 
L

Lothar Kimmeringer

Pif said:
Hello, I want to save a URL in a file. I open the URL and I get it as
an inputstream that I write on disk.

This is done in the servlet. I can get the session in the parameters,
but what should I do to transmit the session to the server. Here is my
current code :

...
URL url = new URL("..... my url ....");
URLConnection connection = url.openConnection();
InputStream is = url.openStream();

You can leave away url.openConnection() or use
connection.getInputStream()

As John said, you simply write it to a file. Instead of a
Writer you can do a binary save:

byte[] buf = new byte[4096];
FileOutputStream fos = new FileOutputStream(filename);
int read;
while ((read = is.read(buf)) != -1){
fos.write(buf, 0, read);
}
fos.close();

Bytewise read and write takes a lot more time than above
blockwise read and write.

That will keep binary data like images or PDF-files intact.
If it is text you need to do the conversion afterwards.

If you need that information you should use url.openConnection()
instead of url.openStream() instead and parse the content-type
header in the response. If you have j2ee or the activation.jar
in your classpath you can do it this way:

MimeType mt = new MimeType(connection.getHeaderField("Content-Type"));
String charset = mt.getParameter("charset");
if (charset == null){
charset = "8859_1";
}

After that you open an InputStreamReader with above charset
and write it to a file with the charset of your choice
(by using OutputStreamWriter with an embedded FileOutputStream,
don't use FileWriter, it used the System-encoding which might
be not what you want).

BTW: It's possible that connection.getContentEncoding() also
returns the charset (and not the transfer-encoding e.g. BASE64),
so you should check that out first.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top