1. The most direct and simplest way is to place the file address directly into a link in the HTML page. The disadvantage is that the path to the file is exposed on the server, and no other controls (such as permissions) can be made on the file download. This does not write the example.
2. On the server side of the file into the output stream, write to response, to response the file to the browser, by the browser to prompt the user is willing to save the file to the local. (for example below)
<%
Response.setcontenttype (Fileminitype);
Response.setheader ("Location", filename);
Response.setheader ("Cache-control", "max-age=" + cacheTime);
FileName should be encoded (UTF-8)
Response.setheader ("Content-disposition", "attachment; Filename= "+ filename);
Response.setcontentlength (filelength);
OutputStream outputstream = Response.getoutputstream ();
InputStream InputStream = new FileInputStream (filepath);
byte[] buffer = new byte[1024];
int i =-1;
while ((i = inputstream.read (buffer))!=-1) {
Outputstream.write (buffer, 0, I);
}
Outputstream.flush ();
Outputstream.close ();
Inputstream.close ();
OutputStream = null;
%>
3. Since it is JSP, there is another way is to use applets to achieve the download file. However, the client first has to trust your applets applet, which accepts the stream of data sent by the servlet and writes to the Local.
Servlet End Sample
public void Service (HttpServletRequest req, httpservletresponse Res)
Throws Servletexception, IOException {
Res.setcontenttype ("Text/plain");
OutputStream outputstream = null;
try {
OutputStream = Res.getoutputstream ();
Writes file path to srcfile file into OutputStream
POPFile (Srcfile, OutputStream));
catch (IOException e) {
E.printstacktrace ();
}
}
JApplet End Sample
URLConnection con;
try {
URL is the web address of the servlet being invoked, such as *.do
con = url.openconnection ();
Con.setusecaches (FALSE);
Con.setdoinput (TRUE);
Con.setdooutput (TRUE);
Con.setrequestproperty ("Content-type",
"Application/octet-stream");
InputStream in = Con.getinputstream ();
Progressmonitorinputstream Pminputstream = new Progressmonitorinputstream
(pane, "Downloading file contents from server", in);
Progressmonitor pmonitor = Pminputstream.getprogressmonitor ();
Pmonitor.setmillistodecidetopopup (3);
Pmonitor.setmillistopopup (3);
Localfilepath Local Path, localstr file folder, filename local filename
String Localfilepath = localstr + filename;
Method Savefilsavefilee is to write the input stream pminputstream to the file Localfilepath
if (Savefilsavefilee (Localfilepath,pminputstream)) {
Openlocalfile (Localfilepath);
}
4. By the way the JApplet upload file code also posted up.
JApplet End Sample
URLConnection con;
try {
con = url.openconnection ();
URL is the web address of the servlet being invoked, such as *.do
Con.setusecaches (FALSE);
Con.setdoinput (TRUE);
Con.setdooutput (TRUE);
Con.setrequestproperty ("Content-type", "Application/octet-stream");
OutputStream out = Con.getoutputstream ();
Localfilepath Local Path, localstr file folder, filename local filename
String Localfilepath = localstr + filename;
File Getoutputstream is to write file Localfilepath to output stream out
Getoutputstream (localfilepath,out);
InputStream in = Con.getinputstream ();
return true;
}catch (IOException e) {
SYSTEM.OUT.PRINTLN ("Error on file!) ");
E.printstacktrace ();
}
Servlet-Side code example
public void Service (HttpServletRequest req, httpservletresponse Res)
Throws Servletexception, IOException {
Res.setcontenttype ("Text/plain");
InputStream inputstream = null;
try {
InputStream = Res.getinputstream ();
Save the input stream InputStream to a file with a file path of Srcfile
WriteFile (Srcfile, InputStream);
catch (IOException e) {
E.printstacktrace ();
}
}//End service
Summary: In the transmission of the file is the form of the stream exists, on the hard disk is the form of the file exists. All we have to do is send streams and read streams through HttpServletRequest and httpservletresponse, or response and request. And the operation of converting a file into a stream or transferring it to a file.