From 18c724b4b9adb86d2188775a31edaadfc3daed64 Mon Sep 17 00:00:00 2001 From: tamguo Date: Wed, 14 Nov 2018 17:03:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/tamguo/utils/FileMd5Utils.java | 37 ++++++++++ .../tamguo/web/member/DocumentController.java | 59 ++++++++++++++++ .../test/java/com/tamguo/PaperCrawler.java | 10 +-- .../java/com/tamguo/PaperQuestionCrawler.java | 4 +- .../modules/book/dao/FileEntityMapper.java | 8 +++ .../tamguo/modules/book/model/FileEntity.java | 69 +++++++++++++++++++ .../book/service/IFileEntityService.java | 8 +++ .../service/impl/FileEntityServiceImpl.java | 13 ++++ .../src/main/resources/application.properties | 8 +-- .../resources/templates/include/footer.html | 3 + 10 files changed, 208 insertions(+), 11 deletions(-) create mode 100644 tamguo-bms/src/main/java/com/tamguo/utils/FileMd5Utils.java create mode 100644 tamguo-modules-core/src/main/java/com/tamguo/modules/book/dao/FileEntityMapper.java create mode 100644 tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileEntity.java create mode 100644 tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/IFileEntityService.java create mode 100644 tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/FileEntityServiceImpl.java diff --git a/tamguo-bms/src/main/java/com/tamguo/utils/FileMd5Utils.java b/tamguo-bms/src/main/java/com/tamguo/utils/FileMd5Utils.java new file mode 100644 index 0000000..539a8ba --- /dev/null +++ b/tamguo-bms/src/main/java/com/tamguo/utils/FileMd5Utils.java @@ -0,0 +1,37 @@ +package com.tamguo.utils; + +import java.io.FileInputStream; +import java.io.IOException; +import java.security.MessageDigest; +import org.apache.commons.codec.binary.Hex; + +public class FileMd5Utils { + + /** + * 获取一个文件的md5值(可处理大文件) + * @return md5 value + */ + public static String getMD5(FileInputStream fileInputStream) { + try { + MessageDigest MD5 = MessageDigest.getInstance("MD5"); + byte[] buffer = new byte[8192]; + int length; + while ((length = fileInputStream.read(buffer)) != -1) { + MD5.update(buffer, 0, length); + } + return new String(Hex.encodeHex(MD5.digest())); + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + try { + if (fileInputStream != null){ + fileInputStream.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +} diff --git a/tamguo-bms/src/main/java/com/tamguo/web/member/DocumentController.java b/tamguo-bms/src/main/java/com/tamguo/web/member/DocumentController.java index abda4da..e5a21a3 100644 --- a/tamguo-bms/src/main/java/com/tamguo/web/member/DocumentController.java +++ b/tamguo-bms/src/main/java/com/tamguo/web/member/DocumentController.java @@ -1,17 +1,33 @@ package com.tamguo.web.member; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Date; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.multipart.MultipartFile; +import com.baomidou.mybatisplus.mapper.Condition; +import com.tamguo.common.utils.DateUtil; import com.tamguo.common.utils.Result; import com.tamguo.modules.book.model.DocumentEntity; +import com.tamguo.modules.book.model.FileEntity; import com.tamguo.modules.book.service.IDocumentService; +import com.tamguo.modules.book.service.IFileEntityService; +import com.tamguo.utils.FileMd5Utils; @Controller @RequestMapping(value="member/document") @@ -21,6 +37,13 @@ public class DocumentController { @Autowired IDocumentService iDocumentService; + @Autowired + IFileEntityService iFileEntityService; + + @Value("${file.storage.path}") + private String fileStoragePath; + @Value("${domain.name}") + private String domainName; @RequestMapping(value = "{id}" , method = RequestMethod.GET) @ResponseBody @@ -64,4 +87,40 @@ public class DocumentController { } return Result.successResult(document); } + + @SuppressWarnings("unchecked") + @RequestMapping(value = "uploadImage" , method = RequestMethod.POST) + @ResponseBody + public Result uploadImage(@RequestParam("editormd-image-file") MultipartFile file, String bookId , HttpServletRequest request) { + try { + String fileMd5 = FileMd5Utils.getMD5((FileInputStream)file.getInputStream()); + FileEntity sysFile = iFileEntityService.selectOne(Condition.create().eq("file_md5", fileMd5)); + if(sysFile != null) { + sysFile.setFilePath(domainName + "files/" + sysFile.getFilePath()); + return Result.successResult(sysFile); + } + String filePath = fileStoragePath + "book/" + DateUtil.fomatDate(new Date(), "yyyyMM") + "/" + bookId; + File dest = new File(filePath); + if(!dest.exists()) { + dest.mkdirs(); + } + // save 文件 + FileUtils.writeByteArrayToFile(new File(filePath + "/" + file.getOriginalFilename()) , file.getBytes()); + + FileEntity fileEntity = new FileEntity(); + fileEntity.setFileContentType(file.getContentType()); + fileEntity.setFileExtension(file.getOriginalFilename()); + fileEntity.setFileMd5(FileMd5Utils.getMD5((FileInputStream)file.getInputStream())); + fileEntity.setFileSize(file.getSize()); + fileEntity.setFilePath("book/" + DateUtil.fomatDate(new Date(), "yyyyMM") + "/" + bookId + "/" + file.getOriginalFilename()); + iFileEntityService.insert(fileEntity); + + fileEntity.setFilePath(domainName + "files/" + fileEntity.getFilePath()); + + return Result.successResult(fileEntity); + } catch (IOException e) { + e.printStackTrace(); + return Result.failResult("上传失败"); + } + } } diff --git a/tamguo-crawler/src/test/java/com/tamguo/PaperCrawler.java b/tamguo-crawler/src/test/java/com/tamguo/PaperCrawler.java index 1cc15b4..faf3783 100644 --- a/tamguo-crawler/src/test/java/com/tamguo/PaperCrawler.java +++ b/tamguo-crawler/src/test/java/com/tamguo/PaperCrawler.java @@ -29,18 +29,18 @@ public class PaperCrawler { // 高考 private final String SUBJECT_ID = "gaokao"; // 科目 - private final String COURSE_ID = "wuli"; + private final String COURSE_ID = "shengwu"; // 110000 北京 | 310000 上海 | 500000 重庆 | 120000 天津 | 370000 山东 | 410000 河南 | 420000 湖北 | 320000 江苏 | 330000 浙江 // 140000 山西 | 350000 福建 | 340000 安徽 | 220000 吉林 | 150000 内蒙古 | 640000 宁夏 | 650000 新疆 | 广西 450000 | 210000 辽宁 // 230000 黑龙江 | 610000 陕西 | 360000 江西 | 440000 广东 | 430000 湖南 | 460000 海南 | 530000 云南 | 510000 四川 | 630000 青海 // 620000 甘肃 | 130000 河北 | 540000 西藏 | 贵州 520000 - private final String AREA_ID = "460000"; + private final String AREA_ID = "310000"; // 年份 - private final String YEAR = "2017"; + private final String YEAR = "2016"; // 真题试卷 类型(1:真题试卷,2:模拟试卷,3:押题预测,4:名校精品) - private final String PAPER_TYPE = "2"; + private final String PAPER_TYPE = "4"; // 开始采集的URL - private final String START_URL = "https://tiku.baidu.com/tikupc/paperlist/1bfd700abb68a98271fefa04-18-4-2017-1567-1-download"; + private final String START_URL = "https://tiku.baidu.com/tikupc/paperlist/1bfd700abb68a98271fefa04-20-7-2016-93-1-download"; private RunData runData; diff --git a/tamguo-crawler/src/test/java/com/tamguo/PaperQuestionCrawler.java b/tamguo-crawler/src/test/java/com/tamguo/PaperQuestionCrawler.java index f22e4aa..978af41 100644 --- a/tamguo-crawler/src/test/java/com/tamguo/PaperQuestionCrawler.java +++ b/tamguo-crawler/src/test/java/com/tamguo/PaperQuestionCrawler.java @@ -63,8 +63,8 @@ public class PaperQuestionCrawler { @Autowired CacheService cacheService; private static final String FILES_NO_FORMAT = "000000000"; - private static final String FILES_PREFIX = "wuli"; - private static final String COURSE_ID = "wuli"; + private static final String FILES_PREFIX = "shengwu"; + private static final String COURSE_ID = "shengwu"; private RunData runData; diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/dao/FileEntityMapper.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/dao/FileEntityMapper.java new file mode 100644 index 0000000..78d9741 --- /dev/null +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/dao/FileEntityMapper.java @@ -0,0 +1,8 @@ +package com.tamguo.modules.book.dao; + +import com.baomidou.mybatisplus.mapper.BaseMapper; +import com.tamguo.modules.book.model.FileEntity; + +public interface FileEntityMapper extends BaseMapper{ + +} diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileEntity.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileEntity.java new file mode 100644 index 0000000..f6a19b7 --- /dev/null +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/model/FileEntity.java @@ -0,0 +1,69 @@ +package com.tamguo.modules.book.model; + +import com.baomidou.mybatisplus.annotations.TableId; +import com.baomidou.mybatisplus.annotations.TableName; + +/** + * + * Entity - 图片 + * + * @author tamguo + * + */ +@TableName(value="b_file_entity") +public class FileEntity { + + @TableId("file_id") + private String fileId; + + private String fileMd5; + private String filePath; + private String fileContentType; + private String fileExtension; + private Long fileSize; + + public String getFileId() { + return fileId; + } + public void setFileId(String fileId) { + this.fileId = fileId; + } + public String getFileMd5() { + return fileMd5; + } + public void setFileMd5(String fileMd5) { + this.fileMd5 = fileMd5; + } + public String getFilePath() { + return filePath; + } + public void setFilePath(String filePath) { + this.filePath = filePath; + } + public String getFileContentType() { + return fileContentType; + } + public void setFileContentType(String fileContentType) { + this.fileContentType = fileContentType; + } + public String getFileExtension() { + return fileExtension; + } + public void setFileExtension(String fileExtension) { + this.fileExtension = fileExtension; + } + public Long getFileSize() { + return fileSize; + } + public void setFileSize(Long fileSize) { + this.fileSize = fileSize; + } + + public String getFileUrl() { + return getFilePath(); + } + + public String getId() { + return getFileId(); + } +} diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/IFileEntityService.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/IFileEntityService.java new file mode 100644 index 0000000..a6029e4 --- /dev/null +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/IFileEntityService.java @@ -0,0 +1,8 @@ +package com.tamguo.modules.book.service; + +import com.baomidou.mybatisplus.service.IService; +import com.tamguo.modules.book.model.FileEntity; + +public interface IFileEntityService extends IService{ + +} diff --git a/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/FileEntityServiceImpl.java b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/FileEntityServiceImpl.java new file mode 100644 index 0000000..c6f4a02 --- /dev/null +++ b/tamguo-modules-core/src/main/java/com/tamguo/modules/book/service/impl/FileEntityServiceImpl.java @@ -0,0 +1,13 @@ +package com.tamguo.modules.book.service.impl; + +import org.springframework.stereotype.Service; + +import com.baomidou.mybatisplus.service.impl.ServiceImpl; +import com.tamguo.modules.book.dao.FileEntityMapper; +import com.tamguo.modules.book.model.FileEntity; +import com.tamguo.modules.book.service.IFileEntityService; + +@Service +public class FileEntityServiceImpl extends ServiceImpl implements IFileEntityService{ + +} diff --git a/tamguo-tms/src/main/resources/application.properties b/tamguo-tms/src/main/resources/application.properties index 6f48bab..1c869ee 100644 --- a/tamguo-tms/src/main/resources/application.properties +++ b/tamguo-tms/src/main/resources/application.properties @@ -1,5 +1,5 @@ -domain.name=http://localhost:8081/ -admin.domain.name=http://localhost:8081/ +domain.name=https://www.tamguo.com/ +admin.domain.name=https://www.tamguo.com/ server.port=8081 jasypt.encryptor.password=tamguo @@ -12,14 +12,14 @@ spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.maxWait=60000 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.minIdle=5 -spring.datasource.password=123456 +spring.datasource.password=tanguo520pig spring.datasource.poolPreparedStatements=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false spring.datasource.testWhileIdle=true spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource -spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tamguo_20181110?useUnicode=true&characterEncoding=UTF-8&useSSL=false +spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tamguo?useUnicode=true&characterEncoding=UTF-8&useSSL=false spring.datasource.username=root spring.datasource.validationQuery=SELECT 1 FROM DUAL diff --git a/tamguo-tms/src/main/resources/templates/include/footer.html b/tamguo-tms/src/main/resources/templates/include/footer.html index e579c4c..ba8203c 100644 --- a/tamguo-tms/src/main/resources/templates/include/footer.html +++ b/tamguo-tms/src/main/resources/templates/include/footer.html @@ -40,6 +40,9 @@
  • 江西自考网
  • +
  • 上海研究生招生信息网 +