Downloading the Andriod file includes servers (servers)

Source: Internet
Author: User

Downloading the Andriod file includes servers (servers)

This article continues to explain the andriod network problems. This article mainly builds a server on Eclipse.

In the previous article, the tomcat version 7.0 and servlet 3.0 do not need to register xml files under web. xml.

The content in this article is relatively simple. The server reads a pdf file (other files are also supported) on the disk and returns a streaming file to the client.

The server code is as follows:

 

Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// String pathString = F: \ job \ embedded system development example .docx Based on arm11; // file path to be downloaded, this document uses the absolute path String pathString = D: \ My Documents \ Desktop \ new folder (3) \ new concept 3 \ old concepts English lesson 3; InputStream inputStream = null; OutputStream outputStream = null; file file = new File (pathString); inputStream = new BufferedInputStream (new FileInputStream (file ));/ /Set to stream download response. setContentType (application/octet-sream); // you can specify the response size. setContentLength (int) file. length (); response. setHeader (Content-type, text/html; charset = UTF-8); // the meaning of this sentence is to tell the servlet to use the UTF-8 for transcoding, instead of using the default ISO8859 response. setCharacterEncoding (UTF-8); String fileName = file. getName (); // download response from the browser. addHeader (Content-Disposition, attachment; filename = + new String (fileName. getBytes (gb2312), ISO8859-1 ); OutputStream = new BufferedOutputStream (response. getOutputStream (); // buffer size: 1024 byte [] s = new byte [10240]; int len = 0; // avoid the last data read, data smaller than B is filled, causing data inaccuracy while (len = inputStream. read (s ))! =-1) {outputStream. write (s, 0, len);} if (inputStream! = Null) {inputStream. close () ;}response. flushBuffer (); if (outputStream! = Null) {outputStream. close ();}}


Doget calls the dopost method.

 

 

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.