apache_fileupload實現檔案上傳_上傳多個檔案

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

1、導包

核心類:

       DiskFileItemFactory – 設定磁碟空間,儲存臨時檔案。只是一個具類。

       ServletFileUpload  - 檔案上傳的核心類,此類接收request,並解析reqeust。

2、設定表單為post,且enctype為multipart/form-data,傳遞的是位元組碼

  //req.getParameter("txt");//返回的是null

1 <form action=‘<c:url value="/UpLoad"></c:url>‘ method="post" enctype="multipart/form-data">2 File:<input type="file" name="file" /><br/>3 <input type="submit" value="上傳" />4 </form>

3、Demo    /Upload

 1 request.setCharacterEncoding("utf-8"); 2         //擷取項目的絕對位址 3         String filepath= getServletContext().getRealPath("/file"); 4         String fileTemp=getServletContext().getRealPath("/temp"); 5         //1、聲明工廠類,用於在指定磁碟上設定一個臨時目錄 6         /* 7          * 第一個參數:sizeThreadHold - 設定緩衝(記憶體)儲存多少位元組資料,預設為10K 8          *                如果一個檔案沒有大於10K,則直接使用記憶體直接儲存成檔案就可以了。 9          *              如果一個檔案大於10K,就需要將檔案先儲存到臨時目錄中去。10          * 第二個參數 File 是指臨時目錄位置11          * 或者無參函數12          * disk.setSizeThreshold(sizeThreshold);13          * disk.setRepository(repository)14          * */15         DiskFileItemFactory disk=new DiskFileItemFactory(1024*10, new File(fileTemp));16         //2、聲明,接收上面的臨時目錄17         ServletFileUpload upload=new ServletFileUpload(disk);18         //3、解析request19         try {20             //隱藏檔資訊21             List<Map<String, String>> imgs=new ArrayList<Map<String,String>>();22             23             List<FileItem> list=upload.parseRequest(request);24             //遍曆多個上傳檔案,如果只有一個上傳檔案可以用list.get(0)獲得25             for(FileItem item:list)26             {27                 Map<String, String> map=new HashMap<String, String>();28                 29                 String fileType= item.getContentType();30                 if(fileType.contains("image/"))31                 {32                     //擷取檔案名稱,getFieldName獲得全路徑33                     String fileName=item.getName();34                     fileName=fileName.substring(fileName.lastIndexOf("\\")+1);35                     //擷取副檔名36                     String extName=fileName.substring(fileName.lastIndexOf("."));37                     fileName=UUID.randomUUID().toString().replace("-", "")+extName;    //組成新檔案名稱            38                     39                     InputStream in= item.getInputStream();40                     int size=in.available();//擷取檔案大小41                     FileUtils.copyInputStreamToFile(in, new File(filepath+"/"+fileName));//使用工具類copy檔案到流中42                     43                     map.put("name", fileName);44                     map.put("size", ""+size);45                     map.put("type",fileType);46                     47                     imgs.add(map);48                     item.delete();//刪除緩衝中緩衝                    49                 }50                 request.setAttribute("list", imgs);51                 request.getRequestDispatcher("/show.jsp").forward(request, response);                52             }        53             54         } catch (Exception e) {55             e.printStackTrace();56         }57         58     }

 

apache_fileupload實現檔案上傳_上傳多個檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.