java 文件上傳
阿新 • • 發佈:2018-08-08
cep exist mkdir ota xtend 獲取 comm 表單元 integer
package servlet; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; 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 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; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import entit.News; import service.NewService; import service.impl.NewServiceImpl;/** * Servlet implementation class newsServlet */ @WebServlet("/uploadFileServlet") public class newsServlet extends HttpServlet { private static final long serialVersionUID = 1L; public newsServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); }protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String uplaodPath = null; response.setCharacterEncoding("utf-8"); News news = new News(); DiskFileItemFactory factory= new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); boolean flag = upload.isMultipartContent(request); if (flag) { // 證明文件上傳請求 try { // 獲取的是所有的表單元素 包括普通元素和文件元素 List<FileItem> items = upload.parseRequest(request); // List<FileItem> 是一個元素嗎? 是4個??有可能N個??? 遍歷!!!! Iterator<FileItem> its = items.iterator(); while (its.hasNext()) { // 遍歷集合 // 獲取一個一個的元素 FileItem item = its.next(); if (item.isFormField()) { // 普通字段 String fileName = item.getFieldName(); // 獲取name屬性值 if (fileName.equals("id")) { news.setNewsid(Integer.parseInt(item.getString("utf-8"))); } else if (fileName.equals("title")) { news.setNewstitle(item.getString("utf-8")); } else if (fileName.equals("author")) { news.setNewssummary(item.getString("utf-8")); } } else { // 文件元素 // 獲取服務器所在的位置 uplaodPath = request.getSession().getServletContext().getRealPath("uploadFiles/"); File file = new File(uplaodPath); if (!file.exists()) { // 如果創建文件夾不存雜 則創建 file.mkdirs(); } // 獲取上傳文件的名稱 String fileName = item.getName(); // 用戶有可能不上傳文件 if (!fileName.equals("") && fileName != null) { File uploadFile = new File(fileName); // 獲取上傳文件 File saveFile = new File(uplaodPath, uploadFile.getName());// 拼接文件位置 // 真正的上傳 item.write(saveFile); news.setNewspic(uploadFile.getName()); // 給對象賦值 } } } NewService service = new NewServiceImpl(); int num = service.addNew(news); if (num > 0) { System.out.println(uplaodPath); } else { System.out.println("新增失敗"); } } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("不是文件上傳請求!"); } } }
使用的時候需要導入倆個jar
java 文件上傳