文件的上传实例:
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>上传页面</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>
<form action="${pageContext.request.contextPath}/uploadServlet2" method="post" enctype="multipart/form-data" >
上传用户:<input type="text" name="user"><br>
上传文件1:<input type="file" name="file1"><br>
上传文件2:<input type="file" name="file2"><br>
<input type="submit" value="上传开始">
</form>
<h2>${message}</h2>
</body>
</html>
描述文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.hbsi.servlet.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/uploadServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
selvelt:
package com.hbsi.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //上传文件
try {
//(1)创建一个工厂对象FileItem
DiskFileItemFactory factory=new DiskFileItemFactory();
//(2)得到解析器
ServletFileUpload upload=new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
//(3)对请求进行解析,有几个输入项,就有几个FileItem对象
List<FileItem> list= upload.parseRequest(request);
//(4)迭代list集合,获取list集合中的每一项
for(FileItem item:list){
//判断输入的类型
if(item.isFormField()){
//普通输入项
String inputName=item.getFieldName();
String inputValue=item.getString();
System.out.println(inputName+" "+inputValue);
}else{
//上传文件的输入项
String fileName=item.getName();//上传文件的文件名
fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
InputStream is=item.getInputStream();
FileOutputStream fos=new FileOutputStream("D:\\"+fileName);
byte[] buff=new byte[1024];
int len=0;
while((len=is.read(buff))>0){
fos.write(buff,0,len);
}
is.close();
fos.close();
request.setAttribute("message", "上传成功");
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
request.setAttribute("message","上传失败");
}
request.getRequestDispatcher("/upload.jsp").forward(request, response);
}
}