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

首先你要排查一下错误。

1.debug 看一下 file是否传到了后台 是否是null

是null请检查前台请没罩求问题,不伏友是null 看一下 获取的filename是不是null

2.图片路径 根枯厅闹据提示 到盘符中查看是否有此路径,没有的话 手动建立文件夹或者通过代码创建

用JSP实搜歼现文件上传功能,参考如下:

UploadExample.jsp

<%@ page contentType="text/htmlcharset=gb2312"%>

<html>

<title><%= application.getServerInfo() %></title>

<body>

上传文件程序应用示例

<form action="doUpload.jsp" method="post" enctype="multipart/form-data">

<%-- 类型enctype用multipart/form-data,这样可以把文件中的数据作为流式数据上传,不管是什么文件类型,均可上传。--%>

请选择要上传的文件<input type="file" name="upfile" size="50">

<input type="submit" value="提交">

</form>

</body>

</html>

doUpload.jsp

<%@ page contentType="text/htmlcharset=GBK" %>

<%@ page import="java.io.*"%>

<%@ page import="java.util.*"%>

<%@ page import="javax.servlet.*"%>

<%@ page import="javax.servlet.http.*"%>

<html><head><title>upFile</title></head>

<body bgcolor="#ffffff">

<%

//猛漏局定义上载文件的最大字节

int MAX_SIZE = 102400 * 102400

// 创建根路径的保存变量

String rootPath

//声明文件读入类

DataInputStream in = null

FileOutputStream fileOut = null

//取得客户端的网络地址

String remoteAddr = request.getRemoteAddr()

//获得服务器的名字

String serverName = request.getServerName()

//取得互联网程序的绝对地址

String realPath = request.getRealPath(serverName)

realPath = realPath.substring(0,realPath.lastIndexOf("\\"))

//创建文件的保存目录

rootPath = realPath + "\\upload\\"

//取得客户端上传的数据类型

String contentType = request.getContentType()

try{

if(contentType.indexOf("multipart/form-data") >= 0){

//读入上传的数据

in = new DataInputStream(request.getInputStream())

int formDataLength = request.getContentLength()

if(formDataLength >MAX_SIZE){

out.println("<P>上传的文件字节数不可以超过" + MAX_SIZE + "</p>")

return

}

//保存上传文件的数据

byte dataBytes[] = new byte[formDataLength]

int byteRead = 0

int totalBytesRead = 0

//上传的数据保存在byte数组

while(totalBytesRead <formDataLength){

byteRead = in.read(dataBytes,totalBytesRead,formDataLength)

totalBytesRead += byteRead

}

//根据byte数组创建字符串

String file = new String(dataBytes)

//out.println(file)

/枝让/取得上传的数据的文件名

String saveFile = file.substring(file.indexOf("filename=\"") + 10)

saveFile = saveFile.substring(0,saveFile.indexOf("\n"))

saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""))

int lastIndex = contentType.lastIndexOf("=")

//取得数据的分隔字符串

String boundary = contentType.substring(lastIndex + 1,contentType.length())

//创建保存路径的文件名

String fileName = rootPath + saveFile

//out.print(fileName)

int pos

pos = file.indexOf("filename=\"")

pos = file.indexOf("\n",pos) + 1

pos = file.indexOf("\n",pos) + 1

pos = file.indexOf("\n",pos) + 1

int boundaryLocation = file.indexOf(boundary,pos) - 4

//out.println(boundaryLocation)

//取得文件数据的开始的位置

int startPos = ((file.substring(0,pos)).getBytes()).length

//out.println(startPos)

//取得文件数据的结束的位置

int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length

//out.println(endPos)

//检查上载文件是否存在

File checkFile = new File(fileName)

if(checkFile.exists()){

out.println("<p>" + saveFile + "文件已经存在.</p>")

}

//检查上载文件的目录是否存在

File fileDir = new File(rootPath)

if(!fileDir.exists()){

fileDir.mkdirs()

}

//创建文件的写出类

fileOut = new FileOutputStream(fileName)

//保存文件的数据

fileOut.write(dataBytes,startPos,(endPos - startPos))

fileOut.close()

out.println(saveFile + "文件成功上载.</p>")

}else{

String content = request.getContentType()

out.println("<p>上传的数据类型不是multipart/form-data</p>")

}

}catch(Exception ex){

throw new ServletException(ex.getMessage())

}

%>

</body>

</html>

运行方法,将这两个JSP文件放在同一路径下,运行UploadExample.jsp即可。


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

原文地址: https://outofmemory.cn/tougao/8172256.html

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

发表评论

登录后才能评论

评论列表(0条)

保存