Use JSP to implement the function of downloading files directly

xiaoxiao2021-03-06  106

<%

/ / Sometimes the user wants to download the file, but this file type is associated with the browser in the system, and the result is turned on in IE.

// Common Word, Excel, PDF, etc. Therefore, transfer the file into a data stream to make the browser do not know the purpose of the file type.

// EXAMPLE:

// Download Image

String root = GetServletContext (). GetRealPath ("/");

String path = Request.getParameter ("path");

String name = Request.getParameter ("name");

Response.setContentType ("unknown");

// Note that if the attachment in the following line code is removed; then the IE will automatically open the file.

Response.addheader ("Content-Disposition", "Attachment; FileName = /" " Name " / "");

Try

{

Java.io.outputstream OS = response.getOutputStream ();

Java.io.fileinputStream Fis = new java.io.fileInputStream (root path name);

Byte [] b = new byte [1024];

INT i = 0;

While ((i = fis.read (b))> 0)

{

Os.Write (B, 0, I);

}

fis.close ();

Os.flush ();

Os.Close ();

}

Catch (Exception E)

{}

%>

转载请注明原文地址:https://www.9cbs.com/read-103766.html

New Post(0)