Simple implementation of the download function--jsp,servlet

Source: Internet
Author: User

Just to the company, the day before yesterday gave me the first small task, although just do a simple download function.

But at first it took a while to understand what the mentor meant, and it took a few of the company's library classes to get it done in an afternoon.

There are about three ways

1. Directly use the a tag to jump to the target file at the specified location. <a href= "Doc/help.txt"/></a> (file is located under Doc folder Help.txt) (this method is not recommended, and this jump transfer directly opens the file)

2.jsp jump and then write the download function directly inside the JSP

<a href= "download.jsp"/></a>

Use the byte stream method in download.jsp to provide download

<% @page language= "java" contenttype= "Application/x-msdownload" pageencoding= "gb2312"%><%
File stream output is processed when downloading files:
Plus Response.reset (), anddo not wrap all the%> behind, including the last one .

Response.reset ();//Can be added or not added
Response.setcontenttype ("Application/x-download");
String filedownload = "Try to find the physical path of the file to provide the download + filename";
String filedisplay = "Download file name provided to user";
Filedisplay = Urlencoder.encode (Filedisplay, "UTF-8");
Response.AddHeader ("Content-disposition", "attachment;filename=" + filedisplay);

OutputStream OUTP = null;
FileInputStream in = null;
Try
{
OUTP = Response.getoutputstream ();
in = new FileInputStream (filenamedownload);

Byte[] B = new byte[1024];
int i = 0;

while ((i = In.read (b)) > 0)
{
Outp.write (b, 0, I);
}
Outp.flush ();
}
catch (Exception e)
{
System.out.println ("error!");
E.printstacktrace ();
}
Finally
{
if (in = null)
{
In.close ();
in = null;
}
if (OUTP! = null)
{
Outp.close ();
OUTP = null;
}
}
%>

I think this method can be used when there are only a few download requirements for the site

3 using Servlets to control downloads

Xml

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app id= "Webapp_9" version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_ 2_4.xsd ">

<servlet>
<servlet-name>DownloadFile</servlet-name>
<servlet-class>com.suntek.web.servlet.DownloadServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>DownloadFile</servlet-name>
<url-pattern>/downloadfile</url-pattern>
</servlet-mapping>


</web-app>

Servlet

Package com.suntek.web.servlet;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.OutputStream;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Downloadservlet extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws IOException, Servletexception {
OutputStream o = Response.getoutputstream ();
byte b[] = new byte[1024];
The file to download.
File Fileload = new file ("f:/", "Eula.txt");
The dialogbox of download file.
Response.setheader ("Content-disposition", "Attachment;filename="
+ "Help.txt");
Set the MIME type.
Response.setcontenttype ("Text/plain");
Get the file length.
Long filelength = Fileload.length ();
String length = string.valueof (filelength);
Response.setheader ("Content_length", Length);
Download the file.
FileInputStream in = new FileInputStream (fileload);
int n = 0;
while ((n = in.read (b))! =-1) {
O.write (b, 0, N);
}
}

public void DoPost (HttpServletRequest request, httpservletresponse response) throws IOException, Servletexception {
Doget (request, response);
}
}

In this servlet use a hard-coded way to match the path of the file, in practice to achieve dynamic download, from the path, and note that the file suffix must be set to succeed, that is

Response.setcontenttype ("Text/plain");

To set the downloaded file by default what software to open, ContentType online has a list, you can search by themselves

The next article should be written multi-threaded download and implementation of the breakpoint download (just do this download function, the knowledge of this piece of the way to learn it)

Simple implementation of the download function--jsp,servlet

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.