Java downloads the xls file from the server to the client

Source: Internet
Author: User

Check the online code and write a download xls file to the client's JSP page. You only need to pass the server's file address to this JSP page to download the file to the client.

Code:
  1. <% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
  2. <% @ Taglib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %>
  3. <% @ Page import = "Java. Io. *" %>
  4. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  6. <Head>
  7. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  8. <Link href = "styles/basic.css" rel = "stylesheet" type = "text/CSS"/>
  9. <Title> download </title>
  10. </Head>
  11. <%
  12. Response. setcharacterencoding ("gb2312 ");
  13. Request. setcharacterencoding ("gb2312 ");
  14. If (request. getparameter ("file ")! = NULL ){
  15. Outputstream OS = NULL;
  16. FileInputStream FCM = null;
  17. Try {
  18. String file = request. getParameter ("file ");
  19. If (! (New File (file). exists ()){
  20. System. out. println ("no file ");
  21. Return;
  22. }
  23. System. Out. println ("file name:" + file );
  24. OS = response. getoutputstream ();
  25. Response. setheader ("content-disposition", "attachment; filename =" + file );
  26. Response. setcontenttype ("application/vnd. MS-excel"); // The content varies with the file type.
  27. Byte temp [] = new byte [1000];
  28. FS = new fileinputstream (File );
  29. Int n = 0;
  30. While (n = FS. Read (temp ))! =-1 ){
  31. OS. Write (temp, 0, N );
  32. }
  33. } Catch (exception e ){
  34. Out. Print ("error ");
  35. } Finally {
  36. If (OS! = NULL)
  37. OS. Close ();
  38. If (FS! = NULL)
  39. FCM. Close ();
  40. }
  41. Out. Clear ();
  42. Out = pagecontext. pushbody ();
  43. }
  44. %>
  45. <Form action = "" method = "Post">
  46. <Select name = "file">
  47. <Option value = "D: // test // test.xls">
  48. Lengshan sky_snow
  49. </Option>
  50. </SELECT>
  51. <Input type = "Submit"/>
  52. </Form>
  53. </Html>

 

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.