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

FileOutputStream很明显你是用的文件流返回的

//以byte流的方式打开文件d:\1.gif

FileInputStreamhFile=newFileInputStream(url)   //得到文件大小

inti=hFile.available()

bytedata[]=newbyte[i]    //读数据

hFile.read(data)     //得到向客户端输出二进制数据的对象

OutputStreamtoClient=response.getOutputStream()     //输出数据

toClient.write(data)

toClient.flush()

toClient.close()

hFile.close()

扩展资料

如果是纯文本使用字符流,如果二进制文件,使用字节流。

如果只是得到信息,原样不动,不进行修改 *** 作,例如文件上传旦枝和下载,这时就使用字节流。文件上传:在服务器端把浏览器端信息提取出来。文件下载:把服务器端内容竖配写给浏览器端。

如果要 *** 作的是自定义信息,这时使用字符流。

通过response获取的输出流它的真实类型是什么?

ServletOutputStream response.getOutputStream()

PrintWriter response.getWriter()

ServletOutputStream由模纤敏于使用字节流多数是原样复制,所以使用write方法,而不是print方法。

PrintWriter:打印流,两个特点:1.可以设置自动刷新。2.可以将信息原样输出。

<! #include FILE="upload inc" >谈如 <% dim upload file formName formPath iCount filename fileExt set upload=new upload_ xSoft 建立上传对象 formPath="uploadimages/"   在目录后启戚加(/) if right(formPath )<>"/" then formPath=formPath&"/" iCount= for each formName in upload file 列出所有上传了的文件  set file=upload file(formName)  生成一个文件对象  if file filesize<then   response write "<font size= ><br>请先选择你要上传的图片 [ <a href=# onclick=history go( )>重新上传</a>]</font>"  response end  end if    if file filesize>then  含旁启 response write "<font size= ><br>图片大小超过了限制 [ <a href=# onclick=history go( )>重新上传</a>]</font>"  response end  end if

fileExt=lcase(right(file filename ))

if fileEXT<>" jpg" and fileEXT<>" gif" then   response write "<font size= ><br>文件格式只能为jpg和gif格式 [ <a href=# onclick=history go( )>重新上传</a>]</font>"  response end  end if  randomize  ranNum=int( *rnd)+  filename=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&fileExt  filename =year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&fileExt    if file FileSize>then         如果 FileSize >说明有文件数据  file SaveAs Server mappath(filename)   保存文件 response write "<script>parent form img value= "&FileName &" </script>"

iCount=iCount+  end if  set file=nothing next set upload=nothing  删除此对象

Response Write "<img src= "&" /UpLoad/UpLoadImages/"&FileName &" onload = DrawImage(this) >"  response end %>

下面是upload inc的代码

<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>dim upfile_ xSoft_Stream Class upload_ xSoft dim Form File Version Private Sub Class_Initialize dim iStart iFileNameStart iFileNameEnd iEnd vbEnter iFormStart iFormEnd theFile dim strDiv mFormName mFormValue mFileName mFileSize mFilePath iDivLen mStr if Request TotalBytes<then Exit Sub set Form=CreateObject("Scripting Dictionary") set File=CreateObject("Scripting Dictionary") set upfile_ xSoft_Stream=CreateObject("Adodb Stream") upfile_ xSoft_Stream mode= upfile_ xSoft_Stream type= upfile_ xSoft_Stream open upfile_ xSoft_Stream write Request BinaryRead(Request TotalBytes) vbEnter=Chr( )&Chr( ) iDivLen=inString( vbEnter)+ strDiv=subString( iDivLen) iFormStart=iDivLen iFormEnd=inString(iformStart strDiv) while iFormStart <iFormEnd   iStart=inString(iFormStart "name=""")   iEnd=inString(iStart+ """")   mFormName=subString(iStart+ iEnd iStart )   iFileNameStart=inString(iEnd+ "filename=""")   if iFileNameStart>and iFileNameStart<iFormEnd then    iFileNameEnd=inString(iFileNameStart+ """")    mFileName=subString(iFileNameStart+ iFileNameEnd iFileNameStart )    iStart=inString(iFileNameEnd+ vbEnter&vbEnter)    iEnd=inString(iStart+ vbEnter&strDiv)    if iEnd>iStart then     mFileSize=iEnd iStart    else     mFileSize=    end if    set theFile=new FileInfo    theFile FileName=getFileName(mFileName)    theFile FilePath=getFilePath(mFileName)    theFile FileSize=mFileSize    theFile FileStart=iStart+    theFile FormName=FormName    file add mFormName theFile   else    iStart=inString(iEnd+ vbEnter&vbEnter)    iEnd=inString(iStart+ vbEnter&strDiv)

if iEnd>iStart then     mFormValue=subString(iStart+ iEnd iStart )    else     mFormValue=""    end if    form Add mFormName mFormValue   end if

iFormStart=iformEnd+iDivLen   iFormEnd=inString(iformStart strDiv) wend End Sub

Private Function subString(theStart theLen)  dim i c stemp  upfile_ xSoft_Stream Position=theStart  stemp=""  for i= to theLen    if upfile_ xSoft_Stream EOS then Exit for    c=ascB(upfile_ xSoft_Stream Read( ))    If c >Then     if upfile_ xSoft_Stream EOS then Exit for     stemp=stemp&Chr(AscW(ChrB(AscB(upfile_ xSoft_Stream Read( )))&ChrB(c)))     i=i+    else     stemp=stemp&Chr(c)    End If  Next  subString=stemp End function

Private Function inString(theStart varStr)  dim i j bt theLen str  InString=  Str=toByte(varStr)  theLen=LenB(Str)  for i=theStart to upfile_ xSoft_Stream Size theLen    if i>upfile_ xSoft_Stream size then exit Function    upfile_ xSoft_Stream Position=i    if AscB(upfile_ xSoft_Stream Read( ))=AscB(midB(Str )) then     InString=i     for j= to theLen       if upfile_ xSoft_Stream EOS then         inString=         Exit for       end if       if AscB(upfile_ xSoft_Stream Read( ))<>AscB(MidB(Str j )) then         InString=         Exit For       end if     next     if InString<>then Exit Function    end if  next End Function

Private Sub Class_Terminate    form RemoveAll   file RemoveAll   set form=nothing   set file=nothing   upfile_ xSoft_Stream close   set upfile_ xSoft_Stream=nothing End Sub       Private function GetFilePath(FullPath)   If FullPath <>"" Then    GetFilePath = left(FullPath InStrRev(FullPath ""))   Else    GetFilePath = ""   End If  End  function    Private function GetFileName(FullPath)   If FullPath <>"" Then    GetFileName = mid(FullPath InStrRev(FullPath "")+ )   Else    GetFileName = ""   End If  End  function

Private function toByte(Str)    dim i iCode c iLow iHigh    toByte=""    For i= To Len(Str)    c=mid(Str i )    iCode =Asc(c)    If iCode<Then iCode = iCode +    If iCode>Then      iLow = Left(Hex(Asc(c)) )      iHigh =Right(Hex(Asc(c)) )      toByte = toByte &chrB("&H"&iLow) &chrB("&H"&iHigh)    Else      toByte = toByte &chrB(AscB(c))    End If    Next  End function End Class

lishixinzhi/Article/program/net/201311/14198


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存