Esempio n. 1
0
  // 后台添加管理员   上传头像
  public void insert(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    try {
      // 1实例化SmartUpload对象
      SmartUpload su = new SmartUpload();
      // 2 初始化
      su.initialize(this.getServletConfig(), request, response);
      // 3 上传
      su.upload();
      // 4 原名保存所有文件
      su.save("/uploadImage");

      Request req = su.getRequest();
      String name = req.getParameter("name");
      String password = req.getParameter("password");
      String access = req.getParameter("access");
      String pic = req.getParameter("pic");
      int n = pic.lastIndexOf("\\");
      String p = pic.substring(n + 1, pic.length());
      Manager manager = new Manager();
      manager.setName(name);
      manager.setPassword(password);
      manager.setPic(p);
      manager.setAccess(access);
      ManagerDAO.getDAO().insertManager(manager);
      response.sendRedirect("http://localhost:8080/supermarket/manage/jsp/index.jsp");

    } catch (SmartUploadException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Esempio n. 2
0
  // 添加新闻(实现文件上传)
  public void newsAdd(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String filename = "";

    SmartUpload su = new SmartUpload();
    // 初始化SmartUpload对象
    su.initialize(this.getServletConfig(), request, response);
    com.jspsmart.upload.File file = null;
    // 允许上传类型
    String allowed = "gif,jpg,doc,rar";
    // 不许上传类型
    String denied = "jsp,asp,php,aspx,html,htm,exe,bat";
    com.jspsmart.upload.Request req = null;
    // 设置上传文件大小
    int file_size = 10 * 1024 * 1024; // 10mb
    String exceptionMsg = null;
    int i = 0;
    try {
      // 定义允许上传文件类型
      su.setAllowedFilesList(allowed);
      // 不允许上传文件类型
      su.setDeniedFilesList(denied);
      // 单个文件最大限制
      su.setMaxFileSize(file_size);
      su.setCharset("GBK");
      // 执行上传
      su.upload();
      // 得到单个上传文件的信息
      file = su.getFiles().getFile(0);
      String filepath = null;
      if (!file.isMissing()) {
        // 设置文件在服务器的保存位置

        filepath = "upload\\";
        filename = file.getFileName();
        String ext = filename.substring(filename.lastIndexOf(".") + 1);
        Date date = new Date();
        filename = date.getTime() + "" + (int) (Math.random() * 100000) + "." + ext;
        filepath += filename;
        // 文件另存为
        file.setCharset("gbk");
        file.saveAs(filepath, SmartUpload.SAVE_VIRTUAL);
      }

      // 获取smartupload封装的request
      req = su.getRequest();
      // 获取添加新闻页面传递过来的参数
      int typeId = Integer.parseInt(req.getParameter("typeId"));
      String title = req.getParameter("title");
      String author = req.getParameter("author");
      String summary = req.getParameter("summary");
      String content = req.getParameter("content");
      String pic = filepath; // 获取文件上传后的路径

      NewsInfo newsInfo = new NewsInfo();
      newsInfo.setTypeId(typeId);
      newsInfo.setNewsTitle(title);
      newsInfo.setNewsAut(author);
      newsInfo.setNewsSum(summary);
      newsInfo.setNewsCon(content);
      newsInfo.setNewsPic(pic);
      newsInfo.setNewsDate(new Date());

      boolean flag = newsDao.addNewsInfo(newsInfo);
      if (flag) {
        newsList(request, response);
      } else {
        request.setAttribute("error", "添加新闻失败!");

        request.setAttribute("newsInfo", newsInfo);

        gotoNewsAdd(request, response);
      }
    } catch (Exception e) {
      exceptionMsg = e.getMessage();
      e.printStackTrace();
      request.setAttribute("error", "添加新闻失败!");
      gotoNewsAdd(request, response);
    }
  }