檔案上傳
以前覺得這是個十分高大上的功能,結果發現,隻要使用實用的工具類,五分鐘就能搞定一個檔案上傳的功能了!
環境搭建
前提是已經搭建好開發環境。
- 建立JavaWEB項目
- Tomcat伺服器安裝
步驟
1. 下載下傳工具類:smartupload
- 将其導入到項目的lib目錄下
2. 寫一個前端頁面,此處是在jsp頁面中寫了一個form表單,命名為index.jsp
<%--
Created by IntelliJ IDEA.
User: Elvira
Date: 2020/11/16
Time: 20:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>首頁</title>
</head>
<body>
檔案上傳:<br>
<form action="/upload" method="post" enctype="multipart/form-data">
名稱:<input type="text" name="uname"><br>
檔案:<input type="file" name="myfile"><br>
<input type="submit" value="上傳">
</form>
</body>
</html>
- 表單送出跳轉到請求名為/upload的servlet。
3. 控制上傳的servlet,命名為UploadServlet。
- 在類名上方添加WebServlet注解:value為“/upload”,與表單跳轉的請求名一緻。
- 重寫service方法。
- 上傳檔案
//1. 建立對象 SmartUpload smartUpload = new SmartUpload();
- 獲得pageContext對象
//2. 初始化 PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(this, req, resp, null, false, 1024 * 5, true); smartUpload.initialize(pageContext);
- 設定編碼方式
- 上傳
//實作檔案資料的上傳 smartUpload.upload();
- 儲存檔案
- 得到檔案的基本資訊
String filename = file.getFileName(); //指定檔案的路徑 String url = "uploadfile/" + filename; //将上傳檔案儲存到指定目錄 file.saveAs(url, SmartUpload.SAVE_VIRTUAL); //是否儲存成功?如果上傳成功,頁面中顯示該檔案。 req.setAttribute("filename",filename);
- 測試:除檔案以外的參數如何擷取
String uname = smartUpload.getRequest().getParameter("uname"); System.out.println("uname=" + uname);
-
跳轉頁面
這裡一定要使用轉發,要儲存域裡的資料。
- 上傳檔案
4. 上傳成功會轉發跳轉至success.jsp頁面,建立jsp頁面。
- 如果上傳成功,頁面上會顯示檔案。
- 此處拿圖檔做測試。
<%-- Created by IntelliJ IDEA. User: Elvira Date: 2020/11/18 Time: 14:21 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>成功跳轉至success.jsp,上傳成功!</h1> <img src="uploadfile/${filename}"> </body> </html>
servlet完整代碼
package web;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
/**
* @author Elvira
* @date 2020/11/17 20:56
* @description
*/
@WebServlet(value="/upload")
public class UploadServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//上傳檔案
//1. 建立對象
SmartUpload smartUpload = new SmartUpload();
//獲得pageContext對象
//2. 初始化
PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(this, req, resp, null, false, 1024 * 5, true);
smartUpload.initialize(pageContext);
//3. 設定編碼方式
smartUpload.setCharset("utf-8");
try {
//4. 上傳
//實作檔案資料的上傳
smartUpload.upload();
//5. 儲存檔案
File file = smartUpload.getFiles().getFile(0);
//6. 得到檔案的基本資訊
String filename = file.getFileName();
//指定檔案的路徑
String url = "uploadfile/" + filename;
//将上傳檔案儲存到指定目錄
file.saveAs(url, SmartUpload.SAVE_VIRTUAL);
//是否儲存成功?如果上傳成功,頁面中顯示該檔案。
req.setAttribute("filename",filename);
} catch (SmartUploadException e) {
e.printStackTrace();
}
//7. 測試:除檔案以外的參數如何擷取
String uname = smartUpload.getRequest().getParameter("uname");
System.out.println("uname=" + uname);
//8. 跳轉頁面
req.getRequestDispatcher("success.jsp").forward(req, resp);
}
}
效果
- 完成上述步驟,可以開啟伺服器檢視一下效果。
前端效果圖
- 點選選擇檔案,彈出視窗。
- 選擇一張圖檔:兄妹.png,頁面上顯示兄妹.png,填寫名稱。
- 點選上傳,頁面跳轉。
總結
這樣一個檔案上傳的功能就完成了,smartupload工具真是強大!