首页 > 编程知识 正文

以及在jsp下得提交代码下载,jsp实现文件的上传和下载

时间:2023-12-29 13:16:59 阅读:330432 作者:MYZV

本文目录一览:

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页面如何实现下载文档

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

请问谁会文件的上传和下载啊,基于jsp的,直接右击连接,另存为的那种

要用到jspSmartUpload组件,先到网站下载这个组件(或直接搜它),下载解压后,把Web-inf/classes下的文件打成JAR包,放到Tomcat的,lib下,再在你的项目中导入此JAR包。

下面附代码给你,去试试:

jspSmartUpload.html

head

/head

meta http-equiv="Content-Type" content="text/html; charset=gb2312"

body

CENTER

FONT SIZE = 5 COLOR = blue一个关于文件上传的例子/FONT

/CENTER

BR

HR

BR

form method="post" action="jspSmartUpload.jsp" enctype="multipart/form-data"

input type="hidden" name="TEST" value="good"

table width="80%" border="0" align="center"

tr

td1.

input type="FILE" name="FILE1" size="30"

/td

/tr

tr

td2.

input type="FILE" name="FILE2" size="30"

/td

/tr

tr

td

center

brinput type="submit" name="Submit" value="上传"

/center

/td

/tr

/table

/form

/body

/html

jspSmartUpload.jsp

html

head

title文件上传成功/title

/head

%@ page contentType="text/html; charset=gb2312"%

%@ page import="java.io.File,com.jspsmart.upload.*"%

body

CENTER

FONT SIZE = 5 COLOR = blue恭喜您!文件上传成功/FONT

/CENTER

BR

HR

BR

%

//新建一个SmartUpload对象

com.jspsmart.upload.SmartUpload su = new SmartUpload();

// 上传初始化

su.initialize(pageContext);

//上传文件

su.upload();

// 将上传文件全部保存到指定目录

int count = su.save("/upload/");

out.println("成功上传"+count+"个文件!br");

%

p

上传文件的信息如下:

/p

table border=1 align="center"

tr

td文件编号/td

td文件大小(字节)/td

td文件名/td

td文件类型/td

/tr

%

//逐一提取上传文件信息,同时可保存文件。

for(int i=0;isu.getFiles().getCount();i++)

{

com.jspsmart.upload.File file = su.getFiles().getFile(i);

//若文件不存在则继续

if(file.isMissing()) continue;

//显示当前文件信息

%

tr

td%=file.getFieldName()%/td

td%=file.getSize()%/td

td%=file.getFileName()%/td

td%=file.getFileExt()%/td

/tr

%

}%

/table

/body

/html

downloadFile.html

html

head

title下载文件/title

/head

meta http-equiv="Content-Type" content="text/html; charset=gb2312"

body

CENTER

FONT SIZE = 5 COLOR = blue下载文件/FONT

/CENTER

BR

HR

BR

p要下载的文件是:/p

p

test.doc

/p

center

a href="downloadFile.jsp"单击下载/a

/center

/body

/html

downloadFile.jsp

html

head

title文件下载处理页面/title

/head

%@ page contentType="text/html; charset=gb2312"%

%@ page import="com.jspsmart.upload.*"%

body

%

// 新建一个SmartUpload对象

SmartUpload su=new SmartUpload();

// 初始化

su.initialize(pageContext);

// 设定contentDisposition为null以禁止浏览器自动打开文件

su.setContentDisposition(null);

// 下载文件

su.downloadFile("/download/test.doc");

%

/body

/html

Java的jsp页面提交不跳转,怎么自动成下载功能了! 奇怪了?! 请帮我看下,谢谢了。 Jav

看一下addbook2.jsp头部代码,你检查下头部,估计是头部写错了

要不你直接复制下面的,替换一下

%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。