jsp下載代碼,jsp下載文件代碼

本文目錄一覽:

JSP通過超鏈接下載文件

JSP頁面點擊超鏈接彈出文件下載,代碼如下:

%

String path = request.getContextPath();

String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;

%

//然後

a href =”%= basePath %/upload/aa.doc }” target=”_blank”下nbsp;nbsp;載/a

註:%= basePath %獲取部署JSP項目的根目錄,/upload/aa.doc/是根目錄uploadaa.doc文件,根據需求修改即可。

用jsp怎麼編寫文件下載代碼

下面是我寫的一個小例子,下載遠程文件urlString,到本地文件localFile.

成功返回True,不成功返回False.

把這代碼插入到你JSP中用到的地方就OK了:)

public boolean downLoadFile(String urlString, String localFile) {

URL url;

byte[] buffer = new byte[512];

int size = 0;

boolean success = false;

try {

url = new URL(urlString);

BufferedInputStream stream = new BufferedInputStream(url.openStream());

FileOutputStream fos = new FileOutputStream(localFile);

while ((size = stream.read(buffer)) != -1) {

fos.write(buffer, 0, size);

}

fos.close();

stream.close();

success = true;

}

catch (MalformedURLException e) {

e.printStackTrace();

}

catch (IOException e) {

e.printStackTrace();

}

return success;

}

JSP文件下載代碼怎麼寫?

通過設置 page 的 contentType 可以實現下載功能 。

如 :

%@ page contentType=”application/msword” pageEncoding=”UTF-8″%

那麼打相此 jsp 頁面會彈出保存 *.doc 文檔的對話框 。

這裡列出了更多的設置類型 :

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/159851.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-20 00:16
下一篇 2024-11-20 00:16

相關推薦

發表回復

登錄後才能評論