file name of an uploaded file in tomcat

A

AA

I create a servlet in Tomcat that pass a file to the client, like this:

// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();

// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
}

When the client download the file and save it, the name of the file is
the name of the servlet.
The big question is: how can I show a name that I want instead of the
name of the servlet?

Thank you
Alb
 
O

Oliver Wong

AA said:
I create a servlet in Tomcat that pass a file to the client, like this:

// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();

// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
}

When the client download the file and save it, the name of the file is
the name of the servlet.
The big question is: how can I show a name that I want instead of the
name of the servlet?

Send the following HTTP headers:

Content-Disposition: attachment; filename="downloaded.pdf"

- Oliver
 
A

Andrea Desole

AA said:
I create a servlet in Tomcat that pass a file to the client, like this:

// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = response.getOutputStream();

// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
}

When the client download the file and save it, the name of the file is
the name of the servlet.
The big question is: how can I show a name that I want instead of the
name of the servlet?

use the Content-Disposition header:

http://www.onjava.com/pub/a/onjava/excerpt/jebp_3/index3.html
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top