For the site, the site itself often need to provide some resources or data to download, said that the most original way to download the Web site to provide the download URL. Today, there are several other ways to implement file download, for which method is better this is to see their own needs.
1, the most direct and simplest way is to put the file address directly to the HTML page in a link. 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, examples are as follows:
<%
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 ();
Write files with srcfile file paths to OutputStream
popfile (srcfile, OutputStream));
catch (IOException e) {
e.printstacktrace ();
}
}
JApplet End Sample
URLConnection con;
try {
//url is the URL of the invoked servlet 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 ();
The URL is the URL of the invoked servlet, 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 the file Localfilepath written to the output stream out of
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.
The above is the JSP file downloading function method, hoped that has one kind of method may be suitable for you, helps everybody to solve the JSP file downloading function realization question.