项目背景:
云监测平台主要应用于大型设备温度,油管流量,湿度,水箱温度,电机信号等监测和测量
1.开发工具
eclipse/IDEA+mysql
2.技术架构
SpringBoot+jpa+mysql+html+websocket+thymeleaf
3.主要核心功能(演示地址在最底部)
监测大厅、选项配置、用户管理、账号管理、角色管理、模块管理、角色配置、日志管理、审批配置
4.部分代码展示
文件相关
@Controller
@RequestMapping(value = "/upload")
public class AttachmentController extends BaseController {
@Autowired
private AttachmentService attachmentService;
String filesRoot="c:/upload";
/**
* 附件下载,显示所有附件 用于实体中只有一类附件,返回所有附件
*
* @param model
* @param parentId
* 附件所在实体ID
* @param groupId
* 附件名称,如:方案附件
* @return
* @throws IOException
*/
@RequestMapping(value = "/{parentId}/{groupId}/{del}", method = RequestMethod.GET)
public String listUploadedFiles(Model model, @PathVariable String del, @PathVariable String parentId,
@PathVariable String groupId) {
model.addAttribute("del", del);
List<Attachment> a=attachmentService.getByParentId(parentId, groupId);
model.addAttribute("files", attachmentService.getByParentId(parentId, groupId));
model.addAttribute("groupId", groupId);
return "attachment/downloadlist";
}
/**
* @param filename
* @return 通过存储时的UUID文件名下载单个附件
* @throws UnsupportedEncodingException
*/
@RequestMapping(value = "/files/{filename:.+}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Resource> serveFile(@PathVariable String filename) throws UnsupportedEncodingException {
Resource file = attachmentService.loadAsResource(filename);
Attachment a = attachmentService.getRepository().findBySavedFileName(filename);
/*
* if(null==a){ return ResponseEntity .notFound()
* .header(HttpHeaders.CONTENT_TYPE,"") .body(file); }
*/
String originalFileName = a.getOriginalFileName();
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; charset=UTF-8;filename=\"" + java.net.URLEncoder.encode(originalFileName, "UTF-8") + "\"")
.body(file);
}
/**
* 附件上传处理 *
*/
@RequestMapping(value = "/{parentId}/{groupId}",method = RequestMethod.POST)
public @ResponseBody String handleFileUploadMultiple(@RequestParam("file") MultipartFile file,
HttpServletRequest request, @PathVariable String parentId, @PathVariable String groupId,
RedirectAttributes redirectAttributes) {
Account account = (Account) request.getSession().getAttribute("account");
String sender = "";
if (null != account)
sender = account.getUser().getName();
attachmentService.save(file, sender, parentId, groupId);
redirectAttributes.addFlashAttribute("files", attachmentService.getByParentId(parentId, groupId));
return "redirect:/upload/" + parentId + "/" + groupId;
}
/**
* 附件上传处理 mobile *
*/
@RequestMapping(value = "/{parentId}/{groupId}/json", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> handleFileUploadMultipleMobile(@RequestParam("file") MultipartFile file,
@PathVariable String parentId, @PathVariable String groupId) {
Map<String, Object> map = new HashMap<>();
String getlist=request.getParameter("getlist");
map.put("code", -1);
Account account = this.getMyAccount();
String sender = "";
if (null != account)
sender = account.getUser().getName();
attachmentService.save(file, sender, parentId, groupId);
if(StringUtils.hasText(getlist) && getlist.equals("1")){
List<Attachment> list=new ArrayList<>();
list=attachmentService.getRepository().findByParentIdAndGroupId(parentId, groupId);
map.put("data", list);
}
map.put("code", 0);
return map;
}
/*
* 获得附件,mobile
*/
@RequestMapping(value = "/{parentId}/{groupId}/json", method = RequestMethod.GET)
public @ResponseBody Map<String,Object> getAList(@PathVariable String parentId, @PathVariable String groupId) {
List<Attachment> list = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
list = attachmentService.getRepository().findByParentIdAndGroupId(parentId, groupId);
map.put("data", list);
map.put("code", 0);
return map;
}
@RequestMapping(value = "/files/delete/{id}", method = RequestMethod.POST)
public @ResponseBody String deleteAFile(@PathVariable String id) {
if (null != id) {
attachmentService.deleteA(id);
}
return "ok";
}
/**删除文件
* @param ParentId 存储记录的ParentId;
* @return
*/
@RequestMapping(value = "/files/deleteByParentId/{parentId}", method = RequestMethod.POST)
@ResponseBody
public String deleteFileBySaveFileName(@PathVariable String parentId) {
if (null != parentId) {
attachmentService.deleteFileByParentId(parentId);
}
return "ok";
}
/**删除文件
* @param parentId 存储记录的ParentId;
* @param groupId 分组Id;
* @return
*/
@RequestMapping(value = "/files/deleteByParentIdAndGroupId/{parentId}/{groupId}", method = RequestMethod.POST)
@ResponseBody
public String deleteFileBySaveFileName(@PathVariable String parentId,@PathVariable String groupId) {
if (null != parentId) {
attachmentService.deleteFileByParentIdAndGroupId(parentId,groupId);
}
return "ok";
}
/**
* 附件上传下载演示
*
* @return
*/
@RequestMapping(value = "/demo", method = RequestMethod.GET)
public ModelAndView demo() {
ModelAndView mv = this.createLayoutView("attachment/demo");
return mv;
}
@ExceptionHandler(StorageFileNotFoundException.class)
public ResponseEntity handleStorageFileNotFound(StorageFileNotFoundException exc) {
return ResponseEntity.notFound().build();
}
/*
* 获得附件的个数,根据parentId
*/
@RequestMapping(value = "/attachmentNum/{parentId}/{groupId}", method = RequestMethod.POST)
public @ResponseBody long getANum(@PathVariable String parentId, @PathVariable String groupId) {
long count = 0;
if (null != parentId) {
List<Attachment> a = attachmentService.getRepository().findByParentIdAndGroupId(parentId, groupId);
count = a.size();
}
return count;
}
//通过用户id得到其在文件上传文件夹中的头像文件,返回文件流,适用于小的图片文件
@RequestMapping(value="/avatar/{id}",method=RequestMethod.GET,produces=MediaType.ALL_VALUE)
@ResponseBody public byte[] getImage(@PathVariable String id,HttpServletRequest request, HttpServletResponse response)
{
byte[] b =null;
try {
File file=attachmentService.getUniqueNameFile(filesRoot, id);
if(null==file) return null;
RandomAccessFile f = new RandomAccessFile(file,"r");
if(f==null) return null;
b = new byte[(int)f.length()];
f.readFully(b);
f.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
@RequestMapping(value="/tempfiles/delete/{id}",method=RequestMethod.GET,produces=MediaType.ALL_VALUE)
@ResponseBody public Map<String,Object> delete(@PathVariable String id)
{
Map<String,Object> map=new HashMap<>();
map.put("code", -1);
File file=attachmentService.getUniqueNameFile(filesRoot, id);
if(null!=file){
if(true==file.delete())
map.put("code", 0);
}
return map;
}
}
首页
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>首页</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div class="container" id="content">
<!-- Main content -->
<section class="content">
<div class="row" style="margin-left: 0px; margin-right: 0px">
<div class="col-md-9">
<div class="block1">
<div class="titlePic">
<img src="/cmp/index/image/home_user.png" width="72" height="64" />
<div>
<span th:text="${session.account.user.name}">个人信息</span>
</div>
<div>
(<span th:text="${session.account.role.name}">个人信息</span>)
</div>
</div>
</div>
<div class="block3">
<div class="titlePic">
<img src="/cmp/index/image/home_tp3.png" width="72" height="64">
<div>
<span>我的文件</span>
</div>
</div>
</div>
<div class="block4">
<div class="titlePic">
<img src="/cmp/index/image/tp4.png" width="72" height="64">
<div>
<span>我的试验</span>
</div>
</div>
</div>
<div class="block5">
<div class="titlePic">
<img src="/cmp/index/image/tp5.png" width="72" height="64">
<div>
<span>我的消息</span>
<span class="label label-danger">4</span>
</div>
</div>
</div>
<div class="block6">
<div class="titlePic">
<img src="/cmp/index/image/tp6.png" width="72" height="64">
<div>
<span>我的待办</span>
<span class="label bg-yellow">5</span>
</div>
</div>
</div>
<div class="block7">
<div class="titlePic">
<img src="/cmp/index/image/tp7.png" width="72" height="64">
<div>
<span>视频广场</span>
</div>
</div>
</div>
<div class="block8">
<div class="titlePic">
<img src="/cmp/index/image/tp8.png" width="72" height="64">
<div>
<span>环境监测</span>
</div>
</div>
</div>
<div class="block9">
<div class="titlePic">
<img src="/cmp/index/image/tp9.png" width="72" height="64">
<div>
<span>试验申请</span>
</div>
</div>
</div>
<div class="block10">
<div class="titlePic">
<img src="/cmp/index/image/tp11.png" width="72" height="64">
<div>
<span>试验过程</span>
</div>
</div>
</div>
<div class="block11">
<div class="titlePic">
<img src="/cmp/index/image/tp10.png" width="72" height="64">
<div>
<span>试验监控</span>
</div>
</div>
</div>
</div>
<div class="col-md-3">
</div>
</div>
</section>
<link rel="stylesheet" type="text/css" media="all"
th:href="@{/index/indexcss/index.css}" />
<script th:src="@{/index/index.js}"></script>
</div>
</body>
</html>
演示地址:
链接:https://pan.baidu.com/s/1RX5c0MXs5poJWqxO54XHsw
提取码:7sz9