387 lines
13 KiB
Plaintext
387 lines
13 KiB
Plaintext
package com.bonus.sys.controller;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.Enumeration;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
import com.bonus.core.DateTimeHelper;
|
|
import com.bonus.core.ExcelUtils;
|
|
import com.bonus.core.ReflectUtil;
|
|
import com.bonus.core.StringHelper;
|
|
import com.bonus.sys.AjaxRes;
|
|
import com.bonus.sys.BaseController;
|
|
import com.bonus.sys.GlobalConst;
|
|
import com.bonus.sys.PropertyHelper;
|
|
import com.bonus.sys.UserShiroHelper;
|
|
import com.bonus.sys.beans.FileUploadInfoBean;
|
|
import com.bonus.sys.beans.ViewImgBean;
|
|
import com.bonus.sys.dao.FileUploadInfoDao;
|
|
import com.bonus.sys.service.FileUploadService;
|
|
import com.bonus.sys.service.SysDataDictService;
|
|
import com.oreilly.servlet.MultipartRequest;
|
|
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
|
|
import com.sun.xml.internal.ws.message.StringHeader;
|
|
|
|
@Controller
|
|
@RequestMapping("/backstage/fileUpload/")
|
|
public class FileUploadController extends BaseController<Object> {
|
|
|
|
@Autowired
|
|
public FileUploadInfoDao dao;
|
|
|
|
@Autowired
|
|
public FileUploadService service;
|
|
|
|
@Autowired
|
|
protected JdbcTemplate jdbcTemplate;
|
|
|
|
/**
|
|
* @Author 无畏
|
|
* @Date 2019-06-11
|
|
* @function 配合layer.photos 图片查看插件使用
|
|
* 该函数返回完全符合插件需要的JSON型字符串,
|
|
* 前端JSON.parse转换后直接放入插件参数中即可
|
|
* @param o 需传来modelFlag 模块标记 和 ownerId 所属者的id
|
|
* @return
|
|
*/
|
|
@RequestMapping("getImgsByOwnerId")
|
|
@ResponseBody
|
|
public String getImgsByOwnerId(FileUploadInfoBean o) {
|
|
ViewImgBean imgs = dao.findImgByOwnerIdOfAudit(o);
|
|
return imgs.allToString();
|
|
}
|
|
|
|
/**
|
|
* 下载文档
|
|
* @param request
|
|
* @param response
|
|
* @param o
|
|
*/
|
|
@RequestMapping(value = "downloadDocument")
|
|
@ResponseBody
|
|
public void downloadDocument(HttpServletRequest request, HttpServletResponse response,FileUploadInfoBean o) {
|
|
service.downloadDocument(request, response, o);
|
|
}
|
|
|
|
/**
|
|
* 下载文档
|
|
* @param request
|
|
* @param response
|
|
* @param o
|
|
*/
|
|
@RequestMapping(value = "downloadTemplate")
|
|
@ResponseBody
|
|
public void downloadTemplate(HttpServletRequest request, HttpServletResponse response,FileUploadInfoBean o) {
|
|
service.downloadTemplate(request, response, o);
|
|
}
|
|
|
|
/**
|
|
* @Author 无畏
|
|
* @Date 2019-06-11
|
|
* @function 打开通用的图片上传查看页面
|
|
* @param o 需传来modelFlag 模块标记 和 ownerId 所属者的id
|
|
* @return
|
|
*/
|
|
@RequestMapping("toImgsUpload")
|
|
public String toImgsUpload(Model model,FileUploadInfoBean o) {
|
|
|
|
List<FileUploadInfoBean> imgs = dao.findListByOwnerId(o);
|
|
model.addAttribute("imgs", imgs);
|
|
model.addAttribute("obj", o);
|
|
|
|
return "imgsUpload";
|
|
}
|
|
|
|
/**
|
|
* @Author 无畏
|
|
* @Date 2019-06-16
|
|
* @function 上传图片
|
|
* @param o 需传来modelFlag 模块标记 和 ownerId 所属者的id 及保存的文件夹名
|
|
* @return
|
|
*/
|
|
@RequestMapping("imgsUpload")
|
|
@ResponseBody
|
|
public List<String> imgsUpload(HttpServletRequest request,FileUploadInfoBean uploadFile) {
|
|
String fileName = "";
|
|
//String saveDirectory = request.getSession().getServletContext().getRealPath("/agreeImg");
|
|
String filePath = "";
|
|
String folderName = uploadFile.getFolderName();
|
|
|
|
String realSavePath = PropertyHelper.getPropertyByKey("fileSavePath");
|
|
|
|
uploadFile.setFolderName("/"+uploadFile.getFolderName());
|
|
|
|
if(StringHelper.isNotEmpty(folderName)){
|
|
filePath = realSavePath+"/"+folderName;
|
|
}else {
|
|
filePath = realSavePath;
|
|
}
|
|
|
|
File file = new File(filePath);
|
|
if (!file.exists()) {
|
|
file.mkdirs();
|
|
}
|
|
|
|
String newurl="";
|
|
// 打印上传路径信息
|
|
System.out.println("文件保存路径=" + filePath);
|
|
int maxPostSize = 200 * 1024 * 1024;
|
|
MultipartRequest multi = null;
|
|
DefaultFileRenamePolicy dfp = new DefaultFileRenamePolicy();
|
|
try {
|
|
multi = new MultipartRequest(request, filePath, maxPostSize, "UTF-8", dfp);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
// 输出反馈信息
|
|
Enumeration<?> files = multi.getFileNames();
|
|
List<String> urls = new ArrayList<String>();
|
|
while (files.hasMoreElements()) {
|
|
String name = (String) files.nextElement();
|
|
File f = multi.getFile(name);
|
|
if (f != null) {
|
|
long newName=System.currentTimeMillis();
|
|
fileName = multi.getFilesystemName(name);
|
|
int begin = fileName.indexOf(".");
|
|
int last = fileName.length();
|
|
String suffix = fileName.substring(begin,last);
|
|
String onlyfileName = fileName.substring(0,begin);
|
|
uploadFile.setPreName(onlyfileName);
|
|
uploadFile.setSuffix(suffix);
|
|
File oldfile = new File(filePath + "/" + fileName);
|
|
File newfile = new File(filePath + "/" + newName + suffix);
|
|
newurl=newName+suffix;
|
|
oldfile.renameTo(newfile);
|
|
uploadFile.setSaveName(newName+"");
|
|
uploadFile.setUploadTime(new Date());
|
|
uploadFile.setCreator(UserShiroHelper.getCurrentUser());
|
|
dao.insertBean(uploadFile);
|
|
|
|
}
|
|
String json = "{\"preName\":\""+uploadFile.getPreName()+"\",\"url\":\"/img/"+folderName+"/"+newurl+"\",\"id\":"+uploadFile.getId()+"}";
|
|
urls.add(json);
|
|
}
|
|
|
|
System.out.println(urls);
|
|
return urls;
|
|
}
|
|
|
|
/**
|
|
* @Author 无畏
|
|
* @Date 2019-07-16
|
|
* @function 上传文件到插入数据库
|
|
* @param o 需传来modelFlag 模块标记 和 ownerId 所属者的id 及保存的文件夹名
|
|
* @return
|
|
*/
|
|
@RequestMapping("uploadFileImportDB")
|
|
@ResponseBody
|
|
public AjaxRes uploadFileImportDB(HttpServletRequest request,FileUploadInfoBean uploadFile) {
|
|
|
|
String realSavePath = PropertyHelper.getPropertyByKey("fileSavePath");
|
|
AjaxRes ar = new AjaxRes();
|
|
Integer result = 0;
|
|
try {
|
|
String className=GlobalConst.map.get(uploadFile.getModelFlag());
|
|
String fileName = "";
|
|
String filePath = "";
|
|
String folderName = "/temp";
|
|
if(StringHelper.isNotEmpty(folderName)){
|
|
filePath = realSavePath+folderName;
|
|
}else {
|
|
filePath = realSavePath;
|
|
}
|
|
File file = new File(filePath);
|
|
if (!file.exists()) {
|
|
file.mkdirs();
|
|
}
|
|
// 打印上传路径信息
|
|
int maxPostSize = 200 * 1024 * 1024;
|
|
MultipartRequest multi = null;
|
|
DefaultFileRenamePolicy dfp = new DefaultFileRenamePolicy();
|
|
try {
|
|
multi = new MultipartRequest(request, filePath, maxPostSize, "UTF-8", dfp);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
// 输出反馈信息
|
|
Enumeration<?> files = multi.getFileNames();
|
|
while (files.hasMoreElements()) {
|
|
String name = (String) files.nextElement();
|
|
File f = multi.getFile(name);
|
|
if (f != null) {
|
|
long newName=System.currentTimeMillis();
|
|
fileName = multi.getFilesystemName(name);
|
|
int begin = fileName.indexOf(".");
|
|
int last = fileName.length();
|
|
String suffix = fileName.substring(begin,last);
|
|
File oldfile = new File(filePath + "/" + fileName);
|
|
File newfile = new File(filePath + "/" + newName + suffix);
|
|
oldfile.renameTo(newfile);
|
|
filePath = filePath + "/" + newName + suffix;
|
|
|
|
List<List<String>> info = ExcelUtils.extractExcelFileInfo(filePath);
|
|
result = insertInfo(info,className);
|
|
File tempFile = new File(filePath);
|
|
tempFile.delete();
|
|
}
|
|
}
|
|
if(result == 1) {
|
|
ar.setSucceedMsg("信息录入成功!");
|
|
}else {
|
|
ar.setFailMsg("信息录入失败!");
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
ar.setFailMsg("信息录入失败!");
|
|
}
|
|
return ar;
|
|
}
|
|
|
|
|
|
public Integer insertInfo(List<List<String>> info,String className) {
|
|
Integer result = 0;
|
|
try {
|
|
String[] param = ReflectUtil.getTypeAnnotation(className);
|
|
String[] columns = ReflectUtil.getFieldColumn(className);
|
|
List<Map<String, String>> backstageValue = ReflectUtil.getBackstageValue(className);
|
|
|
|
int bSize = backstageValue.size();
|
|
int length = columns.length;
|
|
|
|
String[] type = ReflectUtil.getTypeColumn(className);
|
|
|
|
int size = info.size();
|
|
|
|
String c = "";
|
|
for(int i = 0;i<columns.length;i++) {
|
|
if("".equals(c)) {
|
|
c = columns[i];
|
|
}else {
|
|
c+=","+columns[i];
|
|
}
|
|
}
|
|
|
|
for(int i = 0;i<backstageValue.size();i++) {
|
|
c+=","+backstageValue.get(i).get("column");
|
|
}
|
|
|
|
String val = "";
|
|
List<String> list = null;
|
|
String v = "";
|
|
String t = "";
|
|
for(int i = 0; i<size ; i++) {
|
|
list = info.get(i);
|
|
v = "";
|
|
for(int j = 0; j<length ; j++) {
|
|
t = type[j];
|
|
if("".equals(v)) {
|
|
if("Integer".equals(t)) {
|
|
v = "("+list.get(j);
|
|
}else if("String".equals(t)){
|
|
if(list.get(j) == null) {
|
|
v += "("+list.get(j);
|
|
}else {
|
|
v = "('"+list.get(j)+"'";
|
|
}
|
|
}
|
|
}else {
|
|
if("Integer".equals(t)) {
|
|
v += ","+list.get(j);
|
|
}else if("String".equals(t)){
|
|
if(list.get(j) == null) {
|
|
v += ","+list.get(j);
|
|
}else {
|
|
v += ",'"+list.get(j)+"'";
|
|
}
|
|
}
|
|
}
|
|
if(bSize == 0 && j == length-1) {
|
|
v += ")";
|
|
}
|
|
}
|
|
String d = "";
|
|
for(int j = 0; j<bSize ; j++) {
|
|
d = backstageValue.get(j).get("value");
|
|
t = backstageValue.get(j).get("jdbcType");
|
|
if("currentAccount".equals(d)) {
|
|
if("Integer".equals(t)) {
|
|
v += ","+UserShiroHelper.getRealCurrentUser().getId();
|
|
}else if("String".equals(t)) {
|
|
v += ",'"+UserShiroHelper.getRealCurrentUser().getName()+"'";
|
|
}
|
|
}
|
|
if("currentDateTime".equals(d)) {
|
|
v += ",'"+DateTimeHelper.currentDateTime()+"'";
|
|
}
|
|
if(j == bSize-1) {
|
|
v += ")";
|
|
}
|
|
}
|
|
|
|
if("".equals(val)) {
|
|
val = v;
|
|
}else {
|
|
val +=","+ v;
|
|
}
|
|
}
|
|
|
|
String sql = " insert into "+ param[0] +"("+c+") value"+val;
|
|
|
|
System.out.println(sql);
|
|
|
|
jdbcTemplate.execute(sql);
|
|
|
|
result = 1;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* * 访问系统首页
|
|
*/
|
|
@RequestMapping("deleteImg")
|
|
@ResponseBody
|
|
public AjaxRes deleteImg(@RequestBody FileUploadInfoBean o){
|
|
AjaxRes ar = getAjaxRes();
|
|
try {
|
|
String realSavePath = PropertyHelper.getPropertyByKey("fileSavePath");
|
|
FileUploadInfoBean img = dao.findByFileId(o);
|
|
dao.deleteByFileId(o);
|
|
String path = realSavePath+"/"+img.getFolderName()+"/"+img.getSaveName()+img.getSuffix();
|
|
|
|
File file = new File(path);
|
|
|
|
if (file.exists()) {
|
|
file.delete();
|
|
}
|
|
ar.setRes(GlobalConst.SUCCEED);
|
|
ar.setResMsg("图片删除成功!");
|
|
} catch (Exception e) {
|
|
ar.setResMsg("图片删除失败!");
|
|
}
|
|
return ar;
|
|
}
|
|
|
|
@RequestMapping("uploadFile")
|
|
public String toAdd(Model model) {
|
|
return "/sys/fileUpload";
|
|
}
|
|
}
|