Esempio n. 1
0
 @Override
 protected void service() throws ServletException, IOException {
   HttpServletRequest request = getRequest();
   String path = null;
   DiskFileItemFactory factory =
       new DiskFileItemFactory(5 * 1024, new File(Configuration.getContextPath("temp")));
   ServletFileUpload upload = new ServletFileUpload(factory);
   upload.setSizeMax(3 * 1024 * 1024);
   try {
     List<FileItem> items = upload.parseRequest(request);
     FileItem fileItem = null;
     if (items != null
         && items.size() > 0
         && StringUtils.isNotBlank((fileItem = items.get(0)).getName())) {
       String fileName = fileItem.getName();
       if (!fileName.endsWith(".jpg")
           && !fileName.endsWith(".gif")
           && !fileName.endsWith(".png")) {
         writeText("format_error");
         return;
       }
       path = ImageUtil.generatePath(fileItem.getName());
       IOUtil.copy(fileItem.getInputStream(), Configuration.getContextPath(path));
       fileItem.delete();
       writeText(Configuration.getSiteUrl(path));
     }
   } catch (FileUploadException e) {
     throw new RuntimeException(e);
   }
 }
 private void generateNovelList() {
   List<Map<String, String>> articles =
       DaoFactory.getDao(ArticleDao.class)
           .getArticlesByType(Type.novel, Status.published, VIEW_MODE);
   int total = articles.size();
   int page = (total % 10 == 0) ? (total / 10) : (total / 10 + 1);
   for (int i = 1; i < page + 1; i++) {
     Writer writer = null;
     try {
       Map<String, Object> data = FreemarkerHelper.buildCommonDataMap(VIEW_MODE);
       ArticleListHelper.putDataMapForNovel(data, VIEW_MODE, i, total);
       String htmlPath =
           Configuration.getContextPath(ArticleListHelper.generateStaticPath("novel", i));
       writer = new FileWriter(htmlPath);
       FreemarkerHelper.generate("article_list", writer, data);
     } catch (IOException e) {
       throw new RuntimeException(e);
     } finally {
       try {
         if (writer != null) {
           writer.close();
         }
       } catch (IOException e) {
         throw new RuntimeException(e);
       }
     }
   }
 }