Apache Commons fileUpload實(shí)現(xiàn)文件上傳之一
廢話不多說(shuō)了,直奔主題了。
需要兩個(gè)jar包:
commons-fileupload.jar
Commons IO的jar包(本文使用commons-io-2.4.jar)
利用Servlet來(lái)實(shí)現(xiàn)文件上傳。
package web.servlet;
import java.io.IOException;
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 java.io.File;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class UploadServlet
*/
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String uploadPath = "D:\\temp"; // 上傳文件的目錄
private String tempPath = "d:\\temp\\buffer\\"; // 臨時(shí)文件目錄
File tempPathFile;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096); // 設(shè)置緩沖區(qū)大小,這里是4kb
factory.setRepository(tempPathFile);// 設(shè)置緩沖區(qū)目錄
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(4194304); // 設(shè)置最大文件尺寸,這里是4MB
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
System.out.print("upload succeed");
} catch (Exception e) {
// 可以跳轉(zhuǎn)出錯(cuò)頁(yè)面
e.printStackTrace();
}
}
public void init() throws ServletException {
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
File tempPathFile = new File(tempPath);
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
}
}
jsp
<%@ page language="java" contentType="text/html; charset=ISO--" pageEncoding="utf-"%> <!DOCTYPE html PUBLIC "-//WC//DTD HTML . Transitional//EN" "http://www.w.org/TR/html/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB"> <title>File upload</title> </head> <body> <!-- // action="fileupload"對(duì)應(yīng)web.xml中<servlet-mapping>中<url-pattern>的設(shè)置. --> <form name="myform" action="UploadServlet" method="post" enctype="multipart/form-data"> File:<br> <input type="file" name="myfile"><br> <br> <input type="submit" name="submit" value="Commit"> </form> </body> </html>
這樣就能簡(jiǎn)單實(shí)現(xiàn)一個(gè)文件上傳功能了,當(dāng)然這是最最最基本的,繼續(xù)研究中。
- java利用Apache commons codec進(jìn)行MD5加密,BASE64加密解密,執(zhí)行系統(tǒng)命令
- Apache Commons Math3探索之快速傅立葉變換代碼示例
- Apache Commons Math3探索之多項(xiàng)式曲線擬合實(shí)現(xiàn)代碼
- Apache Commons Math3學(xué)習(xí)之?dāng)?shù)值積分實(shí)例代碼
- Apache commons fileupload文件上傳實(shí)例講解
- Apache Commons fileUpload文件上傳多個(gè)示例分享
- Apache Commons DbUtils工具包使用介紹
- apache commons工具集代碼詳解
相關(guān)文章
Java中BigDecimal的equals方法和compareTo方法的區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于Java中BigDecimal的equals方法和compareTo方法區(qū)別的相關(guān)資料,對(duì)于BigDecimal的大小比較,用equals方法的話會(huì)不僅會(huì)比較值的大小,還會(huì)比較兩個(gè)對(duì)象的精確度,而compareTo方法則不會(huì)比較精確度,只比較數(shù)值的大小,需要的朋友可以參考下2023-11-11
Spring事務(wù)執(zhí)行流程及如何創(chuàng)建事務(wù)
這篇文章主要介紹了Spring事務(wù)執(zhí)行流程及如何創(chuàng)建事務(wù),幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下2021-03-03
最安全的加密算法Bcrypt防止數(shù)據(jù)泄露詳解
這篇文章主要為大家介紹了最安全的加密算法Bcrypt防止數(shù)據(jù)泄露詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
在java中判斷兩個(gè)浮點(diǎn)型(float)數(shù)據(jù)是否相等的案例
這篇文章主要介紹了在java中判斷兩個(gè)浮點(diǎn)型(float)數(shù)據(jù)是否相等的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Java鏈接redis_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java鏈接redis,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
SpringMVC?Restful風(fēng)格與中文亂碼問(wèn)題解決方案介紹
Restful就是一個(gè)資源定位及資源操作的風(fēng)格,不是標(biāo)準(zhǔn)也不是協(xié)議,只是一種風(fēng)格,是對(duì)http協(xié)議的詮釋,下面這篇文章主要給大家介紹了關(guān)于SpringMVC對(duì)Restful風(fēng)格支持的相關(guān)資料,需要的朋友可以參考下2022-10-10
springboot如何使用assembly打包項(xiàng)目和啟動(dòng)腳本
這篇文章主要介紹了springboot如何使用assembly打包項(xiàng)目和啟動(dòng)腳本問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
java性能優(yōu)化四種常見(jiàn)垃圾收集器匯總
這篇文章主要介紹了java性能優(yōu)化四種常見(jiàn)垃圾收集器匯總,每種垃圾收集器都有其不同的算法實(shí)現(xiàn)和步驟,下面我們簡(jiǎn)單描述下我們常見(jiàn)的四種垃圾收集器的算法過(guò)程,感興趣的同學(xué)們最好先看下以下的兩篇文章去增加理解2022-07-07

