- blic class ReceiveFile extends HttpServlet {
- private String uploadPath = "uploadpic/upload/"; // 上傳文件的目錄
- private String tempPath = "uploadpic/uploadtmp/"; // 臨時文件目錄
- private String serverPath = null;
- private String[] fileType = new String[]{".jpg",".gif",".bmp",".png",".jpeg",".ico"};
- private int sizeMax = 5;//圖片最大上限
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // 服務器端根目錄
- String serverPath = getServletContext().getRealPath("/").replace("\\", "/");
- // System.out.println(serverPath);
- //Servlet初始化時執行,如果上傳文件目錄不存在則自動創建
- if(!new File(serverPath+uploadPath).isDirectory()){
- new File(serverPath+uploadPath).mkdirs();
- }
- if(!new File(serverPath+tempPath).isDirectory()){
- new File(serverPath+tempPath).mkdirs();
- }
- DiskFileItemFactory factory = new DiskFileItemFactory();
- factory.setSizeThreshold(5*1024); //最大緩存
- factory.setRepository(new File(serverPath+tempPath));//臨時文件目錄
- ServletFileUpload upload = new ServletFileUpload(factory);
- upload.setSizeMax(sizeMax*1024*1024);//文件最大上限
- String filePath = null;
- try {
- List<FileItem> items = upload.parseRequest(request);//獲取所有文件列表
- //
- for (int i=0;i<items.size();i++) {
- //裏面一個for循環,獲取一行的數據
- FileItem item = items.get(i);
- <span style="white-space:pre"> </span> if(!item.isFormField()){//文件名
- String fileName = item.getName().toLowerCase();
- if(fileName.endsWith(fileType[0])||fileName.endsWith(fileType[1])||fileName.endsWith(fileType[2])||fileName.endsWith(fileType[3])||fileName.endsWith(fileType[4])||fileName.endsWith(fileType[5])){
- // String uuid = UUID.randomUUID().toString();
- filePath = serverPath+uploadPath+fileName;
- // System.out.println(filePath);
- File file = new File(filePath);
- item.write(file);
- System.out.println(fileName);
- }else {
- request.setAttribute("errorMsg", "上傳失敗,請確認上傳的文件存在並且類型是圖片!");
- request.getRequestDispatcher("uploaderror.jsp").forward(request,response);
- }
- }else {
- //非文件流
- String value=item.getString();
- value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
- // System.out.println(value);
- System.out.println(value);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- request.setAttribute("errorMsg", "上傳失敗,請確認上傳的文件存在並且類型是圖片!");
- request.getRequestDispatcher("uploaderror.jsp").forward(request,response);
- }
- }
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- this.doPost(req, resp);
- }
- }
- public class ReceiveFile extends HttpServlet {
- private String uploadPath = "uploadpic/upload/"; // 上傳文件的目錄
- private String tempPath = "uploadpic/uploadtmp/"; // 臨時文件目錄
- private String serverPath = null;
- private String[] fileType = new String[]{".jpg",".gif",".bmp",".png",".jpeg",".ico"};
- private int sizeMax = 5;//圖片最大上限
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // 服務器端根目錄
- String serverPath = getServletContext().getRealPath("/").replace("\\", "/");
- // System.out.println(serverPath);
- //Servlet初始化時執行,如果上傳文件目錄不存在則自動創建
- if(!new File(serverPath+uploadPath).isDirectory()){
- new File(serverPath+uploadPath).mkdirs();
- }
- if(!new File(serverPath+tempPath).isDirectory()){
- new File(serverPath+tempPath).mkdirs();
- }
- DiskFileItemFactory factory = new DiskFileItemFactory();
- factory.setSizeThreshold(5*1024); //最大緩存
- factory.setRepository(new File(serverPath+tempPath));//臨時文件目錄
- ServletFileUpload upload = new ServletFileUpload(factory);
- upload.setSizeMax(sizeMax*1024*1024);//文件最大上限
- String filePath = null;
- try {
- List<FileItem> items = upload.parseRequest(request);//獲取所有文件列表
- //
- for (int i=0;i<items.size();i++) {
- //裏面一個for循環,獲取一行的數據
- FileItem item = items.get(i);
- <span style="white-space:pre"> </span> if(!item.isFormField()){//文件名
- String fileName = item.getName().toLowerCase();
- if(fileName.endsWith(fileType[0])||fileName.endsWith(fileType[1])||fileName.endsWith(fileType[2])||fileName.endsWith(fileType[3])||fileName.endsWith(fileType[4])||fileName.endsWith(fileType[5])){
- // String uuid = UUID.randomUUID().toString();
- filePath = serverPath+uploadPath+fileName;
- // System.out.println(filePath);
- File file = new File(filePath);
- item.write(file);
- System.out.println(fileName);
- }else {
- request.setAttribute("errorMsg", "上傳失敗,請確認上傳的文件存在並且類型是圖片!");
- request.getRequestDispatcher("uploaderror.jsp").forward(request,response);
- }
- }else {
- //非文件流
- String value=item.getString();
- value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
- // System.out.println(value);
- System.out.println(value);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- request.setAttribute("errorMsg", "上傳失敗,請確認上傳的文件存在並且類型是圖片!");
- request.getRequestDispatcher("uploaderror.jsp").forward(request,response);
- }
- }
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- this.doPost(req, resp);
- }
- }
- public class ReceiveFile extends HttpServlet {
- private String uploadPath = "uploadpic/upload/"; // 上傳文件的目錄
- private String tempPath = "uploadpic/uploadtmp/"; // 臨時文件目錄
- private String serverPath = null;
- private String[] fileType = new String[]{".jpg",".gif",".bmp",".png",".jpeg",".ico"};
- private int sizeMax = 5;//圖片最大上限
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // 服務器端根目錄
- String serverPath = getServletContext().getRealPath("/").replace("\\", "/");
- // System.out.println(serverPath);
- //Servlet初始化時執行,如果上傳文件目錄不存在則自動創建
- if(!new File(serverPath+uploadPath).isDirectory()){
- new File(serverPath+uploadPath).mkdirs();
- }
- if(!new File(serverPath+tempPath).isDirectory()){
- new File(serverPath+tempPath).mkdirs();
- }
- DiskFileItemFactory factory = new DiskFileItemFactory();
- factory.setSizeThreshold(5*1024); //最大緩存
- factory.setRepository(new File(serverPath+tempPath));//臨時文件目錄
- ServletFileUpload upload = new ServletFileUpload(factory);
- upload.setSizeMax(sizeMax*1024*1024);//文件最大上限
- String filePath = null;
- try {
- List<FileItem> items = upload.parseRequest(request);//獲取所有文件列表
- //
- for (int i=0;i<items.size();i++) {
- //裏面一個for循環,獲取一行的數據
- FileItem item = items.get(i);
- <span style="white-space:pre"> </span> if(!item.isFormField()){//文件名
- String fileName = item.getName().toLowerCase();
- if(fileName.endsWith(fileType[0])||fileName.endsWith(fileType[1])||fileName.endsWith(fileType[2])||fileName.endsWith(fileType[3])||fileName.endsWith(fileType[4])||fileName.endsWith(fileType[5])){
- // String uuid = UUID.randomUUID().toString();
- filePath = serverPath+uploadPath+fileName;
- // System.out.println(filePath);
- File file = new File(filePath);
- item.write(file);
- System.out.println(fileName);
- }else {
- request.setAttribute("errorMsg", "上傳失敗,請確認上傳的文件存在並且類型是圖片!");
- request.getRequestDispatcher("uploaderror.jsp").forward(request,response);
- }
- }else {
- //非文件流
- String value=item.getString();
- value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
- // System.out.println(value);
- System.out.println(value);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- request.setAttribute("errorMsg", "上傳失敗,請確認上傳的文件存在並且類型是圖片!");
- request.getRequestDispatcher("uploaderror.jsp").forward(request,response);
- }
- }
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- this.doPost(req, resp);
- }
- }