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

你下载一个jspsmart组件,网上很容桐歼易下到,用法如下,这是我程序的相关片断,供你参考: <%@ page import="com.jspsmart.upload.*" %>

<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />

<%

String photoname="photoname"

// Variables

int count=0 //饥尘 Initialization

mySmartUpload.initialize(pageContext)// Upload

mySmartUpload.upload()

for (int i=0i<mySmartUpload.getFiles().getCount()i++){ // Retreive the current file

com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i) // Save it only if this file exists

if (!myFile.isMissing()) {

java.util.Date thedate=new java.util.Date()

java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd-HH-mm-ss")

photoname = df.format(thedate)

photoname +="."+ myFile.getFileExt()

myFile.saveAs("/docs/docimg/" + photoname)

count ++ } }

%>

<% String title="1"

String author="1"

String content="1"

String pdatetime="1"

String topic="1"

String imgintro="1"

String clkcount="1"if(mySmartUpload.getRequest().getParameter("title")!=null){

title=(String)mySmartUpload.getRequest().getParameter("title")

title=new String(title.getBytes("gbk"),"ISO-8859-1")

}

if(mySmartUpload.getRequest().getParameter("author")!=null){

author=(String)mySmartUpload.getRequest().getParameter("author")

author=new String(author.getBytes("gbk"),"ISO-8859-1")

}

if(mySmartUpload.getRequest().getParameter("content")!=null){

content=(String)mySmartUpload.getRequest().getParameter("content")

content=new String(content.getBytes("gbk"),"烂轮禅ISO-8859-1")

}

if(mySmartUpload.getRequest().getParameter("pdatetime")!=null){

pdatetime=(String)mySmartUpload.getRequest().getParameter("pdatetime")

}

if(mySmartUpload.getRequest().getParameter("topic")!=null){

topic=(String)mySmartUpload.getRequest().getParameter("topic")

}

if(mySmartUpload.getRequest().getParameter("imgintro")!=null){

imgintro=(String)mySmartUpload.getRequest().getParameter("imgintro")

imgintro=new String(imgintro.getBytes("gbk"),"ISO-8859-1")

}

if(mySmartUpload.getRequest().getParameter("clkcount")!=null){

clkcount=(String)mySmartUpload.getRequest().getParameter("clkcount")

}

//out.println(code+name+birthday)

%>


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存