jsp文件下载,jsp文件下载失败

http://www.itjxue.com  2023-01-21 20:05  来源:未知  点击次数: 

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();

}

}

}

}

jsp下载文件前提示是否成功

不能判断。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;

(责任编辑:IT教学网)

更多

推荐Discuz!建站文章