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怎么编写文件下载代码

下面是我写的一个小例子,下载远程文件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页面如何实现下载文档

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页面点击超链接弹出文件下载,代码如下:

%

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文件,根据需求修改即可。

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/279107.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-20 15:02
下一篇 2024-12-20 15:02

相关推荐

  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python字符串宽度不限制怎么打代码

    本文将为大家详细介绍Python字符串宽度不限制时如何打代码的几个方面。 一、保持代码风格的统一 在Python字符串宽度不限制的情况下,我们可以写出很长很长的一行代码。但是,为了…

    编程 2025-04-29
  • vue下载无后缀名的文件被加上后缀.txt,有后缀名的文件下载正常问题的解决

    本文旨在解决vue下载无后缀名的文件被加上后缀.txt,有后缀名的文件下载正常的问题,提供完整的代码示例供参考。 一、分析问题 首先,需了解vue中下载文件的情况。一般情况下,我们…

    编程 2025-04-29
  • Python基础代码用法介绍

    本文将从多个方面对Python基础代码进行解析和详细阐述,力求让读者深刻理解Python基础代码。通过本文的学习,相信大家对Python的学习和应用会更加轻松和高效。 一、变量和数…

    编程 2025-04-29
  • 如何在Java中拼接OBJ格式的文件并生成完整的图像

    OBJ格式是一种用于表示3D对象的标准格式,通常由一组顶点、面和纹理映射坐标组成。在本文中,我们将讨论如何将多个OBJ文件拼接在一起,生成一个完整的3D模型。 一、读取OBJ文件 …

    编程 2025-04-29
  • Python程序文件的拓展

    Python是一门功能丰富、易于学习、可读性高的编程语言。Python程序文件通常以.py为文件拓展名,被广泛应用于各种领域,包括Web开发、机器学习、科学计算等。为了更好地发挥P…

    编程 2025-04-29
  • Python中读入csv文件数据的方法用法介绍

    csv是一种常见的数据格式,通常用于存储小型数据集。Python作为一种广泛流行的编程语言,内置了许多操作csv文件的库。本文将从多个方面详细介绍Python读入csv文件的方法。…

    编程 2025-04-29
  • 为什么用cmd运行Java时需要在文件内打开cmd为中心

    在Java开发中,我们经常会使用cmd在命令行窗口运行程序。然而,有时候我们会发现,在运行Java程序时,需要在文件内打开cmd为中心,这让很多开发者感到疑惑,那么,为什么会出现这…

    编程 2025-04-29
  • Python zipfile解压文件乱码处理

    本文主要介绍如何在Python中使用zipfile进行文件解压的处理,同时详细讨论在解压文件时可能出现的乱码问题的各种解决办法。 一、zipfile解压文件乱码问题的根本原因 在P…

    编程 2025-04-29
  • Python将矩阵存为CSV文件

    CSV文件是一种通用的文件格式,在统计学和计算机科学中非常常见,一些数据分析工具如Microsoft Excel,Google Sheets等都支持读取CSV文件。Python内置…

    编程 2025-04-29

发表回复

登录后才能评论