jsp文件上传与下载实例代码
程序员文章站
2023-11-04 21:08:52
文件上传: 复制代码 代码如下: public class uploadservlet extends httpservlet{ @override protected v...
文件上传:
public class uploadservlet extends httpservlet{
@override
protected void doget(httpservletrequest req, httpservletresponse resp)
throws servletexception, ioexception {
dopost(req, resp);
}
@override
protected void dopost(httpservletrequest req, httpservletresponse resp)
throws servletexception, ioexception {
smartupload myupload = new smartupload();
servletconfig config = getservletconfig();
myupload.initialize(config,req,resp);
resp.setcontenttype("text/html");
resp.setcharacterencoding("utf-8");
printwriter out = resp.getwriter();
out.println("<h2>处理上传的文件</h2>");
out.println("<hr>");
try {
myupload.setmaxfilesize(1024*1024);
myupload.settotalmaxfilesize(5*1024*1024);
myupload.setallowedfileslist("doc,txt,jpg,gif");
myupload.setdeniedfileslist("exe,bat,jsp,htm,html");
myupload.upload();//上传文件,此项是必须的
int count = myupload.getfiles().getcount();
request myrequest = myupload.getrequest();
string rndfilename,fileextname,filename,filepathname;
date dt = null;
simpledateformat fmt = new simpledateformat("yyyymmdd");
//一一提取上传文件信息,同时可保持文件
for(int i = 0; i < count; i++){
file file = myupload.getfiles().getfile(i);
if(file.ismissing())
continue;
filename = new string(file.getfilename().getbytes(),"utf-8");//获取文件名
filepathname = new string(file.getfilepathname().getbytes(),"utf-8");
fileextname = file.getfileext();
dt = new date(system.currenttimemillis());
thread.sleep(100);
rndfilename = fmt.format(dt)+i+"."+fileextname;
out.println("第"+(i+1)+"个文件的文件信息:<br>");
out.println(" 文件名为:"+filename+"<br>");
out.println(" 文件扩展名为:"+fileextname+"<br>");
out.println(" 文件全名:"+filepathname+"<br>");
out.println(" 文件大小:"+file.getsize()/1024+"kb<br>");
//创建文件保存位置
servletcontext context = req.getservletcontext();
string savepath = context.getrealpath("/upload/");
java.io.file folder = new java.io.file(savepath);
if(!folder.exists()){
folder.mkdirs();
}
out.println(" 文件保存路径:"+savepath);
file.saveas(savepath+"/"+rndfilename,smartupload.save_physical);
}
} catch (exception e) {
out.println("文件上传失败!!!!");
e.printstacktrace();
}
out.flush();
out.close();
}
}
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*" %>
<%
//设定请求编码方式,否则遇到中文就会乱码
request.setcharacterencoding("utf-8");
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>上传文件实例</title>
<script type="text/javascript" src="js/jquery-1.8.1.js"></script>
<script type="text/javascript">
$(function(){
$("#number").change(function(){
var number = $("#number").attr("value");
$("#files").html("");
for(var i = 0; i < number; i++){
$("#files").append("<input type='file' name='file''"+i+"'><br/>");
}
});
});
</script>
</head>
<body>
<h2>上传文件实例</h2>
请选择上传文件数量:
<select id="number">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<form action="uploadservlet" method="post" enctype="multipart/form-data">
<div id="files"></div>
<input type="submit" value="提交"/>
</form>
</body>
</html>
文件下载:
<%
response.setcontenttype("application/x-download");//设置为下载application/x-download
string filedownload = "/要下载的文件名";//即将下载的文件的相对路径
string filedisplay = "最终要显示给用户的保存文件名";//下载文件时显示的文件保存名称
string filenamedisplay = urlencoder.encode(filedisplay,"utf-8");
response.addheader("content-disposition","attachment;filename=" + filedisplay);
try
{
requestdispatcher dis = application.getrequestdispatcher(filedownload);
if(dis!= null)
{
dis.forward(request,response);
}
response.flushbuffer();
}
catch(exception e)
{
e.printstacktrace();
}
finally
{
}
%>
复制代码 代码如下:
public class uploadservlet extends httpservlet{
@override
protected void doget(httpservletrequest req, httpservletresponse resp)
throws servletexception, ioexception {
dopost(req, resp);
}
@override
protected void dopost(httpservletrequest req, httpservletresponse resp)
throws servletexception, ioexception {
smartupload myupload = new smartupload();
servletconfig config = getservletconfig();
myupload.initialize(config,req,resp);
resp.setcontenttype("text/html");
resp.setcharacterencoding("utf-8");
printwriter out = resp.getwriter();
out.println("<h2>处理上传的文件</h2>");
out.println("<hr>");
try {
myupload.setmaxfilesize(1024*1024);
myupload.settotalmaxfilesize(5*1024*1024);
myupload.setallowedfileslist("doc,txt,jpg,gif");
myupload.setdeniedfileslist("exe,bat,jsp,htm,html");
myupload.upload();//上传文件,此项是必须的
int count = myupload.getfiles().getcount();
request myrequest = myupload.getrequest();
string rndfilename,fileextname,filename,filepathname;
date dt = null;
simpledateformat fmt = new simpledateformat("yyyymmdd");
//一一提取上传文件信息,同时可保持文件
for(int i = 0; i < count; i++){
file file = myupload.getfiles().getfile(i);
if(file.ismissing())
continue;
filename = new string(file.getfilename().getbytes(),"utf-8");//获取文件名
filepathname = new string(file.getfilepathname().getbytes(),"utf-8");
fileextname = file.getfileext();
dt = new date(system.currenttimemillis());
thread.sleep(100);
rndfilename = fmt.format(dt)+i+"."+fileextname;
out.println("第"+(i+1)+"个文件的文件信息:<br>");
out.println(" 文件名为:"+filename+"<br>");
out.println(" 文件扩展名为:"+fileextname+"<br>");
out.println(" 文件全名:"+filepathname+"<br>");
out.println(" 文件大小:"+file.getsize()/1024+"kb<br>");
//创建文件保存位置
servletcontext context = req.getservletcontext();
string savepath = context.getrealpath("/upload/");
java.io.file folder = new java.io.file(savepath);
if(!folder.exists()){
folder.mkdirs();
}
out.println(" 文件保存路径:"+savepath);
file.saveas(savepath+"/"+rndfilename,smartupload.save_physical);
}
} catch (exception e) {
out.println("文件上传失败!!!!");
e.printstacktrace();
}
out.flush();
out.close();
}
}
复制代码 代码如下:
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<%@ page import="com.jspsmart.upload.*" %>
<%
//设定请求编码方式,否则遇到中文就会乱码
request.setcharacterencoding("utf-8");
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>上传文件实例</title>
<script type="text/javascript" src="js/jquery-1.8.1.js"></script>
<script type="text/javascript">
$(function(){
$("#number").change(function(){
var number = $("#number").attr("value");
$("#files").html("");
for(var i = 0; i < number; i++){
$("#files").append("<input type='file' name='file''"+i+"'><br/>");
}
});
});
</script>
</head>
<body>
<h2>上传文件实例</h2>
请选择上传文件数量:
<select id="number">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<form action="uploadservlet" method="post" enctype="multipart/form-data">
<div id="files"></div>
<input type="submit" value="提交"/>
</form>
</body>
</html>
文件下载:
复制代码 代码如下:
<%
response.setcontenttype("application/x-download");//设置为下载application/x-download
string filedownload = "/要下载的文件名";//即将下载的文件的相对路径
string filedisplay = "最终要显示给用户的保存文件名";//下载文件时显示的文件保存名称
string filenamedisplay = urlencoder.encode(filedisplay,"utf-8");
response.addheader("content-disposition","attachment;filename=" + filedisplay);
try
{
requestdispatcher dis = application.getrequestdispatcher(filedownload);
if(dis!= null)
{
dis.forward(request,response);
}
response.flushbuffer();
}
catch(exception e)
{
e.printstacktrace();
}
finally
{
}
%>