基于javaweb+mysql的jsp+servlet超市商品会员管理系统(java+mysql+servlet+jsp+bootstrap)

基于javaweb+mysql的jsp+servlet超市商品会员管理系统(java+mysql+servlet+jsp+bootstrap)

私信源码获取及调试交流

运行环境

Java≥8、MySQL≥5.7、Tomcat≥8

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

基于javaweb+mysql的JSP+Servlet超市商品会员管理系统(java+mysql+servlet+jsp+bootstrap)

登录:

admin 123456

		supGoods.setWeight(Double.parseDouble(weight));
		supGoods.setStock(Integer.parseInt(stock));
		supGoods.setExpirationDate(Integer.parseInt(expirationDate));
		if (supGoods.getId()!=null && !"".equals(supGoods.getId())) {
   			supGoodsService.update(supGoods);
   		}else{
   			supGoodsService.add(supGoods);
   		}
		response.sendRedirect(contextPath+"/supGoods?method=list");
	}
	
	//列表查询
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.err.println("---开始查询---");
		SupGoods supGoods = new SupGoods();
		//分页有关
		Page<SupGoods> page = new Page<SupGoods>();
		//设置查询页
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null && pageNoStr != "") {
			page.setPageNo(Integer.parseInt(pageNoStr));
		}
		//设置查询条件
		String goodsName = request.getParameter("goodsName");
		if (goodsName != null && goodsName != "") {
			supGoods.setGoodsName(goodsName);
			request.setAttribute("goodsName", goodsName);
		}
		String goodsType = request.getParameter("goodsType");
		if (goodsType != null && goodsType != "") {
			supGoods.setGoodsType(goodsType);
			request.setAttribute("goodsType", goodsType);
		}
		
		//判断提示信息
		Object msg = request.getSession().getAttribute("msg");
		if (msg != null) {
			request.setAttribute("msg", msg.toString());
			request.getSession().removeAttribute("msg");
		}
		
		page = supGoodsService.page(supGoods, page);
		request.setAttribute("page", page);
		request.getRequestDispatcher("/views/system/supGoodsList.jsp").forward(request, response);
	}
	
	//form跳转页面
	private void form(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		SupGoods supGoods = new SupGoods();
		if (id!=null && id!="") {
					
					if ("vipType".endsWith(name)) {
						supVipInfo.setVipType(item.getString("UTF-8"));
						
					}
					
					if ("vipScore".endsWith(name)) {
						supVipInfo.setVipScore(Integer.parseInt(item.getString("UTF-8")));
						
					}
                    
                }else{//如果fileitem中封装的是上传文件
                    //得到上传的文件名称,
                    String filename = item.getName();
                    System.out.println(filename);
                    if(filename==null || filename.trim().equals("")){
                        continue;
                    }
                    //注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如:  c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
                    //处理获取到的上传文件的文件名的路径部分,只保留文件名部分
                    filename = filename.substring(filename.lastIndexOf("\\")+1);
                    //获取item中的上传文件的输入流
                    InputStream in = item.getInputStream();
                    //创建一个文件输出流
                    FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
                    //创建一个缓冲区
                    byte buffer[] = new byte[1024];
                    //判断输入流中的数据是否已经读完的标识
                    int len = 0;
                    //循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
                    while((len=in.read(buffer))>0){
                        //使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
                        out.write(buffer, 0, len);
                    }
                    //关闭输入流
                    in.close();
                    //关闭输出流
                    out.close();
                    //删除处理文件上传时生成的临时文件
               //     item.delete();
 					msg = "文件上传成功!";
                    //supVipInfo.setPic("\\upload" + "\\" + filename);
                }
            }
            if (supVipInfo.getId()!=null && !"".equals(supVipInfo.getId())) {
    			supVipInfoService.update(supVipInfo);
    		}else{
    			supVipInfoService.add(supVipInfo);
    		}
    		response.sendRedirect(contextPath+"/supVipInfo?method=list");
        }catch (Exception e) {
            msg= "服务异常!";
            e.printStackTrace();
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		contextPath = request.getServletContext().getContextPath();
		String method = request.getParameter("method");
		if ("add".equals(method)) {
			add(request,response);
		}else if ("delete".equals(method)) {
			delete(request, response);
		}else if ("list".equals(method)) {
			list(request, response);
		}else if ("update".equals(method)) {
			update(request, response);
		}else if ("form".equals(method)) {
			form(request, response);
		}else if ("save".equals(method)) {
			save(request, response);
		}else if ("login".equals(method)) {
			login(request, response);
		}
		
	}
	
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		User login = userService.login(username, password);
		if (login!=null) {
		}else{
		}
	}

	//添加
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
			list(request, response);
		}else if ("form".equals(method)) {
			form(request, response);
		}else if ("saveAndFile".equals(method)) {
			saveAndFile(request, response);
		}
		
	}
	
	//添加保存
	protected void saveAndFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SupActivePro supActivePro = new SupActivePro();
		 //这是上传到tomcat下,文件容易丢失
		/* //得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全
        String savePath = this.getServletContext().getRealPath("/upload");
        File file = new File(savePath);
        //判断上传文件的保存目录是否存在
        if (!file.exists() && !file.isDirectory()) {
            System.out.println(savePath+"目录不存在,需要创建");
            //创建目录
            file.mkdir();
        }*/
        //上传到服务器硬盘上,保证重启tomcat不会丢失文件
        //获取上传文件的路径
        String savePath = PropertiesUtil.getValue("fileUpload");
        File file = new File(savePath+"/");
        if (!file.exists() && !file.isDirectory()) {
            System.out.println(savePath+"目录不存在,需要创建");
            //创建目录
            file.mkdirs();
        }
        //消息提示
        String msg = "";
        try{
            //使用Apache文件上传组件处理文件上传步骤:
            //1、创建一个DiskFileItemFactory工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //2、创建一个文件上传解析器
            ServletFileUpload upload = new ServletFileUpload(factory);
             //解决上传文件名的中文乱码
            upload.setHeaderEncoding("UTF-8"); 
            //3、判断提交上来的数据是否是上传表单的数据
		}else{
		}
	}

	//添加
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String phone = request.getParameter("phone");
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setName(name);
		user.setSex(sex);
		user.setPhone(phone);
		userService.add(user);
	}
	
	//添加保存
	protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = new User();
		 //得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全
        String savePath = this.getServletContext().getRealPath("/upload");
        File file = new File(savePath);
        //判断上传文件的保存目录是否存在
        if (!file.exists() && !file.isDirectory()) {
            System.out.println(savePath+"目录不存在,需要创建");
            //创建目录
            file.mkdir();
        }
        //消息提示
        String msg = "";
        try{
            //使用Apache文件上传组件处理文件上传步骤:
            //1、创建一个DiskFileItemFactory工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //2、创建一个文件上传解析器
            ServletFileUpload upload = new ServletFileUpload(factory);
             //解决上传文件名的中文乱码
            upload.setHeaderEncoding("UTF-8"); 
            //3、判断提交上来的数据是否是上传表单的数据
            if(!ServletFileUpload.isMultipartContent(request)){
                //按照传统方式获取数据
		SupVipInfo supVipInfo = new SupVipInfo();
		 //这是上传到tomcat下,文件容易丢失
		/* //得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全
        String savePath = this.getServletContext().getRealPath("/upload");
        File file = new File(savePath);
        //判断上传文件的保存目录是否存在
        if (!file.exists() && !file.isDirectory()) {
            System.out.println(savePath+"目录不存在,需要创建");
            //创建目录
            file.mkdir();
        }*/
        //上传到服务器硬盘上,保证重启tomcat不会丢失文件
        //获取上传文件的路径
        String savePath = PropertiesUtil.getValue("fileUpload");
        File file = new File(savePath+"/");
        if (!file.exists() && !file.isDirectory()) {
            System.out.println(savePath+"目录不存在,需要创建");
            //创建目录
            file.mkdirs();
        }
        //消息提示
        String msg = "";
        try{
            //使用Apache文件上传组件处理文件上传步骤:
            //1、创建一个DiskFileItemFactory工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //2、创建一个文件上传解析器
            ServletFileUpload upload = new ServletFileUpload(factory);
             //解决上传文件名的中文乱码
            upload.setHeaderEncoding("UTF-8"); 
            //3、判断提交上来的数据是否是上传表单的数据
            if(!ServletFileUpload.isMultipartContent(request)){
                //按照传统方式获取数据
                return;
            }
            //4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,每一个FileItem对应一个Form表单的输入项
            List<FileItem> list = upload.parseRequest(request);
            for(FileItem item : list){
                //如果fileitem中封装的是普通输入项的数据
                if(item.isFormField()){
                    String name = item.getFieldName();
                     //解决普通输入项的数据的中文乱码问题
					if ("id".endsWith(name)) {
                    	if (item.getString("UTF-8")!=null && item.getString("UTF-8")!="") {
                    		supVipInfo.setId(item.getString("UTF-8"));
						}
					}
					
					if ("vipNo".endsWith(name)) {
						supVipInfo.setVipNo(item.getString("UTF-8"));
						
					}
					
					if ("vipName".endsWith(name)) {
            request.getRequestDispatcher("/views/system/supActiveProForm.jsp").forward(request, response);
        }
	}
	
	//删除
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		supActiveProService.delete(id);
		response.sendRedirect(contextPath+"/supActivePro?method=list");
	}
	
	//修改
	private void saveNoFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String activeType = request.getParameter("activeType");
		SupActivePro supActivePro = new SupActivePro();
		supActivePro.setId(id);
		supActivePro.setTitle(title);
		supActivePro.setContent(content);
		supActivePro.setStartTime(startTime);
		supActivePro.setEndTime(endTime);
		supActivePro.setActiveType(activeType);
		if (supActivePro.getId()!=null && !"".equals(supActivePro.getId())) {
   			supActiveProService.update(supActivePro);
   		}else{
   			supActiveProService.add(supActivePro);
   		}
		response.sendRedirect(contextPath+"/supActivePro?method=list");
	}
	
	//列表查询
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.err.println("---开始查询---");
		SupActivePro supActivePro = new SupActivePro();
		//分页有关
		Page<SupActivePro> page = new Page<SupActivePro>();
		//设置查询页
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null && pageNoStr != "") {
			page.setPageNo(Integer.parseInt(pageNoStr));
		}
					if ("id".endsWith(name)) {
                    	if (item.getString("UTF-8")!=null && item.getString("UTF-8")!="") {
                    		supVipInfo.setId(item.getString("UTF-8"));
						}
					}
					
					if ("vipNo".endsWith(name)) {
						supVipInfo.setVipNo(item.getString("UTF-8"));
						
					}
					
					if ("vipName".endsWith(name)) {
						supVipInfo.setVipName(item.getString("UTF-8"));
						
					}
					
					if ("vipPhone".endsWith(name)) {
						supVipInfo.setVipPhone(item.getString("UTF-8"));
						
					}
					
					if ("vipTime".endsWith(name)) {
						supVipInfo.setVipTime(item.getString("UTF-8"));
						
					}
					
					if ("vipType".endsWith(name)) {
						supVipInfo.setVipType(item.getString("UTF-8"));
						
					}
					
					if ("vipScore".endsWith(name)) {
						supVipInfo.setVipScore(Integer.parseInt(item.getString("UTF-8")));
						
					}
                    
                }else{//如果fileitem中封装的是上传文件
                    //得到上传的文件名称,
                    String filename = item.getName();
                    System.out.println(filename);
                    if(filename==null || filename.trim().equals("")){
                        continue;
                    }
                    //注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如:  c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
                    //处理获取到的上传文件的文件名的路径部分,只保留文件名部分
                    filename = filename.substring(filename.lastIndexOf("\\")+1);
                    //获取item中的上传文件的输入流
                    InputStream in = item.getInputStream();
                    //创建一个文件输出流
                    FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
                    //创建一个缓冲区
                    byte buffer[] = new byte[1024];
                    //判断输入流中的数据是否已经读完的标识
                    int len = 0;
		String title = request.getParameter("title");
		if (title != null && title != "") {
			supActivePro.setTitle(title);
			request.setAttribute("title", title);
		}
		String activeType = request.getParameter("activeType");
		if (activeType != null && activeType != "") {
			supActivePro.setActiveType(activeType);
			request.setAttribute("activeType", activeType);
		}
		
		//判断提示信息
		Object msg = request.getSession().getAttribute("msg");
		if (msg != null) {
			request.setAttribute("msg", msg.toString());
			request.getSession().removeAttribute("msg");
		}
		
		page = supActiveProService.page(supActivePro, page);
		request.setAttribute("page", page);
		request.getRequestDispatcher("/views/system/supActiveProList.jsp").forward(request, response);
	}
	
	//form跳转页面
	private void form(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		SupActivePro supActivePro = new SupActivePro();
		if (id!=null && id!="") {
			supActivePro = supActiveProService.getById(id);
		}
		request.setAttribute("supActivePro", supActivePro);
		request.getRequestDispatcher("/views/system/supActiveProForm.jsp").forward(request, response);
	}
	
}

					
					if ("vipNo".endsWith(name)) {
						supVipInfo.setVipNo(item.getString("UTF-8"));
						
					}
					
					if ("vipName".endsWith(name)) {
						supVipInfo.setVipName(item.getString("UTF-8"));
						
					}
					
					if ("vipPhone".endsWith(name)) {
						supVipInfo.setVipPhone(item.getString("UTF-8"));
						
					}
					
					if ("vipTime".endsWith(name)) {
						supVipInfo.setVipTime(item.getString("UTF-8"));
						
					}
					
					if ("vipType".endsWith(name)) {
						supVipInfo.setVipType(item.getString("UTF-8"));
						
					}
					
					if ("vipScore".endsWith(name)) {
						supVipInfo.setVipScore(Integer.parseInt(item.getString("UTF-8")));
						
					}
                    
                }else{//如果fileitem中封装的是上传文件
                    //得到上传的文件名称,
                    String filename = item.getName();
                    System.out.println(filename);
                    if(filename==null || filename.trim().equals("")){
                        continue;
                    }
                    //注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如:  c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
                    //处理获取到的上传文件的文件名的路径部分,只保留文件名部分
						}
					}
					
					if ("username".endsWith(name)) {
						user.setUsername(item.getString("UTF-8"));
						
					}
					
					if ("password".endsWith(name)) {
						user.setPassword(item.getString("UTF-8"));
						
					}
					
					if ("trueName".endsWith(name)) {
						user.setName(item.getString("UTF-8"));
						
					}
					
					if ("sex".endsWith(name)) {
						user.setSex(item.getString("UTF-8"));
						
					}
					
					if ("phone".endsWith(name)) {
						user.setPhone(item.getString("UTF-8"));
						
					}
                    
                }else{//如果fileitem中封装的是上传文件
                    //得到上传的文件名称,
                    String filename = item.getName();
                    System.out.println(filename);
                    if(filename==null || filename.trim().equals("")){
                        continue;
                    }
                    //注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如:  c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
                    //处理获取到的上传文件的文件名的路径部分,只保留文件名部分
                    filename = filename.substring(filename.lastIndexOf("\\")+1);
                    //获取item中的上传文件的输入流
                    InputStream in = item.getInputStream();
                    //创建一个文件输出流
                    FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
                    //创建一个缓冲区
                    byte buffer[] = new byte[1024];
                    //判断输入流中的数据是否已经读完的标识
                    int len = 0;
                    //循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
                    while((len=in.read(buffer))>0){
                        //使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
                        out.write(buffer, 0, len);
        }
        //消息提示
        String msg = "";
        try{
            //使用Apache文件上传组件处理文件上传步骤:
            //1、创建一个DiskFileItemFactory工厂
            DiskFileItemFactory factory = new DiskFileItemFactory();
            //2、创建一个文件上传解析器
            ServletFileUpload upload = new ServletFileUpload(factory);
             //解决上传文件名的中文乱码
            upload.setHeaderEncoding("UTF-8"); 
            //3、判断提交上来的数据是否是上传表单的数据
            if(!ServletFileUpload.isMultipartContent(request)){
                //按照传统方式获取数据
                return;
            }
            //4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,每一个FileItem对应一个Form表单的输入项
            List<FileItem> list = upload.parseRequest(request);
            for(FileItem item : list){
                //如果fileitem中封装的是普通输入项的数据
                if(item.isFormField()){
                    String name = item.getFieldName();
                     //解决普通输入项的数据的中文乱码问题
					if ("id".endsWith(name)) {
                    	if (item.getString("UTF-8")!=null && item.getString("UTF-8")!="") {
                    		user.setId(item.getString("UTF-8"));
						}
					}
					
					if ("username".endsWith(name)) {
						user.setUsername(item.getString("UTF-8"));
						
					}
					
					if ("password".endsWith(name)) {
						user.setPassword(item.getString("UTF-8"));
						
					}
					
					if ("trueName".endsWith(name)) {
						user.setName(item.getString("UTF-8"));
						
					}
					
					if ("sex".endsWith(name)) {
						user.setSex(item.getString("UTF-8"));
						
					}
					
		
		page = supGoodsService.page(supGoods, page);
		request.setAttribute("page", page);
		request.getRequestDispatcher("/views/system/supGoodsList.jsp").forward(request, response);
	}
	
	//form跳转页面
	private void form(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		SupGoods supGoods = new SupGoods();
		if (id!=null && id!="") {
			supGoods = supGoodsService.getById(id);
		}
		request.setAttribute("supGoods", supGoods);
		request.getRequestDispatcher("/views/system/supGoodsForm.jsp").forward(request, response);
	}
	
}

                    while((len=in.read(buffer))>0){
                        //使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
                        out.write(buffer, 0, len);
                    }
                    //关闭输入流
                    in.close();
                    //关闭输出流
                    out.close();
                    //删除处理文件上传时生成的临时文件
               //     item.delete();
 					msg = "文件上传成功!";
                    //supVipInfo.setPic("\\upload" + "\\" + filename);
                }
            }
            if (supVipInfo.getId()!=null && !"".equals(supVipInfo.getId())) {
    			supVipInfoService.update(supVipInfo);
    		}else{
    			supVipInfoService.add(supVipInfo);
    		}
    		response.sendRedirect(contextPath+"/supVipInfo?method=list");
        }catch (Exception e) {
            msg= "服务异常!";
            e.printStackTrace();
            request.setAttribute("msg",msg);
            request.getRequestDispatcher("/views/system/supVipInfoForm.jsp").forward(request, response);
        }
	}
	
	//删除
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		supVipInfoService.delete(id);
		response.sendRedirect(contextPath+"/supVipInfo?method=list");
	}
	
	//修改
	private void saveNoFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String vipNo = request.getParameter("vipNo");
		String vipName = request.getParameter("vipName");
		String vipPhone = request.getParameter("vipPhone");
		String vipTime = request.getParameter("vipTime");
		String vipType = request.getParameter("vipType");
		String vipScore = request.getParameter("vipScore");
		SupVipInfo supVipInfo = new SupVipInfo();
		supVipInfo.setId(id);
		supVipInfo.setVipNo(vipNo);
		supVipInfo.setVipName(vipName);
		supVipInfo.setVipPhone(vipPhone);

