Apache Commons fileUpload實(shí)現(xiàn)文件上傳之一
廢話不多說了,直奔主題了。
需要兩個jar包:
commons-fileupload.jar
Commons IO的jar包(本文使用commons-io-2.4.jar)
利用Servlet來實(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\\"; // 臨時文件目錄 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)出錯頁面 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"對應(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>
這樣就能簡單實(shí)現(xiàn)一個文件上傳功能了,當(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文件上傳多個示例分享
- Apache Commons DbUtils工具包使用介紹
- apache commons工具集代碼詳解
相關(guān)文章
Java中BigDecimal的equals方法和compareTo方法的區(qū)別詳析
這篇文章主要給大家介紹了關(guān)于Java中BigDecimal的equals方法和compareTo方法區(qū)別的相關(guān)資料,對于BigDecimal的大小比較,用equals方法的話會不僅會比較值的大小,還會比較兩個對象的精確度,而compareTo方法則不會比較精確度,只比較數(shù)值的大小,需要的朋友可以參考下2023-11-11Spring事務(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中判斷兩個浮點(diǎn)型(float)數(shù)據(jù)是否相等的案例
這篇文章主要介紹了在java中判斷兩個浮點(diǎn)型(float)數(shù)據(jù)是否相等的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Java鏈接redis_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java鏈接redis,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08SpringMVC?Restful風(fēng)格與中文亂碼問題解決方案介紹
Restful就是一個資源定位及資源操作的風(fēng)格,不是標(biāo)準(zhǔn)也不是協(xié)議,只是一種風(fēng)格,是對http協(xié)議的詮釋,下面這篇文章主要給大家介紹了關(guān)于SpringMVC對Restful風(fēng)格支持的相關(guān)資料,需要的朋友可以參考下2022-10-10springboot如何使用assembly打包項(xiàng)目和啟動腳本
這篇文章主要介紹了springboot如何使用assembly打包項(xiàng)目和啟動腳本問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06