最近项目中遇到需要导入excel文件并将文件中数据保存到数据库的问题,于是特意练习了一下文件的上传。
一、页面upload.jsp文件
<body>
<h2>导入标准xml关系模式</h2>
<form name="upform" action="UploadServlet" method="POST" enctype="multipart/form-data">
<input type ="file" name="file1" id="file1"/><br/>
<input type ="file" name="file2" id="file2"/><br/>
<input type ="file" name="file3" id="file3"/><br/>
<input type="submit" value="Submit" /><br/>
<input type="reset" />
</form>
</body>
二、servlet文件UploadServlet.java
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.DefaultFileItemFactory;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
public class UploadServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
File tmpDir = null;//初始化上传文件的临时存放目录
File saveDir = null;//初始化上传文件后的保存目录
public UploadServlet() { super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
public void init() throws ServletException {
super.init();
String tmpPath = "c:\\tmpdir";
String savePath = "c:\\updir";
tmpDir = new File(tmpPath);
saveDir = new File(savePath);
if(!tmpDir.isDirectory())
tmpDir.mkdir();
if(!saveDir.isDirectory())
saveDir.mkdir();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
if(ServletFileUpload.isMultipartContent(request)){
DiskFileItemFactory dff = new DiskFileItemFactory();//创建该对象
dff.setRepository(tmpDir);//指定上传文件的临时目录
dff.setSizeThreshold(1024000);//指定在内存中缓存数据大小,单位为byte
ServletFileUpload sfu = new ServletFileUpload(dff);//创建该对象
sfu.setFileSizeMax(5000000);//指定单个上传文件的最大尺寸
sfu.setSizeMax(10000000);//指定一次上传多个文件的总尺寸
FileItemIterator fii = sfu.getItemIterator(request);
while(fii.hasNext()){
FileItemStream fis = fii.next();//从集合中获得一个文件流
if(!fis.isFormField() && fis.getName().length()>0){
System.out.println("fff");
String fileName = fis.getName().substring(fis.getName().lastIndexOf(\\)+1);//获得上传文件的文件名
System.out.println(fileName);
BufferedInputStream in = new BufferedInputStream(fis.openStream());//获得文件输入流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(saveDir+"\"+filename)));//获得文件输出流
Streams.copy(in, out, true);//开始把文件写到你指定的上传文件夹
}
}
response.getWriter().println("File upload successfully!!!");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
三、web.xml文件
<?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">
<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>UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>upload.jsp</welcome-file>
</welcome-file-list>
</web-app>