Пример #1
0
  @RequestMapping(value = "upload.htm")
  public ModelAndView uploadDocument(
      @ModelAttribute ImportData importData, BindingResult result, HttpServletRequest httpRequest) {

    logger.info("---- Wait For Uploading File ----");

    ModelAndView mav = new ModelAndView();
    mav.addObject(BuckWaConstants.PAGE_SELECT, BuckWaConstants.ADMIN_INIT);
    mav.setViewName("initImportWorks");
    try {
      String fileName = null;
      InputStream inputStream = null;
      OutputStream outputStream = null;
      MultipartFile originalfile = importData.getFileData();

      logger.info(
          " originalfile size:"
              + originalfile.getSize()
              + " File Name:"
              + originalfile.getOriginalFilename());

      if (originalfile != null && originalfile.getSize() > 0) {
        inputStream = originalfile.getInputStream();
        if (originalfile.getSize() > pathUtil.getMaximumImageUploadSize()) {
          logger.info(
              " Error File Size: "
                  + originalfile.getSize()
                  + " Greater than :"
                  + pathUtil.getMaximumImageUploadSize());
          mav.addObject("errorCode", "E013");
          return mav;
        } else {
          mav.setViewName("initWorksHistory");

          String uploadPath = pathUtil.getUploadPath() + "works/";
          fileName = originalfile.getOriginalFilename();

          FileUtils.createDirectoryIfNotExist(uploadPath);

          logger.info("## File Size :" + originalfile.getSize());
          logger.info("## File Name :" + fileName);
          logger.info("## Upload Path :" + uploadPath);

          fileName = uploadPath + fileName;

          logger.info("## File Name + Path :" + fileName);

          inputStream = originalfile.getInputStream();
          outputStream = new FileOutputStream(fileName);

          int readBytes = 0;
          byte[] buffer = new byte[10000];
          while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
            logger.info("---- Uploading ..... Sucess ----");
            outputStream.write(buffer, 0, readBytes);
          }
          outputStream.close();
          inputStream.close();
        }
      } else {
        mav.addObject("errorCode", "E012");
        return mav;
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      mav.addObject("errorCode", "E001");
    }
    return mav;
  }
Пример #2
0
  public static void main(String arg[]) {
    String filepath = "D:/Project/Baiwa/vision/34/";

    FileUtils test = new FileUtils();
    test.createDirectoryIfNotExist(filepath);
  }