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

本文目錄一覽:

jsp實現文件的下載

%@pagelanguage=”java” import=”java.io.*,java.net.*” contentType=”application/x-msdownload” pageEncoding=”UTF-8″%%

//關於文件下載時採用文件流輸出的方式處理:

//加上response.reset(),並且所有的%後面不要換行,包括最後一個;

String url = request.getParameter(“url”);

System.out.print(url);

int k = url.lastIndexOf(“\\”);

String url1=url.substring(k+1,url.length());

response.reset();//可以加也可以不加

response.setContentType(“application/x-download”);

String filedownload = url;

String filedisplay = url1;

filedisplay = URLEncoder.encode(filedisplay,”UTF-8″);

response.addHeader(“Content-Disposition”,”attachment;filename=” + filedisplay);

OutputStream outp = null;

FileInputStream in = null;

try

{

outp = response.getOutputStream();

in = new FileInputStream(filedownload);

byte[] b = new byte[1024];

int i = 0;

while((i = in.read(b)) 0)

{

outp.write(b, 0, i);

}

out.clear();

out = pageContext.pushBody();

outp.flush();

}

catch(Exception e)

{

System.out.println(“Error!”);

}

finally

{

if(in != null)

{

in.close();

in = null;

}

if(outp != null)

{

outp.close();

outp = null;

}

}

%

jsp 如何實現文件上傳和下載功能?

上傳:

MyjspForm mf = (MyjspForm) form;// TODO Auto-generated method stub

FormFile fname=mf.getFname();

byte [] fn = fname.getFileData();

OutputStream out = new FileOutputStream(“D:\\”+fname.getFileName());

Date date = new Date();

String title = fname.getFileName();

String url = “d:\\”+fname.getFileName();

Upload ul = new Upload();

ul.setDate(date);

ul.setTitle(title);

ul.setUrl(url);

UploadDAO uld = new UploadDAO();

uld.save(ul);

out.write(fn);

out.close();

下載:

DownloadForm downloadForm = (DownloadForm)form;

String fname = request.getParameter(“furl”);

FileInputStream fi = new FileInputStream(fname);

byte[] bt = new byte[fi.available()];

fi.read(bt);

//設置文件是下載還是打開以及打開的方式msdownload表示下載;設置字湖集,//主要是解決文件中的中文信息

response.setContentType(“application/msdownload;charset=gbk”);

//文件下載後的默認保存名及打開方式

String contentDisposition = “attachment; filename=” + “java.txt”;

response.setHeader(“Content-Disposition”,contentDisposition);

//設置下載長度

response.setContentLength(bt.length);

ServletOutputStream sos = response.getOutputStream();

sos.write(bt);

return null;

jsp頁面如何實現下載文檔

jsp頁面下載文檔是在jsp中有一個a標籤 ,當用戶點擊a標籤的時候下載文件。

一般採用href屬性直接指向一個服務器地址,只要鏈接的文件存在,就會給出彈出保存對話框.

點擊a標籤 先執行onclick事件,再請求href中指向的地址。

前端jsp:

a href=”#” onclick=”javascript:downloadtest(‘${app.id}’)” id=”pluginurl” style=”color: #83AFE2;text-decoration:underline;”/a

然後在js中:

function downloadtest(id){

var url = “%=request.getContextPath()%/app/download” + “/” + id;

$(“#pluginurl”).attr(“href”,url);

}

後台處理下載邏輯的java代碼:

/**

* 下載文件

* @param id appid

* @param response

*/

@RequestMapping(value=”/download/{id}”)

public void download(@PathVariable String id, HttpServletResponse response){

String filepath = “”;

Result result = appService.getAppById(id);

App app = (App) result.getMap().get(“app”);

if(app == null){

return;

}

filepath = app.getUrl();

File file = new File(filepath);

InputStream inputStream = null;

OutputStream outputStream = null;

byte[] b= new byte[1024];

int len = 0;

try {

inputStream = new FileInputStream(file);

outputStream = response.getOutputStream();

response.setContentType(“application/force-download”);

String filename = file.getName();

filename = filename.substring(36, filename.length());

response.addHeader(“Content-Disposition”,”attachment; filename=” + URLEncoder.encode(filename, “UTF-8”));

response.setContentLength( (int) file.length( ) );

while((len = inputStream.read(b)) != -1){

outputStream.write(b, 0, len);

}

} catch (Exception e) {

e.printStackTrace();

}finally{

if(inputStream != null){

try {

inputStream.close();

inputStream = null;

} catch (IOException e) {

e.printStackTrace();

}

}

if(outputStream != null){

try {

outputStream.close();

outputStream = null;

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-05 10:21
下一篇 2024-12-05 10:21

相關推薦

發表回復

登錄後才能評論