求commons-fileupload 多文件上传例子项目是使用jsp+jdbc。谢谢
1个回答
展开全部
环境:MyEclipse 6.5 + Tomcat5.5 + Jdk 1.6 并引入 commons-fileupload-1.2.jar
jsp上传文件代码 :
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<樱肢悔head>脊正
<base href="<%=basePath%>">
<title>My JSP 'multiFileUpload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function addMore(){
var td=document.getElementById("moreFile");
var br=document.createElement("br");
var input=document.createElement("input");
var button=document.createElement("input");
input.type="file";
input.name="file";
button.type="button";
button.value="-";
button.onclick=function(){
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</script>
</head>
<body>
<form action="upload/uploadAction.jsp" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>多文件上传</td>
<td></td>
</tr>
<tr>
<td>文件:</td>
<td id="moreFile"><input name="file1" type="file" /><input type="button" value=" + " onclick="addMore()"/饥镇></td>
</tr>
<tr>
<td><input type="submit" value="开始上传" /></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
jsp处理请求页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.apache.commons.fileupload.FileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="org.apache.commons.fileupload.FileUploadException"%>
<%
//设定请求字符集,防止中文文件名乱码
request.setCharacterEncoding("UTF-8");
//错误或异常提示信息
String errorMsg="";
String strFileName = "";
String path = request.getRealPath("/") + File.separator + "upload/";//上传文件的路径
//判断form是否是multpart/form-data
if(FileUpload.isMultipartContent(request)){
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置上传工厂的限制
factory.setSizeThreshold(1024 * 1024 * 20);
//new File(request.getRealPath("/")可以具体执行路径
factory.setRepository(new File(path));
// 创建一个上传文件的ServletFileUpload对象
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置最大的上传限制,-1表示无限制
upload.setSizeMax(1024 * 1024 * 20);
// 处理HTTP请求,items是所有的表单项
try {
List<FileItem> items = upload.parseRequest(request);
if(null==items){
errorMsg = "未上传文件,请选择...";
}else{
for(FileItem fi : items){
//判断提交表单元素,是否是上传组件(type=file)
if(fi.isFormField()){
errorMsg = "上传的不是文件类型";
}else{
// 取得文件类型
String fileName = fi.getName();
if(fileName!=null && !"".equals(fileName)&&fileName.length()>0)
{
strFileName = fileName.substring(fileName.lastIndexOf("\\"),fileName.length());
FileOutputStream fos = new FileOutputStream(path + strFileName);
if (fi.isInMemory()) {
fos.write(fi.get());
} else {
BufferedReader is = new BufferedReader(
new InputStreamReader(fi.getInputStream()));
String str = null;
while ((str += is.readLine()) != null) {
byte[] buffer = str.getBytes("ISO-8859-1");
fos.write(buffer);
}
is.close();
}
fos.close();
}
}
}
errorMsg="文件上传成功";
}
} catch (FileUploadException e) {
errorMsg = "文件上传发生错误"+e.getMessage();
e.printStackTrace();
}
}else{
errorMsg = "提交的表单属性设置不正确,不是上传文件的表单";
}
request.setAttribute("msg", errorMsg);
request.getRequestDispatcher("/result.jsp").forward(request, response);
%>
处理结果页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传结果</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
上传结果:${msg }
</body>
</html>
jsp上传文件代码 :
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<樱肢悔head>脊正
<base href="<%=basePath%>">
<title>My JSP 'multiFileUpload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function addMore(){
var td=document.getElementById("moreFile");
var br=document.createElement("br");
var input=document.createElement("input");
var button=document.createElement("input");
input.type="file";
input.name="file";
button.type="button";
button.value="-";
button.onclick=function(){
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</script>
</head>
<body>
<form action="upload/uploadAction.jsp" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>多文件上传</td>
<td></td>
</tr>
<tr>
<td>文件:</td>
<td id="moreFile"><input name="file1" type="file" /><input type="button" value=" + " onclick="addMore()"/饥镇></td>
</tr>
<tr>
<td><input type="submit" value="开始上传" /></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
jsp处理请求页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="org.apache.commons.fileupload.FileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="org.apache.commons.fileupload.FileUploadException"%>
<%
//设定请求字符集,防止中文文件名乱码
request.setCharacterEncoding("UTF-8");
//错误或异常提示信息
String errorMsg="";
String strFileName = "";
String path = request.getRealPath("/") + File.separator + "upload/";//上传文件的路径
//判断form是否是multpart/form-data
if(FileUpload.isMultipartContent(request)){
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置上传工厂的限制
factory.setSizeThreshold(1024 * 1024 * 20);
//new File(request.getRealPath("/")可以具体执行路径
factory.setRepository(new File(path));
// 创建一个上传文件的ServletFileUpload对象
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置最大的上传限制,-1表示无限制
upload.setSizeMax(1024 * 1024 * 20);
// 处理HTTP请求,items是所有的表单项
try {
List<FileItem> items = upload.parseRequest(request);
if(null==items){
errorMsg = "未上传文件,请选择...";
}else{
for(FileItem fi : items){
//判断提交表单元素,是否是上传组件(type=file)
if(fi.isFormField()){
errorMsg = "上传的不是文件类型";
}else{
// 取得文件类型
String fileName = fi.getName();
if(fileName!=null && !"".equals(fileName)&&fileName.length()>0)
{
strFileName = fileName.substring(fileName.lastIndexOf("\\"),fileName.length());
FileOutputStream fos = new FileOutputStream(path + strFileName);
if (fi.isInMemory()) {
fos.write(fi.get());
} else {
BufferedReader is = new BufferedReader(
new InputStreamReader(fi.getInputStream()));
String str = null;
while ((str += is.readLine()) != null) {
byte[] buffer = str.getBytes("ISO-8859-1");
fos.write(buffer);
}
is.close();
}
fos.close();
}
}
}
errorMsg="文件上传成功";
}
} catch (FileUploadException e) {
errorMsg = "文件上传发生错误"+e.getMessage();
e.printStackTrace();
}
}else{
errorMsg = "提交的表单属性设置不正确,不是上传文件的表单";
}
request.setAttribute("msg", errorMsg);
request.getRequestDispatcher("/result.jsp").forward(request, response);
%>
处理结果页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传结果</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
上传结果:${msg }
</body>
</html>
追问
谢谢,再请问下,如果我要给result.jsp页面在后台传参数进去,怎么传呢,我获取的参数一直都是空的,如果我是多文件上传, upload.parseRequest(request);这一步就报错了,根本不会执行下面的else,如果判断是哪个文件大小超出限制了呢,这个用于单文件上传,非常好。如果是多文件,那我如何判断是哪个文件大小超出文件 factory.setSize(1024 * 1024 * 20);
最大大小呢
追答
request.setAttribute() 或者 request.getSession.setAttribute()设置参数.
文件大小可以通过FileItem的getSize()取的字节数.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询