javaweb實(shí)現(xiàn)文件上傳功能
本文實(shí)例為大家分享了javaweb實(shí)現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
1、創(chuàng)建一個空項(xiàng)目
2、新建一個web application 的Module
3、創(chuàng)建一個lib目錄導(dǎo)入需要用的jar包
- commons-io
- commons-fileupload
4、將lib包添加到項(xiàng)目依賴(右鍵 Add as Library)

5、編寫文件上傳表單
<%--通過表單上傳文件
? ? get : 上傳文件大小有限制
? ? post : 上傳文件大小沒有限制
? ? 上傳文件必須要enctype="multipart/form-data"
--%>
? <form action="${pageContext.request.contextPath}/upload.do" method="post" enctype="multipart/form-data">
? ? <p>上傳用戶:<input type="text" name="username"></p>
? ? <p><input type="file" name="file1"></p>
? ? <p><input type="submit"> | <input type="reset"></p>
? </form>6.編寫Servlet
public class FileServlet extends HttpServlet {
? ? protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
? ? ? ? //判斷上傳的表單是普通表單還是帶文件表單
? ? ? ? if (!ServletFileUpload.isMultipartContent(request)){//如果不是帶文件表單
? ? ? ? ? ? return;//終止方法運(yùn)行,直接返回
? ? ? ? }
? ? ? ? try {
? ? ? ? //創(chuàng)建上傳文件的保存路徑,建議在WEB-INF路徑下,安全,用戶無法直接訪問上傳的文件.
? ? ? ? String uploadPath = this.getServletContext().getRealPath("/WEB-INF/upload");
? ? ? ? File uploadFile = new File(uploadPath);
? ? ? ? if (!uploadFile.exists()){
? ? ? ? ? ? uploadFile.mkdirs();//如果不存在則創(chuàng)建目錄
? ? ? ? }
? ? ? ? //緩存,臨時文件
? ? ? ? //臨時文件,假如文件超出預(yù)期大小,就把它放到臨時文件夾中,過幾天自動刪除,或者提醒用戶轉(zhuǎn)存為永久文件
? ? ? ? String tmpPath = this.getServletContext().getRealPath("/WEB-INF/tmp");
? ? ? ? File tmpFile = new File(tmpPath);
? ? ? ? if (!tmpFile.exists()){
? ? ? ? ? ? tmpFile.mkdirs();//如果不存在則創(chuàng)建臨時目錄
? ? ? ? }
? ? ? ? //1.創(chuàng)建DiskFileItemFactory對象,處理文件上傳路徑或者大小限制
? ? ? ? DiskFileItemFactory factory = new DiskFileItemFactory();
? ? ? ? //通過這個工廠設(shè)置一個緩沖區(qū),當(dāng)上傳的文件大于這個緩沖區(qū)的時候,將它放在臨時文件中
? ? ? ? factory.setSizeThreshold(1024*1024);//緩沖區(qū)大小為1M
? ? ? ? factory.setRepository(tmpFile);//臨時文件的保存目錄
? ? ? ? //2.獲取ServletFileUpload對象
? ? ? ? ServletFileUpload upload = new ServletFileUpload(factory);
? ? ? ? //監(jiān)聽文件上傳進(jìn)度
? ? ? ? upload.setProgressListener(new ProgressListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void update(long l, long l1, int i) {
? ? ? ? ? ? ? ? System.out.println("總大小: "+l1+" 已上傳: "+l);
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //處理亂碼問題
? ? ? ? upload.setHeaderEncoding("utf-8");
? ? ? ? //設(shè)置單個文件的最大值
? ? ? ? upload.setFileSizeMax(1024*1024*10);//10M
? ? ? ? //設(shè)置總共能夠上傳文件的大小
? ? ? ? upload.setSizeMax(1024*1024*10);//10M
? ? ? ? //3.處理上傳文件
? ? ? ? ? ? //把前端請求解析,封裝成一個FileItem對象
? ? ? ? ? ? List<FileItem> fileItems = upload.parseRequest(request);
? ? ? ? ? ? for (FileItem fileItem : fileItems) {
? ? ? ? ? ? ? ? //判斷上傳的表單是普通表單還是帶文件表單
? ? ? ? ? ? ? ? if (fileItem.isFormField()){
? ? ? ? ? ? ? ? ? ? String name=fileItem.getFieldName();//獲取表單控件的名字
? ? ? ? ? ? ? ? ? ? String value=fileItem.getString("UTF-8");//獲取值,處理亂碼
? ? ? ? ? ? ? ? ? ? System.out.println(name+": "+value);
? ? ? ? ? ? ? ? }else {//文件
? ? ? ? ? ? ? ? ? ? String uploadFileName = fileItem.getName();//獲取上傳文件名字(帶路徑)
? ? ? ? ? ? ? ? ? ? //可能存在文件名不合法的情況
? ? ? ? ? ? ? ? ? ? if (uploadFileName==null||uploadFileName.trim().equals("")){
? ? ? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //截取上傳的文件名
? ? ? ? ? ? ? ? ? ? String FileName=uploadFileName.substring(uploadFileName.lastIndexOf("/")+1);//從最后一個/后開始截取
? ? ? ? ? ? ? ? ? ? //截取后綴名
? ? ? ? ? ? ? ? ? ? String fileExtName=uploadFileName.substring(uploadFileName.lastIndexOf(".")+1);//從最后一個.后開始截取
? ? ? ? ? ? ? ? ? ? //網(wǎng)絡(luò)傳輸中的東西,都需要序列化
? ? ? ? ? ? ? ? ? ? //POJO , 實(shí)體類, 如果想要在多個電腦運(yùn)行, 傳輸-->需要把對象序列化
? ? ? ? ? ? ? ? ? ? //JNI= java native Interface
? ? ? ? ? ? ? ? ? ? //implements Serializable : 標(biāo)記接口 , JVM-->java棧 本地方法棧 native -->C++
? ? ? ? ? ? ? ? ? ? //可以使用UUID(唯一標(biāo)識的通用碼),保證文件名唯一
? ? ? ? ? ? ? ? ? ? String uuidPath = UUID.randomUUID().toString();//生成一共隨機(jī)的uuid
? ? ? ? ? ? ? ? ? ? //==========================創(chuàng)建存放目錄========================//
? ? ? ? ? ? ? ? ? ? String realPath= uploadPath+"/"+uuidPath;
? ? ? ? ? ? ? ? ? ? //給每個文件創(chuàng)建一個對應(yīng)的文件夾
? ? ? ? ? ? ? ? ? ? File realPathFile = new File(realPath);
? ? ? ? ? ? ? ? ? ? if (!realPathFile.exists()){
? ? ? ? ? ? ? ? ? ? ? ? realPathFile.mkdirs();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //==========================文件傳輸====================================//
? ? ? ? ? ? ? ? ? ? //獲取文件上傳的流
? ? ? ? ? ? ? ? ? ? InputStream inputStream = fileItem.getInputStream();
? ? ? ? ? ? ? ? ? ? //創(chuàng)建一個輸出文件的流
? ? ? ? ? ? ? ? ? ? FileOutputStream fos = new FileOutputStream(realPath + "/" + FileName);
? ? ? ? ? ? ? ? ? ? //創(chuàng)建緩沖區(qū)
? ? ? ? ? ? ? ? ? ? byte[] buffer=new byte[1024];
? ? ? ? ? ? ? ? ? ? //判斷是否讀取完畢
? ? ? ? ? ? ? ? ? ? int len=0;
? ? ? ? ? ? ? ? ? ? while ((len=inputStream.read(buffer))>0){
? ? ? ? ? ? ? ? ? ? ? ? fos.write(buffer,0,len);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //關(guān)閉流
? ? ? ? ? ? ? ? ? ? fos.close();
? ? ? ? ? ? ? ? ? ? inputStream.close();
? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch (FileUploadException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}7.注冊Servlet
<servlet> ? ? <servlet-name>FileServlet</servlet-name> ? ? <servlet-class>com.kuang.servlet.FileServlet</servlet-class> </servlet> <servlet-mapping> ? ? <servlet-name>FileServlet</servlet-name> ? ? <url-pattern>/upload.do</url-pattern> </servlet-mapping>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaWeb實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- JavaWeb實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解
- JavaWeb文件上傳下載實(shí)例講解(酷炫的文件上傳技術(shù))
- JavaWeb項(xiàng)目實(shí)現(xiàn)文件上傳動態(tài)顯示進(jìn)度實(shí)例
- JavaWeb實(shí)現(xiàn)多文件上傳及zip打包下載
- JavaWeb中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- JavaWeb實(shí)現(xiàn)文件上傳與下載的方法
- javaweb實(shí)現(xiàn)文件上傳示例代碼
- JavaWeb實(shí)現(xiàn)文件上傳與下載實(shí)例詳解
- JavaWeb文件上傳與下載功能解析
相關(guān)文章
SpringCloud Feign傳遞HttpServletRequest對象流程
HttpServletRequest接口的對象代表客戶端的請求,當(dāng)客戶端通過HTTP協(xié)議訪問Tomcat服務(wù)器時,HTTP請求中的所有信息都封裝在HttpServletRequest接口的對象中,這篇文章介紹了Feign傳遞HttpServletRequest對象的流程,感興趣的同學(xué)可以參考下文2023-05-05
基于Java實(shí)現(xiàn)回調(diào)監(jiān)聽工具類
這篇文章主要為大家詳細(xì)介紹了如何基于Java實(shí)現(xiàn)一個回調(diào)監(jiān)聽工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
java解析json復(fù)雜數(shù)據(jù)的方法詳解
這篇文章主要為大家詳細(xì)介紹了java解析json復(fù)雜數(shù)據(jù)的兩種常用方法,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,需要的小伙伴可以了解下2024-01-01
JAVA構(gòu)造函數(shù)不能使用void關(guān)鍵字問題
這篇文章主要介紹了JAVA構(gòu)造函數(shù)不能使用void關(guān)鍵字問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Java多線程程序中synchronized修飾方法的使用實(shí)例
synchronized關(guān)鍵字主要北用來進(jìn)行線程同步,這里我們主要來演示Java多線程程序中synchronized修飾方法的使用實(shí)例,需要的朋友可以參考下:2016-06-06

