Servlet Download

xiaoxiao2021-03-04  50

/ *

* Download.java

* /

Package com.zy.oa.util;

Import java.io. *;

Import java.text. *;

Import java.util. *;

Import javax.servlet. *;

Import javax.servlet.http. *;

Import java.net. *;

/ **

* Implement download function

* /

Public class download {

Public void Download (httpservletResponse response,

String headname, string fileurl) throws exception {

Logger.log ("Begin Download" HeadName);

IF (". jpg") || (HeadName.endSwith (". gif"))) {

// no need setting content type and header

} else if (HeadName.endSwith (". txt")) {

Response.setContentType ("text; charset = GB2312);

Response.setHeader ("Content-Disposition",

"attachment; filename =" headname);

} else {

Response.setContentType ("Application / Doc; Charset = GB2312);

Response.setHeader ("Content-Disposition",

"attachment; filename =" headname);

}

String fileurl = fileurl;

Logger.log (fileurl);

IF (fileurl == NULL)

Return;

Try {

File file = new file (fileurl);

FileInputStream Bis = New FileInputStream (File);

OutputStream Bos = response.getOutputStream ();

BYTE [] BUFF = New byte [1024];

INT readcount = 0;

INT i = 0;

Readcount = Bis.read (BUFF);

While (READCOUNT! = -1) {

Bos.write (buff, 0, readcount);

Readcount = Bis.read (BUFF);

}

Logger.log ("Read Finished!");

IF (bis! = null)

bis.close ();

IF (bos! = null)

Bos.close ();

} catch (exception e) {

E.PrintStackTrace ();

Throw e;

}

}

}

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

New Post(0)