@WebServlet("/supVipInfo")
public class SupVipInfoController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private String contextPath = "";
	
	SupVipInfoDao supVipInfoDao=new SupVipInfoDao();
	SupVipInfoService supVipInfoService = new SupVipInfoServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		contextPath = request.getServletContext().getContextPath();
		String method = request.getParameter("method");
		if ("saveNoFile".equals(method)) {
			saveNoFile(request,response);
		}else if ("delete".equals(method)) {
			delete(request, response);
		}else if ("list".equals(method)) {
			list(request, response);
		}else if ("form".equals(method)) {
			form(request, response);
		}else if ("saveAndFile".equals(method)) {
			saveAndFile(request, response);
		}
		
	}
	
	//添加保存
	protected void saveAndFile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SupVipInfo supVipInfo = new SupVipInfo();
		 //这是上传到tomcat下,文件容易丢失
		/* //得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全
        String savePath = this.getServletContext().getRealPath("/upload");
        File file = new File(savePath);
        //判断上传文件的保存目录是否存在

@WebServlet("/user")
public class UserController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private String contextPath = "";
	
	UserDao userDao=new UserDao();
	UserService userService = new UserServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		contextPath = request.getServletContext().getContextPath();
		String method = request.getParameter("method");
		if ("add".equals(method)) {
			add(request,response);
		}else if ("delete".equals(method)) {
			delete(request, response);
		}else if ("list".equals(method)) {
			list(request, response);
		}else if ("update".equals(method)) {
			update(request, response);
		}else if ("form".equals(method)) {
                    String filename = item.getName();
                    System.out.println(filename);
                    if(filename==null || filename.trim().equals("")){
                        continue;
                    }
                    //注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如:  c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
                    //处理获取到的上传文件的文件名的路径部分,只保留文件名部分
                    filename = filename.substring(filename.lastIndexOf("\\")+1);
                    //获取item中的上传文件的输入流
                    InputStream in = item.getInputStream();
                    //创建一个文件输出流
                    FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
                    //创建一个缓冲区
                    byte buffer[] = new byte[1024];
                    //判断输入流中的数据是否已经读完的标识
                    int len = 0;
                    //循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
                    while((len=in.read(buffer))>0){
                        //使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
                        out.write(buffer, 0, len);
                    }
                    //关闭输入流
                    in.close();
                    //关闭输出流
                    out.close();
                    //删除处理文件上传时生成的临时文件
                //    item.delete();
 					msg = "文件上传成功!";
                    //supActivePro.setPic("\\upload" + "\\" + filename);
                }
            }
            if (supActivePro.getId()!=null && !"".equals(supActivePro.getId())) {
    			supActiveProService.update(supActivePro);
    		}else{
    			supActiveProService.add(supActivePro);
    		}
    		response.sendRedirect(contextPath+"/supActivePro?method=list");
        }catch (Exception e) {
            msg= "服务异常!";
            e.printStackTrace();
            request.setAttribute("msg",msg);
            request.getRequestDispatcher("/views/system/supActiveProForm.jsp").forward(request, response);
        }
	}

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值