portal web service

ArticleService.java 5.5KB

    package com.ekexiu.portal.service; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import org.jfw.apt.annotation.Autowrie; import org.jfw.apt.web.annotation.Path; import org.jfw.apt.web.annotation.operate.Get; import org.jfw.apt.web.annotation.operate.Post; import org.jfw.apt.web.annotation.param.JdbcConn; import org.jfw.util.JpgUtil; import org.jfw.util.StringUtil; import org.jfw.util.exception.JfwBaseException; import org.jfw.util.io.IoUtil; import com.ekexiu.portal.dao.ArticleDao; import com.ekexiu.portal.dao.ProfessorDao; import com.ekexiu.portal.dao.ResourceDao; import com.ekexiu.portal.po.Article; import com.ekexiu.portal.pojo.EditProfessor; @Path("/article") public class ArticleService { private File tmpPath; private File articlePath; private String dateFormat = "yyyyMMdd"; private int artMaxLen=640; private static final String JPG = "jpg"; @Autowrie private ArticleDao articleDao; @Autowrie private ProfessorDao professorDao; @Autowrie private ResourceDao resourceDao; public String getDateFormat() { return dateFormat; } public void setDateFormat(String dateFormat) { this.dateFormat = dateFormat; } public File getTmpPath() { return tmpPath; } public void setTmpPath(File tmpPath) { this.tmpPath = tmpPath; } public File getArticlePath() { return articlePath; } public void setArticlePath(File articlePath) { this.articlePath = articlePath; } public int getArtMaxLen() { return artMaxLen; } public void setArtMaxLen(int artMaxLen) { this.artMaxLen = artMaxLen; } public ArticleDao getArticleDao() { return articleDao; } public void setArticleDao(ArticleDao articleDao) { this.articleDao = articleDao; } public ProfessorDao getProfessorDao() { return professorDao; } public void setProfessorDao(ProfessorDao professorDao) { this.professorDao = professorDao; } public ResourceDao getResourceDao() { return resourceDao; } public void setResourceDao(ResourceDao resourceDao) { this.resourceDao = resourceDao; } private byte[] resImage(byte[] src, int maxLen) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(src); ByteArrayOutputStream out = new ByteArrayOutputStream(); JpgUtil.scalingZoom(in, out, maxLen); out.flush(); return out.toByteArray(); } private byte[] readTmpFile(String fn) throws JfwBaseException { File file = new File(this.tmpPath, fn); if (!file.exists()) throw new JfwBaseException(90, "resource image not exists"); try { InputStream in = new FileInputStream(file); ByteArrayOutputStream out = new ByteArrayOutputStream(); IoUtil.copy(in, out, true, true); return out.toByteArray(); } catch (IOException e) { throw new JfwBaseException(91, "read temp resource image error", e); } } private void saveArtImg(String fn, String id) throws JfwBaseException, IOException{ byte[] src = this.readTmpFile(fn); src = JpgUtil.read(src); byte[] shareResImage = this.resImage(src, this.artMaxLen); String savePath = this.articlePath + "/" + this.createDate(); File dateFile = new File(savePath); if (!dateFile.exists()) { // 创建日期目录 dateFile.mkdir(); } IoUtil.saveStream(new FileOutputStream(new File(dateFile, id + "." + JPG)), src, true); IoUtil.saveStream(new FileOutputStream(new File(dateFile, id + "_s." + JPG)), shareResImage, true); } private String createDate(){ SimpleDateFormat df = new SimpleDateFormat(this.dateFormat); String date = df.format(new Date()); return date; } @Post @Path public String insert(@JdbcConn(true) Connection con, Article article) throws SQLException, IOException, JfwBaseException{ String articleId = StringUtil.buildUUID(); if(article.getArticleImg() != null){ this.saveArtImg(article.getArticleImg(), articleId); article.setArticleImg(this.createDate()+"/"+articleId+"."+JPG); } article.setArticleId(articleId); this.articleDao.insert(con, article); return articleId; } @Post @Path("/updateArt") public void update(@JdbcConn(true) Connection con, Article article) throws SQLException, JfwBaseException, IOException{ if(article.getArticleImg() != null){ this.saveArtImg(article.getArticleImg(), article.getArticleId()); article.setArticleImg(this.createDate()+"/"+article.getArticleId()+"."+JPG); } this.articleDao.update(con, article); } @Get @Path("/qaPro") public List<Article> queryPro(@JdbcConn Connection con, String professorId) throws SQLException{ return this.articleDao.queryPro(con, professorId); } @Get @Path("/query") public Article queryOne(@JdbcConn Connection con, String articleId) throws SQLException{ Article article = this.articleDao.queryOne(con, articleId); if(article != null){ EditProfessor professor = this.professorDao.queryBaseInfo(con, article.getProfessorId()); if(professor != null){ professor.setResources(this.resourceDao.queryPro(con, professor.getId())); } article.setProfessor(professor); } return article; } @Post @Path("/delete") public void delete(@JdbcConn(true) Connection con, String articleId) throws SQLException{ this.articleDao.delete(con, articleId); } }