js| Download
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 (); 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 srcfile file path 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.