javaweb 簡單的檔案上傳動態新增上傳檔案數量[從學習到工作(六)]
阿新 • • 發佈:2018-11-28
1.jsp頁面主要程式碼塊
<script type="text/javascript" src="<%=basePath%>js/jquery.min.js"></script>
2.提交到後臺servlet 處理<form action="<%=basePath%>upload" method="post" enctype="multipart/form-data"> File:<input type="file" name="file"/> <input type="button" id="add" value="add"/><br/> <div></div> <input type="submit" value="submit"/> </form> <script type="text/javascript"> $(function() { $('#add').click(function() { var random = Math.random()*10; var file="File:<input type='file' name='file"+random+"'/><br/>"; $("div").append(file); }); }); </script>
<pre name="code" class="java">package com.zenglx.test.servlet; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class UploadServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); //檔案儲存路徑 String path = getServletContext().getRealPath("/up"); //在硬碟上設立一個臨時儲存地方 DiskFileItemFactory dff = new DiskFileItemFactory(); dff.setSizeThreshold(1024*1024); dff.setRepository(new File("f:/a")); //建立檔案上傳的核心類 ServletFileUpload upload = new ServletFileUpload(dff); List<Map<String,String>> list = new ArrayList<Map<String,String>>(); try { //解析request獲得檔案條目 List<FileItem> fileItems = upload.parseRequest(request); //迴圈讀取上傳的檔案資訊 for(FileItem fileItem:fileItems) { Map<String,String> map = new HashMap<String,String>(); String fileName = fileItem.getName(); fileName = fileName.substring(fileName.lastIndexOf("\\")+1); String size = fileItem.getSize()+"byte"; String fileType = fileItem.getContentType(); //獲得檔案字尾名 String extName = fileName.substring(fileName.lastIndexOf("."));//.jpg //UUID String uuid = UUID.randomUUID().toString().replace("-", ""); //得到新的檔名 String newName = uuid+extName; //建立儲存的檔案 File dest = new File(path+File.separator+newName); //將檔案儲存到目標地址 //方式一 /*InputStream is = fileItem.getInputStream(); FileUtils.copyInputStreamToFile(is, dest); */ //方式二 fileItem.write(dest); //刪除臨時檔案 fileItem.delete(); map.put("fileName", fileName); map.put("fileType",fileType); map.put("size",size); list.add(map); } request.setAttribute("list", list); request.getRequestDispatcher("/success.jsp").forward(request, response); } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
3.上傳成功頁面展示
<c:forEach items="${list}" var="info">
檔名稱:<c:out value="${info.fileName}" /><br/>
檔案型別:<c:out value="${info.fileType}"/><br/>
檔案大小:<c:out value="${info.size}"></c:out><br/><br/>
</c:forEach>