jsp 如何实现文件上传和下载功能?

jsp 如何实现文件上传和下载功能?,第1张

上传:

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/msdownloadcharset=gbk")

//文件下载后的默认保存名及打开方式

String contentDisposition = "attachmentfilename=" + "java.txt"

response.setHeader("Content-Disposition",contentDisposition)

//设置下载长度

response.setContentLength(bt.length)

ServletOutputStream sos = response.getOutputStream()

sos.write(bt)

return null

<%@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","attachmentfilename=" + 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

}

}

%>

可以采用:

1、RequestDispatcher的方式进行;

2、采用文件流输出的方式下载。

1、采用RequestDispatcher的方式进行

jsp页面中添加如下代码:

<%

response.setContentType("application/x-download")//设置为下载application/x-download

String filedownload = "/要下载的文件名"//即将下载的文件的相对路径

String filedisplay = "最终要显示给用户的保存文件名"//下载文件时显示的文件保存名称

filenamedisplay = URLEncoder.encode(filedisplay,"UTF-8")

response.addHeader("Content-Disposition","attachmentfilename=" + filedisplay)

try

{

RequestDispatcher dis = application.getRequestDispatcher(filedownload)

if(dis!= null)

{

dis.forward(request,response)

}

response.flushBuffer()

}

catch(Exception e)

{

e.printStackTrace()

}

finally

{

}

%>

2、采用文件流输出的方式下载

<%@page language="java" contentType="application/x-msdownload"pageEncoding="gb2312"%><%

//关于文件下载时采用文件流输出的方式处理:

//加上response.reset(),并且所有的%>后面不要换行,包括最后一个;

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

response.setContentType("application/x-download")

String filedownload = "想办法找到要提供下载的文件的物理路径+文件名"

String filedisplay = "给用户提供的下载文件名"

filedisplay = URLEncoder.encode(filedisplay,"UTF-8")

response.addHeader("Content-Disposition","attachmentfilename=" + filedisplay)

OutputStream outp = null

FileInputStream in = null

try

{

outp = response.getOutputStream()

in = new FileInputStream(filenamedownload)

byte[] b = new byte[1024]

int i = 0

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

{

outp.write(b, 0, i)

}

outp.flush()

}

catch(Exception e)

{

System.out.println("Error!")

e.printStackTrace()

}

finally

{

if(in != null)

{

in.close()

in = null

}

if(outp != null)

{

outp.close()

outp = null

}

}

%>

在wsad里面写JSP文件下载,总是出现这个异常,getOutputStream() has already been called for this response,输出流已经被调用了.

上网查半天终于明白一点,JSP早下载文件的时候用到了OutputStream,而在Application Server在处理编译jsp时对于%>和<%之间的内容一般是原样输出,而且默认是PrintWriter.


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/8125475.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存