1.layui自定义模块:方便接口调用
异步请求url地址不方便
解决:扩展模块
1):定义对应js文件 ja>lay-module创建http目录创建http.js文件
不适用组件引入url方式,更简单
1).js文件下创建http.js文件
const $api="http://localhost:8070/"
2).使用
引入js文件路径。url路径为 url:$api+’…’
2.layui文件上传 参考码云代码
使用fastDBS:轻量式分布式文件系统
layui具有文件上传组件
var uploadInst = upload.render({
elem:"#test1",//绑定元素
url:$api+"/img/uploadIma",//上传元素
data:{
/*
第一次上传url为空,则直接进行对应上传操作
上传完成后修改url地址
如果第二次,url不为空,执行删除对应url图片
删除对应组 group1
group1/M00/00/00/rBAv0F7xZEuAc_x_AAHyWR0ahwQ566.png
用户重复上传,需要把之前上传的图片删掉
*/
url:function(){
return $("#uImg").attr("src");
}
},
done:function(res){
//上传完毕回调
//group1/M00/00/00/rBAv0F7xZEuAc_x_AAHyWR0ahwQ566.png
console.log(res)
//上传成功后显示
$("#uImg").attr("src","http://47.114.97.182/"+res.data)
},
error:function(){
//请求异常回调
}
})
/**
* 图片文件上传
* @param file
* @return
*/
@PostMapping("uploadIma")
public Dto uploadIma(MultipartFile file,String url){
//问题 图片重复上传 界面最好显示对应图片
if(file.isEmpty()){
return DtoUtil.returnFail("文件夹为空",500);
}
if(url != null || url.equals("")){
//执行删除操作 split:把字符串划分成数组 第0个值
FastDFSClientUtils.delete(url.split("/")[0],url);
}
try{
// //获取图片二进制流
// String file1 = FastDFSClientUtils.upload(file.getBytes(),file.getOriginalFilename());
// //服务器存储图片路径
// String file2 = "http://47.114.97.182/";
// //拼接字符串
// String file3 = file2+file1;
// //图片二进制流 获取图片名称后缀 上传成功后返回字符串
// Image i = new Image();
// i.setSrc(file3);
return DtoUtil.returnSuccess("上传成功",FastDFSClientUtils.upload(file.getBytes(),file.getOriginalFilename()));
}catch (Exception e){
log.error("异常"+e);
e.printStackTrace();
}
return DtoUtil.returnFail("上传失败,请联系管理员",50);
}
工具类
package com.shop.common;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
import java.io.*;
public class FastDFSClientUtils {
private static final String CONF_FILENAME = Thread.currentThread().getContextClassLoader().getResource("fastdfs_client.conf").getPath();
// private static Logger logger = LoggerFactory.getLogger(FastDFSClientUtils.class);
private static TrackerClient trackerClient;
//加载文件
static {
try {
ClientGlobal.init(CONF_FILENAME);
TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
trackerClient = new TrackerClient(trackerGroup);
} catch (Exception e) {
// logger.error("",e);
}
}
/**
* 方法名称:上传方法
* 概要说明:
* @param file 文件
* @param path 路径
* @return 上传成功返回id,失败返回null
*/
public static String upload(File file, String path) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
FileInputStream fis = null;
try {
NameValuePair[] meta_list = null; // new NameValuePair[0];
fis = new FileInputStream(file);
byte[] file_buff = null;
if (fis != null) {
int len = fis.available();
file_buff = new byte[len];
fis.read(file_buff);
}
trackerServer = trackerClient.getConnection();
if (trackerServer == null) {
// logger.error("getConnection return null");
return null;
}
storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
String fileid = storageClient1.upload_file1(file_buff, getFileExt(path), meta_list);
return fileid;
} catch (Exception ex) {
// logger.error("",ex);
return null;
}finally{
if (fis != null){
try {
fis.close();
} catch (IOException e) {
// logger.error("",e);
}
}
if (storageServer != null){
try {
storageServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* 方法名称:上传方法
* 概要说明:
* @param data 数据
* @param fileName 扩展名
* @return 上传成功返回id,失败返回null
*/
public static String upload(byte[] data, String fileName) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
try {
NameValuePair[] meta_list = null; // new NameValuePair[0];
trackerServer = trackerClient.getConnection();
if (trackerServer == null) {
// logger.error("getConnection return null");
return null;
}
storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
String fileid = storageClient1.upload_file1(data, getFileExt(fileName), meta_list);
return fileid;
} catch (Exception ex) {
// logger.error("",ex);
return null;
}finally{
if (storageServer != null){
try {
storageServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* 方法名称:下载方法
* 概要说明:通过文件id进行下载
* @param fileId 文件id
* @return 返回InputStream
*/
public static InputStream download(String groupName, String fileId) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
try {
trackerServer = trackerClient.getConnection();
if (trackerServer == null) {
// logger.error("getConnection return null");
return null;
}
storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
storageClient1 = new StorageClient1(trackerServer, storageServer);
byte[] bytes = storageClient1.download_file1(fileId);
InputStream inputStream = new ByteArrayInputStream(bytes);
return inputStream;
} catch (Exception ex) {
// logger.error("",ex);
return null;
} finally {
if (storageServer != null){
try {
storageServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* 方法名称:删除方法
* 概要说明:根据id来删除一个文件
* @param fileId 文件id
* @return 删除成功返回0,非0则操作失败,返回错误代码
*/
public static int delete(String groupName, String fileId) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
try {
trackerServer = trackerClient.getConnection();
if (trackerServer == null) {
// logger.error("getConnection return null");
}
storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
storageClient1 = new StorageClient1(trackerServer, storageServer);
int result = storageClient1.delete_file1(fileId);
return result;
} catch (Exception ex) {
// logger.error("",ex);
return 0;
} finally {
if (storageServer != null){
try {
storageServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (trackerServer != null){
try {
trackerServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
storageClient1 = null;
}
}
/**
* 方法名称:
* 概要说明:
* @param oldFileId 旧文件id
* @param file 新文件
* @param path 新文件路径
* @return 上传成功返回id,失败返回null
*/
public static String modify(String oldGroupName, String oldFileId, File file, String path) {
String fileid = null;
try {
// 先上传
fileid = upload(file, path);
if (fileid == null) {
return null;
}
// 再删除
int delResult = delete(oldGroupName, oldFileId);
if (delResult != 0) {
return null;
}
} catch (Exception ex) {
// logger.error("",ex);
return null;
}
return fileid;
}
/**
* 方法名称:获取文件后缀名
* 概要说明:获取文件后缀名
* @param fileName
* @return 如:"jpg"、"txt"、"zip" 等
*/
private static String getFileExt(String fileName) {
if (fileName==null || !fileName.contains(".")) {
return "";
} else {
return fileName.substring(fileName.lastIndexOf(".") + 1);
}
}
}
3.日志的使用
log4j配置使用
引入依赖
org.springframework.boot
spring-boot-starter-log4j2
idea下载Maven Helper插件:用于解决jar包冲突
配置文件
java使用
static Logger log = LoggerFactory.getLogger(RotaionImgController.class);
方法中
log.info("进入list方法");//直接调方法即可 日志信息自动存储到本地路径文件下
java使用
static Logger log = LoggerFactory.getLogger(RotaionImgController.class);
方法中
log.info("进入list方法");//直接调方法即可 日志信息自动存储到本地路径文件下
Logger 在每一个方法里面写代码量增加
结合spring AOP处理日志
在AOP中记录方法执行情况,方便查找后端处理情况