javaweb實(shí)現(xiàn)文件上傳功能
本文實(shí)例為大家分享了javaweb實(shí)現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
1、創(chuàng)建一個(gè)空項(xiàng)目
2、新建一個(gè)web application 的Module
3、創(chuàng)建一個(gè)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)建目錄 ? ? ? ? } ? ? ? ? //緩存,臨時(shí)文件 ? ? ? ? //臨時(shí)文件,假如文件超出預(yù)期大小,就把它放到臨時(shí)文件夾中,過幾天自動(dòng)刪除,或者提醒用戶轉(zhuǎn)存為永久文件 ? ? ? ? String tmpPath = this.getServletContext().getRealPath("/WEB-INF/tmp"); ? ? ? ? File tmpFile = new File(tmpPath); ? ? ? ? if (!tmpFile.exists()){ ? ? ? ? ? ? tmpFile.mkdirs();//如果不存在則創(chuàng)建臨時(shí)目錄 ? ? ? ? } ? ? ? ? //1.創(chuàng)建DiskFileItemFactory對(duì)象,處理文件上傳路徑或者大小限制 ? ? ? ? DiskFileItemFactory factory = new DiskFileItemFactory(); ? ? ? ? //通過這個(gè)工廠設(shè)置一個(gè)緩沖區(qū),當(dāng)上傳的文件大于這個(gè)緩沖區(qū)的時(shí)候,將它放在臨時(shí)文件中 ? ? ? ? factory.setSizeThreshold(1024*1024);//緩沖區(qū)大小為1M ? ? ? ? factory.setRepository(tmpFile);//臨時(shí)文件的保存目錄 ? ? ? ? //2.獲取ServletFileUpload對(duì)象 ? ? ? ? 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è)置單個(gè)文件的最大值 ? ? ? ? upload.setFileSizeMax(1024*1024*10);//10M ? ? ? ? //設(shè)置總共能夠上傳文件的大小 ? ? ? ? upload.setSizeMax(1024*1024*10);//10M ? ? ? ? //3.處理上傳文件 ? ? ? ? ? ? //把前端請(qǐng)求解析,封裝成一個(gè)FileItem對(duì)象 ? ? ? ? ? ? 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);//從最后一個(gè)/后開始截取 ? ? ? ? ? ? ? ? ? ? //截取后綴名 ? ? ? ? ? ? ? ? ? ? String fileExtName=uploadFileName.substring(uploadFileName.lastIndexOf(".")+1);//從最后一個(gè).后開始截取 ? ? ? ? ? ? ? ? ? ? //網(wǎng)絡(luò)傳輸中的東西,都需要序列化 ? ? ? ? ? ? ? ? ? ? //POJO , 實(shí)體類, 如果想要在多個(gè)電腦運(yùn)行, 傳輸-->需要把對(duì)象序列化 ? ? ? ? ? ? ? ? ? ? //JNI= java native Interface ? ? ? ? ? ? ? ? ? ? //implements Serializable : 標(biāo)記接口 , JVM-->java棧 本地方法棧 native -->C++ ? ? ? ? ? ? ? ? ? ? //可以使用UUID(唯一標(biāo)識(shí)的通用碼),保證文件名唯一 ? ? ? ? ? ? ? ? ? ? String uuidPath = UUID.randomUUID().toString();//生成一共隨機(jī)的uuid ? ? ? ? ? ? ? ? ? ? //==========================創(chuàng)建存放目錄========================// ? ? ? ? ? ? ? ? ? ? String realPath= uploadPath+"/"+uuidPath; ? ? ? ? ? ? ? ? ? ? //給每個(gè)文件創(chuàng)建一個(gè)對(duì)應(yīng)的文件夾 ? ? ? ? ? ? ? ? ? ? File realPathFile = new File(realPath); ? ? ? ? ? ? ? ? ? ? if (!realPathFile.exists()){ ? ? ? ? ? ? ? ? ? ? ? ? realPathFile.mkdirs(); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //==========================文件傳輸====================================// ? ? ? ? ? ? ? ? ? ? //獲取文件上傳的流 ? ? ? ? ? ? ? ? ? ? InputStream inputStream = fileItem.getInputStream(); ? ? ? ? ? ? ? ? ? ? //創(chuàng)建一個(gè)輸出文件的流 ? ? ? ? ? ? ? ? ? ? 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.注冊(cè)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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaWeb實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- JavaWeb實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解
- JavaWeb文件上傳下載實(shí)例講解(酷炫的文件上傳技術(shù))
- JavaWeb項(xiàng)目實(shí)現(xiàn)文件上傳動(dòng)態(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)文章
Java8實(shí)現(xiàn)任意參數(shù)的鏈棧
這篇文章主要為大家詳細(xì)介紹了Java8實(shí)現(xiàn)任意參數(shù)的鏈棧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10SpringBoot中?Jackson?日期的時(shí)區(qū)和日期格式問題解決
因?yàn)樽罱?xiàng)目需要國際化,需要能夠支持多種國際化語言,目前需要支持三種(法語、英語、簡(jiǎn)體中文),這篇文章主要介紹了SpringBoot中?Jackson?日期的時(shí)區(qū)和日期格式問題,需要的朋友可以參考下2022-12-12java中用數(shù)組實(shí)現(xiàn)環(huán)形隊(duì)列的示例代碼
這篇文章主要介紹了java中用數(shù)組實(shí)現(xiàn)環(huán)形隊(duì)列的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Spring Cloud 覆寫遠(yuǎn)端的配置屬性實(shí)例詳解
這篇文章主要介紹了Spring Cloud 覆寫遠(yuǎn)端的配置屬性的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-01-01SpringCloud HystrixDashboard服務(wù)監(jiān)控詳解
Hystrix Dashboard 是Spring Cloud中查看Hystrix實(shí)例執(zhí)行情況的一種儀表盤組件,支持查看單個(gè)實(shí)例和查看集群實(shí)例,本文將對(duì)其服務(wù)監(jiān)控學(xué)習(xí)2022-11-11IDEA Maven Mybatis generator 自動(dòng)生成代碼(實(shí)例講解)
下面小編就為大家分享一篇IDEA Maven Mybatis generator 自動(dòng)生成代碼的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12