add
<#include "../layout/layout.ftl">
<@html page_title="特殊资源添加" page_tab="mediaresource">
<section class="content-header">
<h1>
<small>列表</small>
</h1>
<ol class="breadcrumb">
<li><a href="/index"><i class="fa fa-dashboard"></i> 首页</a></li>
<li><a href="/admin/mediaResource/list">特殊资源</a></li>
<li class="active">添加</li>
</ol>
</section>
<section class="content">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">特殊资源添加</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<form id="form" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>标题名称</label>
<input type="text" id="title" name="title" placeholder="标题名称" class="form-control">
</div>
<div class="form-group">
<label>文件上传</label>
<input type="file" id="file" name="file" class="d-none"/>
</div>
<div class="form-group">
<label for="">描述</label>
<textarea id="content" name="content" rows="7" class="form-control"></textarea>
</div>
<button type="button" id="btn" class="btn btn-primary">提交</button>
</form>
</div>
</div>
</section>
<script>
$("#btn").click(function () {
var title = $("#title").val();
var file = $("#file").val();
var content = $("#content").val();
if (title.length < 1 || title.length > 120){ return tip("请输入标题,且最大长度在120个字符以内!"); }
if ($("#file").val() == ''){ return tip("请选择图片!"); }
var formData = new FormData();
formData.append("title", title);
formData.append("file", document.getElementById('file').files[0]);
formData.append("content", content);
$.ajax({
url: '/admin/uploadFile',
type: 'post',
data: formData,
contentType: false,
processData: false,
success: function (data) {
if (data.code === 200) {
suc("添加成功");
setTimeout(function () {
window.location.href = "/admin/mediaresource/list";
}, 700);
} else {
err(data.description);
}
}
});
});
</script>
</@html>
IndexAdminController
package com.bfr.telinksemifourm.manage.controller.admin;
import com.bfr.telinksemifourm.manage.exception.ApiAssert;
import com.bfr.telinksemifourm.manage.service.impl.MediaResourceService;
import com.bfr.telinksemifourm.manage.util.FileUtil;
import com.bfr.telinksemifourm.manage.service.ISystemConfigService;
import com.bfr.telinksemifourm.manage.util.Result;
import com.bfr.telinksemifourm.manage.service.ICommentService;
import com.bfr.telinksemifourm.manage.service.ITagService;
import com.bfr.telinksemifourm.manage.service.ITopicService;
import com.bfr.telinksemifourm.manage.service.IUserService;
import com.bfr.telinksemifourm.shared.model.MediaResource;
import com.sun.management.OperatingSystemMXBean;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.lang.management.ManagementFactory;
import java.util.*;
/**
* Created by .
* Copyright (c) 2020, All Rights Reserved.
*
*/
@Controller
@RequestMapping("/admin")
public class IndexAdminController extends BaseAdminController {
@Autowired
private ITopicService topicService;
@Autowired
private ITagService tagService;
@Autowired
private ICommentService commentService;
@Autowired
private IUserService userService;
@Autowired
private ISystemConfigService systemConfigService;
@Autowired
private FileUtil fileUtil;
@Autowired
private MediaResourceService mediaResourceService;
// 上传图片
@PostMapping("/upload")
@ResponseBody
public Result upload(@RequestParam("myFile") MultipartFile[] files, String type, HttpSession session) {
// User user = getApiUser();
// ApiAssert.isTrue(user.getActive(), "你的帐号还没有激活,请去个人设置页面激活帐号");
ApiAssert.notEmpty(type, "传的文件类型不能为空");
Map<String, Object> map = new HashMap<>();
List<String> urls = new ArrayList<>();
List<String> errors = new ArrayList<>();
for (int i = 0; i < files.length; i++) {
MultipartFile file = files[i];
String suffix = "." + file.getContentType().split("/")[1];
if (!Arrays.asList(".jpg", ".png", ".gif", ".jpeg", ".mp4").contains(suffix.toLowerCase())) {
errors.add("File ["+(i+1)+"] exception: The file format is incorrect");
continue;
}
long size = file.getSize();
// 根据不同上传类型,对文件大小做校验
if (type.equalsIgnoreCase("video")) {
int uploadVideoSizeLimit = Integer.parseInt(systemConfigService.selectAllConfig().get("upload_video_size_limit").toString());
if (size > uploadVideoSizeLimit * 1024 * 1024) {
errors.add("File ["+(i+1)+"] exception:" + "The file is too big, please upload the file size within "+uploadVideoSizeLimit+"MB");
continue;
}
} else {
int uploadImageSizeLimit = Integer.parseInt(systemConfigService.selectAllConfig().get("upload_image_size_limit").toString());
if (size > uploadImageSizeLimit * 1024 * 1024) {
errors.add("File ["+(i+1)+"] exception:" + "The file is too big, please upload the file size within "+uploadImageSizeLimit+"MB");
continue;
}
}
String url = null;
if (type.equalsIgnoreCase("avatar")) { // 上传头像
// 拿到上传后访问的url
url = fileUtil.upload(file, "avatar", "avatar/" );
// if (url != null) {
// // 查询当前用户的最新信息
// User user1 = userService.selectById(user.getId());
// user1.setAvatar(url);
// // 保存用户新的头像
// userService.update(user1);
// // 将最新的用户信息更新在session里
// if (session != null) session.setAttribute("_user", user1);
// }
} else if (type.equalsIgnoreCase("topic")) { // 发帖上传图片
url = fileUtil.upload(file, null, "faq/" );
} else if (type.equalsIgnoreCase("faq")) { // 发帖上传图片
url = fileUtil.uploadtop(file, null, "faq" );
} else if (type.equalsIgnoreCase("video")) { // 视频上传
url = fileUtil.upload(file, null, "faq/" );
} else {
errors.add("File ["+(i+1)+"] exception:" + "The type of image uploaded is not covered");
continue;
}
if (url == null) {
errors.add("File ["+(i+1)+"] exception:" + "The uploaded file does not exist or there was an error in the upload process");
continue;
}
urls.add(url);
}
map.put("urls", urls);
map.put("errors", errors);
return success(map);
}
/**
* 添加成功
*
* @author zxk
* @date 2020/7/3
*/
@PostMapping("/uploadFile")
@ResponseBody
public Result uploadFile(@RequestParam("file") MultipartFile file, String title, String content) {
String url = fileUtil.uploadtop(file, null, "mediaresource" );
if (!StringUtils.isEmpty(url)){
MediaResource mediaResource = new MediaResource();
mediaResource.setTitle(title);
mediaResource.setUrl(url);
mediaResource.setContent(content);
mediaResource.setFileName(file.getOriginalFilename());
if (mediaResourceService.saveMediaResource(mediaResource)){
return success();
}
}
return error("添加失败!");
}
}