最近項目中遇到需要匯入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>