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:
- <% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
- <% @ Taglib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %>
- <% @ Page import = "Java. Io. *" %>
- <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <HTML xmlns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
- <Link href = "styles/basic.css" rel = "stylesheet" type = "text/CSS"/>
- <Title> download </title>
- </Head>
- <%
- Response. setcharacterencoding ("gb2312 ");
- Request. setcharacterencoding ("gb2312 ");
- If (request. getparameter ("file ")! = NULL ){
- Outputstream OS = NULL;
- FileInputStream FCM = null;
- Try {
- String file = request. getParameter ("file ");
- If (! (New File (file). exists ()){
- System. out. println ("no file ");
- Return;
- }
- System. Out. println ("file name:" + file );
- OS = response. getoutputstream ();
- Response. setheader ("content-disposition", "attachment; filename =" + file );
- Response. setcontenttype ("application/vnd. MS-excel"); // The content varies with the file type.
- Byte temp [] = new byte [1000];
- FS = new fileinputstream (File );
- Int n = 0;
- While (n = FS. Read (temp ))! =-1 ){
- OS. Write (temp, 0, N );
- }
- } Catch (exception e ){
- Out. Print ("error ");
- } Finally {
- If (OS! = NULL)
- OS. Close ();
- If (FS! = NULL)
- FCM. Close ();
- }
- Out. Clear ();
- Out = pagecontext. pushbody ();
- }
- %>
- <Form action = "" method = "Post">
- <Select name = "file">
- <Option value = "D: // test // test.xls">
- Lengshan sky_snow
- </Option>
- </SELECT>
- <Input type = "Submit"/>
- </Form>
- </Html>