文件上传测试完成
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
package top.fjy8018.fileupload.controller;
|
package top.fjy8018.fileupload.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
@@ -23,6 +25,7 @@ import java.util.Map;
|
|||||||
* @author F嘉阳
|
* @author F嘉阳
|
||||||
* @date 2018-06-23 09:16
|
* @date 2018-06-23 09:16
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping("/upload")
|
@RequestMapping("/upload")
|
||||||
public class UploadController {
|
public class UploadController {
|
||||||
@@ -33,25 +36,30 @@ public class UploadController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ServerPropertiesConfig serverPropertiesConfig;
|
private ServerPropertiesConfig serverPropertiesConfig;
|
||||||
|
|
||||||
|
@GetMapping("")
|
||||||
|
public String toUploadPage(){
|
||||||
|
return "home/upload.html";
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping("/add")
|
@PostMapping("/add")
|
||||||
public String moreUpload(HttpServletRequest request,
|
public void moreUpload(HttpServletRequest request) {
|
||||||
@RequestParam(value = "fileName") String fileName) {
|
|
||||||
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
|
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
|
||||||
Map<String, MultipartFile> files = multipartHttpServletRequest.getFileMap();
|
Map<String, MultipartFile> files = multipartHttpServletRequest.getFileMap();
|
||||||
|
|
||||||
User user = (User)request.getSession().getAttribute(GlobalConstant.USER_SESSION_KEY);
|
//User user = (User)request.getSession().getAttribute(GlobalConstant.USER_SESSION_KEY);
|
||||||
|
|
||||||
EsFileInfo fileInfo = new EsFileInfo();
|
EsFileInfo fileInfo = new EsFileInfo();
|
||||||
|
|
||||||
for (MultipartFile file : files.values()) {
|
for (MultipartFile file : files.values()) {
|
||||||
String pathname = serverPropertiesConfig.getFilePath()+fileName;
|
String pathname = serverPropertiesConfig.getFilePath()+file.getOriginalFilename();
|
||||||
File targetFile = new File(pathname);
|
File targetFile = new File(pathname);
|
||||||
|
|
||||||
fileInfo.setFileName(fileName);
|
fileInfo.setFileName(file.getOriginalFilename());
|
||||||
fileInfo.setFilePath(pathname);
|
fileInfo.setFilePath(pathname);
|
||||||
fileInfo.setFileSize(FormatFileSizeUtil.GetFileSize(file.getSize()));
|
fileInfo.setFileSize(FormatFileSizeUtil.GetFileSize(file.getSize()));
|
||||||
fileInfo.setUserId(user.getId());
|
fileInfo.setUserId("1529723171835369380");
|
||||||
|
|
||||||
|
log.info("【文件上传信息】fileinfo{},PostFile:{}",fileInfo.toString(),file.getOriginalFilename());
|
||||||
// 写入ES
|
// 写入ES
|
||||||
fileService.saveFile(fileInfo);
|
fileService.saveFile(fileInfo);
|
||||||
|
|
||||||
@@ -61,6 +69,5 @@ public class UploadController {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
27
src/main/resources/static/js/upload.js
Normal file
27
src/main/resources/static/js/upload.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
var Main = {
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
fileList: [
|
||||||
|
{
|
||||||
|
name: 'food.jpeg',
|
||||||
|
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
|
||||||
|
}, {
|
||||||
|
name: 'food2.jpeg',
|
||||||
|
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
|
||||||
|
}]
|
||||||
|
};
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
submitUpload() {
|
||||||
|
this.$refs.upload.submit();
|
||||||
|
},
|
||||||
|
handleRemove(file, fileList) {
|
||||||
|
console.log(file, fileList);
|
||||||
|
},
|
||||||
|
handlePreview(file) {
|
||||||
|
console.log(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var Ctor = Vue.extend(Main)
|
||||||
|
new Ctor().$mount('#app')
|
||||||
6
src/main/resources/templates/dist/thymeleaf/common_foot.html
vendored
Normal file
6
src/main/resources/templates/dist/thymeleaf/common_foot.html
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<body>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
20
src/main/resources/templates/dist/thymeleaf/common_head.html
vendored
Normal file
20
src/main/resources/templates/dist/thymeleaf/common_head.html
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head th:fragment="header(title)">
|
||||||
|
<meta name="renderer" content="webkit">
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title th:text="${title}"></title>
|
||||||
|
<link rel="stylesheet" href="https://unpkg.com/element-ui@2.3.3/lib/theme-chalk/index.css">
|
||||||
|
<link th:href="@{/css/style.css}" rel="stylesheet">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="body_js">
|
||||||
|
<!-- 先引入 Vue -->
|
||||||
|
<script src="https://unpkg.com/vue@2.5.15/dist/vue.js"></script>
|
||||||
|
<!-- 引入组件库 -->
|
||||||
|
<script src="https://unpkg.com/element-ui@2.3.3/lib/index.js"></script>
|
||||||
|
<script src="https://unpkg.com/axios@0.18.0/dist/axios.min.js"></script>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
9
src/main/resources/templates/dist/thymeleaf/footer.html
vendored
Normal file
9
src/main/resources/templates/dist/thymeleaf/footer.html
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<body>
|
||||||
|
<div th:fragment="copyright">
|
||||||
|
© 2016 xxx
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
122
src/main/resources/templates/dist/thymeleaf/layout.html
vendored
Normal file
122
src/main/resources/templates/dist/thymeleaf/layout.html
vendored
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<body>
|
||||||
|
<el-header th:fragment="header">
|
||||||
|
<el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
|
||||||
|
<el-menu-item index="1" @click="ClickToJump('home')">首页</el-menu-item>
|
||||||
|
<el-menu-item index="2" @click="ClickToJump('home/user')">个人中心</el-menu-item>
|
||||||
|
<el-menu-item index="3" @click="ClickToJump('home/about')">关于</el-menu-item>
|
||||||
|
<el-menu-item index="4" @click="ClickToJump('home/feedback')">意见反馈</el-menu-item>
|
||||||
|
<el-menu-item index="5" @click="ClickToJump('logout')">登出</el-menu-item>
|
||||||
|
</el-menu>
|
||||||
|
</el-header>
|
||||||
|
|
||||||
|
<el-aside width="200px" th:fragment="asider">
|
||||||
|
<el-row class="tac">
|
||||||
|
<el-col :span="24">
|
||||||
|
<h5>默认颜色</h5>
|
||||||
|
<el-menu default-active="2" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose">
|
||||||
|
<el-submenu index="1">
|
||||||
|
<template slot="title">
|
||||||
|
<i class="el-icon-location"></i>
|
||||||
|
<span>导航一</span>
|
||||||
|
</template>
|
||||||
|
<el-menu-item-group>
|
||||||
|
<template slot="title">分组一</template>
|
||||||
|
<el-menu-item index="1-1">选项1</el-menu-item>
|
||||||
|
<el-menu-item index="1-2">选项2</el-menu-item>
|
||||||
|
</el-menu-item-group>
|
||||||
|
<el-menu-item-group title="分组2">
|
||||||
|
<el-menu-item index="1-3">选项3</el-menu-item>
|
||||||
|
</el-menu-item-group>
|
||||||
|
<el-submenu index="1-4">
|
||||||
|
<template slot="title">选项4</template>
|
||||||
|
<el-menu-item index="1-4-1">选项1</el-menu-item>
|
||||||
|
</el-submenu>
|
||||||
|
</el-submenu>
|
||||||
|
<el-menu-item index="2">
|
||||||
|
<i class="el-icon-menu"></i>
|
||||||
|
<span slot="title">导航二</span>
|
||||||
|
</el-menu-item>
|
||||||
|
<el-menu-item index="3">
|
||||||
|
<i class="el-icon-setting"></i>
|
||||||
|
<span slot="title">导航三</span>
|
||||||
|
</el-menu-item>
|
||||||
|
</el-menu>
|
||||||
|
</el-col>
|
||||||
|
</el-row>
|
||||||
|
</el-aside>
|
||||||
|
|
||||||
|
<el-card class="box-card" th:fragment="userbox">
|
||||||
|
<div slot="header" class="clearfix">
|
||||||
|
<!--下面两行报错正常-->
|
||||||
|
<span th:text="${#httpServletRequest.getSession().getAttribute('USER_SESSION').colname}">用户名</span><br>
|
||||||
|
<span th:text="${#httpServletRequest.getSession().getAttribute('USER_SESSION').colstudentno}"></span>
|
||||||
|
</div>
|
||||||
|
<div class="item">
|
||||||
|
<el-button type="text" @click="ClickToJump('home')">首页</el-button>
|
||||||
|
<br>
|
||||||
|
<el-button type="text" @click="ClickToJump('home/user')">个人中心</el-button>
|
||||||
|
<br>
|
||||||
|
<el-button type="text" @click="ClickToJump('home/about')">关于</el-button>
|
||||||
|
<br>
|
||||||
|
<el-button type="text" @click="ClickToJump('home/feedback')">意见反馈</el-button>
|
||||||
|
</div>
|
||||||
|
</el-card>
|
||||||
|
|
||||||
|
<el-card class="box-card" th:fragment="adminbox">
|
||||||
|
<div slot="header" class="clearfix">
|
||||||
|
<span>管理员</span><br>
|
||||||
|
<span>管理员号</span>
|
||||||
|
</div>
|
||||||
|
<div class="item">
|
||||||
|
<el-button type="text" @click="ClickToJump('home/admin')">后台主页</el-button>
|
||||||
|
<br>
|
||||||
|
<el-button type="text" @click="ClickToJump('home/admin/managecourse')">课程管理</el-button>
|
||||||
|
<br>
|
||||||
|
<el-button type="text" @click="ClickToJump('home/admin/homework')">作业管理</el-button>
|
||||||
|
<br>
|
||||||
|
<el-button type="text" @click="ClickToJump('home/admin/manageuser')">用户管理</el-button>
|
||||||
|
</div>
|
||||||
|
</el-card>
|
||||||
|
|
||||||
|
<el-card class="box-card hidden-xs-only" th:fragment="timelimit">
|
||||||
|
<div slot="header" class="clearfix">
|
||||||
|
<span>作业提交倒计时</span>
|
||||||
|
<!--超时后仍为正数-->
|
||||||
|
<!--<span style="color: red"> 功能未完成</span>-->
|
||||||
|
</div>
|
||||||
|
<div class="text item">
|
||||||
|
<el-table :data="tableHomeworkData" height="300" style="width: 100%"
|
||||||
|
:default-sort="{prop: 'time', order: 'descending'}">
|
||||||
|
<el-table-column prop="coursename" label="科目" sortable>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column prop="workname" label="作业" sortable>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column prop="time" label="倒计时" sortable>
|
||||||
|
<template slot-scope="scope">
|
||||||
|
{{limitTime(scope.row)}}
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
</el-table>
|
||||||
|
</div>
|
||||||
|
</el-card>
|
||||||
|
|
||||||
|
<el-card class="box-card hidden-xs-only" th:fragment="notice">
|
||||||
|
<div slot="header" class="clearfix">
|
||||||
|
<span>公告</span>
|
||||||
|
</div>
|
||||||
|
<div v-for="notice in NoticeList" class="text item">
|
||||||
|
{{notice.noticeContent + notice.issueTime}}
|
||||||
|
</div>
|
||||||
|
</el-card>
|
||||||
|
|
||||||
|
<el-footer th:fragment="footer">
|
||||||
|
<div class="footer">
|
||||||
|
Copyright © 2018 作业提交系统 All Rights Reserved|
|
||||||
|
<a href="http://www.miitbeian.gov.cn/">粤ICP备18026008号</a> 粤ICP备18026008号-1
|
||||||
|
</div>
|
||||||
|
</el-footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
19
src/main/resources/templates/home/upload.html
Normal file
19
src/main/resources/templates/home/upload.html
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head th:include="dist/thymeleaf/common_head :: header('上传')">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div th:insert="~{dist/thymeleaf/common_head :: #body_js}"></div>
|
||||||
|
<div id="app">
|
||||||
|
<el-upload class="upload-demo" ref="upload" action="/fileupload/upload/add"
|
||||||
|
:on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList" :auto-upload="false">
|
||||||
|
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
|
||||||
|
<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
|
||||||
|
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过100Mb</div>
|
||||||
|
</el-upload>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script th:src="@{/js/upload.js}+'?v=0.1'"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
10
src/main/resources/templates/upload/add.html
Normal file
10
src/main/resources/templates/upload/add.html
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>上传成功</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user