首页>代码>JFinal-layui极速开发企业应用系统-专业版>/Jfinal-layui/src/main/java/com/qinhailin/common/base/service/FileService.java
/**
* Copyright 2019-2021 覃海林(qinhaisenlin@163.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qinhailin.common.base.service;
import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.qinhailin.common.model.FileUploaded;
import com.qinhailin.common.kit.ExcelKit;
import com.qinhailin.common.kit.IdKit;
import com.qinhailin.common.kit.SqlKit;
/**
* 附件管理
*
* @author QinHaiLin
*
*/
public class FileService {
private FileUploaded dao = new FileUploaded().dao();
public boolean save(FileUploaded entity) {
return entity.save();
}
public FileUploaded queryFileUploadedByUrl(String url){
List<FileUploaded> list = dao.find("select * from file_uploaded where url=?", url);
if (list.size() > 0) {
return list.get(0);
}
return null;
}
public FileUploaded queryFileUploadedByObjectId(String objectId) {
List<FileUploaded> list = dao.find("select * from file_uploaded where object_id=? order by create_time desc", objectId);
if (list.size() > 0) {
return list.get(0);
}
return null;
}
public List<FileUploaded> queryFileUploadedListByObjectId(String objectId) {
return dao.find("select * from file_uploaded where object_id=?", objectId);
}
public void delete(FileUploaded entity) {
File file = new File(entity.getSavePath());
if (file.exists()) {
file.delete();
}
entity.delete();
}
/**
* 批量删除文件及记录
* @param modelList
*/
public void delete(List<FileUploaded> modelList){
Object[][] paras=new Object[modelList.size()][1];
int i=0;
for(FileUploaded entity:modelList){
File file = new File(entity.getSavePath());
if (file.exists()) {
file.delete();
}
paras[i][0]=entity.getId();
i++;
}
String sql="delete from file_uploaded where id=?";
Db.batch(sql, paras, 100);
}
public void deleteFile(String url) {
List<FileUploaded> list = dao.find("select * from file_uploaded where url=?", url);
delete(list.get(0));
}
public void deleteFiles(List<String> urls) {
List<FileUploaded> list = dao.find("select * from file_uploaded where url in " + SqlKit.joinIds(urls));
delete(list);
}
public void deleteFileByObjectId(String objectId) {
List<FileUploaded> list = dao.find("select * from file_uploaded where object_id=?", objectId);
this.delete(list);
}
/**
* 删除导入数据的文件
* @param uf
*/
public void deleteFile(UploadFile uf) {
File file = new File(uf.getUploadPath() + "/" + uf.getFileName());
if (file.exists()) {
file.delete();
}
}
/**
* 保存文件记录
* @param uploadFile
* @param objectId
* 附件关联对象Id
* @return
*/
public String saveFile(UploadFile uploadFile, String objectId) {
FileUploaded entity=this.createFileUploaded(uploadFile,objectId);
if(entity==null) {
return "";
}
entity.save();
return entity.getUrl();
}
public String saveFile(UploadFile uf) {
return this.saveFile(uf, null);
}
/**
* 保存文件记录
*
* @param list
* @return
*/
public List<String> saveFiles(List<UploadFile> list) {
return this.saveFiles(list,null);
}
/**
* 保存文件记录
*
* @param list
* @param objectId
* 附件关联对象Id
* @return
*/
public List<String> saveFiles(List<UploadFile> list, String objectId) {
List<String> results = new ArrayList<String>();
List<FileUploaded> modelList=new ArrayList<>();
for (UploadFile uf : list) {
FileUploaded entity=createFileUploaded(uf, objectId);
if(entity!=null){
modelList.add(entity);
results.add(entity.getUrl());
}
}
Db.batchSave(modelList, 50);
return results;
}
/**
* 导入xls数据
*
* @param uf
* @param sql
* insert into game_theme (id, state, title) values(?,?,?)
* @return
*/
public boolean importExcel(UploadFile uf, String sql) {
return Db.tx(new IAtom() {
boolean save_flag = true;
@Override
public boolean run() throws SQLException {
try {
UploadFile up = uf;
List<Object[]> list = ExcelKit.getExcelData(up.getFile());
Object[][] objs=new Object[list.size()][list.get(0).length+1];
for(int i=0;i<list.size();i++){
for(int j=0;j<list.get(0).length;j++){
if(j==0){
objs[i][0]=IdKit.createUUID();
objs[i][j+1]=list.get(i)[j];
}else{
objs[i][j+1]=list.get(i)[j];
}
}
}
//批量导入
Db.batch(sql, objs, 100);
} catch (Exception e) {
save_flag = false;
e.printStackTrace();
}
return save_flag;
}
});
}
/**
* 导出excel.xlsx
*
* @param response
* @param title
* 标题,如:String[] title={"姓名","性别","年龄",...}
* @param fileName
* 文件名,如:用户信息
* @param list
* map集合,key=0,1,2,3...,用数字作为key避免数据错乱
* @return
* @author qinhailin
* @date 2018年8月6日
*/
public void exportExcelxlsx(HttpServletResponse response, String[] title, String fileName, List<Record> list) {
try {
ExcelKit.exportExcelxlsx(response, title, fileName, list);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void exportExcelxlsx(HttpServletResponse response, String[] title, List<Record> list) {
try {
ExcelKit.exportExcelxlsx(response, title, null, list);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
*
* @param response
* @param title
* 标题如:String[] title={"姓名","性别","年龄",...}
* @param fileName
* 文件名,如:用户信息
* @param sql
* 如:select name as '0',sex as '1',age as '2' from user
* @author qinhailin
* @date 2018年8月14日
*/
public void exportExcelxlsx(HttpServletResponse response, String[] title, String fileName, String sql) {
try {
List<Record> list = Db.find(sql);
ExcelKit.exportExcelxlsx(response, title, fileName, list);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void exportExcelxlsx(HttpServletResponse response, String[] title, String sql) {
try {
List<Record> list = Db.find(sql);
ExcelKit.exportExcelxlsx(response, title, null, list);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 导出excel数据
* @param response
* @param title
* 标题如:String[] title={"姓名","性别","年龄",...}
* @param fileName
* 文件名,如:用户信息
* @param sql
* 如:select name as '0',sex as '1',age as '2' from user where
* name=?
* @param paras
* sql查询参数
*/
public void exportExcelxls(HttpServletResponse response, String[] title, String fileName, String sql,
Object... paras) {
try {
List<Record> list = Db.find(sql, paras);
ExcelKit.exportExcelxlsx(response, title, fileName, list);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void exportExcelxlsx(HttpServletResponse response, String[] title, String sql, Object... paras) {
try {
List<Record> list = Db.find(sql, paras);
ExcelKit.exportExcelxlsx(response, title, null, list);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 创建文件上传文件对象信息
* @param uploadFile
* @param objectId
* @return
*/
private FileUploaded createFileUploaded(UploadFile uploadFile, String objectId){
if(uploadFile==null) {
return null;
}
// 获取已上传的文件
File file = uploadFile.getFile();
String filePath = uploadFile.getUploadPath();
String fileName = uploadFile.getFileName();
String subfix = fileName.substring(fileName.lastIndexOf("."));
long fileSize = file.length();
// 创建目录
String rename = IdKit.createFileId();
String date = rename.substring(0, 8);
String newFilePath = filePath;
File f = new File(newFilePath, date);
if (!f.exists()) {
f.mkdirs();
}
// 文件重命名
boolean b = file.renameTo(new File(f, rename + subfix));
if (b) {
filePath = newFilePath + "/" + date + "/" + rename + subfix;
} else {
filePath = filePath + "/" + fileName;
date = "";
rename = fileName.substring(0, fileName.indexOf("."));
}
// 保存记录
FileUploaded fileUpload = new FileUploaded();
fileUpload.setId(IdKit.createIdWorker());
fileUpload.setFileName(fileName);
fileUpload.setSavePath(filePath);
fileUpload.setFileSize(fileSize);
fileUpload.setCreateTime(new Date());
fileUpload.setObjectId(objectId);
fileUpload.setUrl(date + "/" + rename);
return fileUpload;
}
}
最近下载更多
z875152686 LV8
2023年11月2日
2602275348 LV12
2022年4月8日
zcl02036716 LV17
2022年2月28日
微量的记忆 LV10
2022年2月11日
最代码官方 LV168
2021年10月5日
最近浏览更多
Frank_F
2024年7月5日
暂无贡献等级
myfz0662 LV10
2024年5月8日
云破月 LV8
2024年4月12日
akittyboy LV9
2024年3月31日
z875152686 LV8
2023年11月2日
abc562311934 LV4
2022年10月8日
1986王志飞 LV6
2022年8月12日
bluesky2016 LV15
2022年6月10日
a318888331 LV13
2022年4月11日
2602275348 LV12
2022年3月31日

