JSP File Download method

Source: Internet
Author: User

<%
Response.setcontenttype (Fileminitype);
Response.setheader ("Location", filename);
Response.setheader ("Cache-control", "max-age=" + cacheTime);
Response.setheader ("Content-disposition", "attachment; Filename= "+ filename); FileName should be encoded (UTF-8)
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 implement file downloads. 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 ();
POPFile (Srcfile, OutputStream))//write files with srcfile file path to OutputStream.
catch (IOException e) {
E.printstacktrace ();
}
}
JApplet End Sample
URLConnection con;
try {
con = url.openconnection ();//url is the URL of the called servlet such as Http://localhost:8080/sendDateSevlet.do
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);
String Localfilepath = localstr + filename;//localfilepath local path, localstr file folder, filename local filename
if (Savefilsavefilee (Localfilepath,pminputstream)) {//Method Savefilsavefilee writes the input stream pminputstream to the file Localfilepath.
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 URL of the called servlet such as Http://localhost:8080/sendDateSevlet.do
Con.setusecaches (FALSE);
Con.setdoinput (TRUE);
Con.setdooutput (TRUE);
Con.setrequestproperty ("Content-type",
"Application/octet-stream");

OutputStream out = Con.getoutputstream ();
String Localfilepath = localstr + filename; Localfilepath Local Path, localstr file folder, filename local filename
Getoutputstream (localfilepath,out)//File Getoutputstream is the file Localfilepath written to the output stream 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 ();
WriteFile (Srcfile, InputStream)//To save the input stream InputStream to a file with a file path of Srcfile
catch (IOException e) {
E.printstacktrace ();
}
}//End service
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.