File remote Download and view

Source: Internet
Author: User


Recently, the company asked to do a fax download to see because previously did not do a similar remote direct download to the client, so in the Internet to find relevant information, the results of a uniform, almost all of you copied me I copied you, are servers downloaded to the server and not the direct server download to the user browser side, the study of the day, Write an HTTP download and the IIS Remote View code as follows:

(a) front desk: JS access servlet

View Fax showfile:function (time,num) {   window.open ("/faxservlet?creattime=" +time+ "&faxnum=" +num+ "&tag= 1 "," _blank ");} Download Fax downloadfile:function (time,num) {window.open ("/faxservlet?creattime=" +time+ "&faxnum=" +num+ "&tag=2 "," _blank ");}

(ii) Backstage: servlet processing

1. First get the database address by reading the configuration file

               Properties Props = new properties (); InputStream fis = FaxServlet.class.getResourceAsStream ("/jdbc.properties"); Props.load (FIS); String Servurl = Props.getproperty ("sqlserverjdbc.addr");
2. Get data from the foreground

  String creattime = Request.getparameter ("Creattime"); String faxnum = Request.getparameter ("Faxnum");//tag flag 1 for View 2 for download string tag = Request.getparameter ("tag");
3, query the database to get the file name to view or download the code is omitted

4, to view or download to operate

                    if (Tag.equals ("1")) {//Set property here inline is intended to set the file to open in the browser     &N bsp;           response.setheader ("content-disposition", "inline; Filename= "                         + filename);                 Request.setattribute ("Url_1", Servurl + filename);                 request.getrequestdispatcher (Jsp_url) forward (request, response);            }//Download             if (Tag.equals ("2")) {                 url url = new URL (servurl + filename);                httpurlconnection conn = (Httpurlconnection) url.openconnection ();                 Conn.connect ();                bufferedinputstream ins = New Bufferedinputstream (Conn.getinputstream ());                 response.reset ();//set to download files as streams                 response.setcontenttype ("Application/octet-stream");//Set attachment file as an attachment download                  response.setheader ("Content-disposition",                          "attachment; Filename= "+ filename";                int i;                byte[] buffer = new BYTE[4 * 1024];                while ((i = ins.read (buffer)) > 0) {   &nbsp ;                response.getoutputstream (). Write ( Buffer, 0, i);                }     & nbsp          ins.close ();                 response.getoutputstream (). Close ();                 conn.disconnect ();            }

5, check the fax need to determine whether there is a plug-in can directly open the fax file

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>//clssid is the plugin's ID <object width= "100%" height= "100%" id=obj1classid= "clsid:106e49cf-797a-11d2-81a2-00e02c015623" ><param name=src value= "<%= Request.getattribute ("Url_1")%> "><param name=" Mousemode "value=" Pan "/><a href="/alternatiffx-1_8_ 2.zip "> Please download plugin </a></object><script type=" Text/javascript ">if (document.all.obj1.object==null) {alert ("Please download Plugin");} </script>



File remote Download and view

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.