layui⾃定义模块、⽇志打印使⽤、layui⽂件上传云服务器1.layui⾃定义模块:⽅便接⼝调⽤
异步请求url地址不⽅便
解决:扩展模块
1):定义对应js⽂件 ja>lay-module创建http⽬录创建http.js⽂件
2):lay-config.js⽂件中引⼊模板
3):页⾯使⽤引⼊js⽂件
不适⽤组件引⼊url⽅式,更简单
1).js⽂件下创建http.js⽂件
const $api="localhost:8070/"
2).使⽤
引⼊js⽂件路径。url路径为 url:$api+’…’2.layui⽂件上传 参考码云代码
使⽤fastDBS:轻量式分布式⽂件系统
layui具有⽂件上传组件
var uploadInst = der({
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","47.114.97.182/"+res.data)
},
error:function(){
//请求异常回调
}
})
/**
* 图⽚⽂件上传
* @param file
* @return
*/
@PostMapping("uploadIma")
public Dto uploadIma(MultipartFile file,String url){
//问题图⽚重复上传界⾯最好显⽰对应图⽚
if(file.isEmpty()){
urnFail("⽂件夹为空",500);
}
if(url != null || url.equals("")){
//执⾏删除操作 split:把字符串划分成数组第0个值
FastDFSClientUtils.delete(url.split("/")[0],url);
}
try{
// //获取图⽚⼆进制流
// String file1 = FastDFSClientUtils.Bytes(),OriginalFilename());
// //服务器存储图⽚路径
// String file2 = "47.114.97.182/";
// //拼接字符串
/
/ String file3 = file2+file1;
// //图⽚⼆进制流获取图⽚名称后缀上传成功后返回字符串
// Image i = new Image();
// i.setSrc(file3);
urnSuccess("上传成功",FastDFSClientUtils.Bytes(),OriginalFilename())); }catch (Exception e){
<("异常"+e);
e.printStackTrace();
}
urnFail("上传失败,请联系管理员",50);
}
⼯具类
package com.shopmon;
import org.csourcemon.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("f").getPath(); // private static Logger logger = Logger(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) {
// ("",e);
}
}
/**
* <B>⽅法名称:</B>上传⽅法<BR>
* <B>概要说明:</B><BR>
* @param file ⽂件
layui下载* @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];
}
trackerServer = Connection();
if (trackerServer == null) {
// ("getConnection return null");
return null;
}
storageServer = StoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
String fileid = storageClient1.upload_file1(file_buff, getFileExt(path), meta_list);
return fileid;
} catch (Exception ex) {
// ("",ex);
return null;
}finally{
if (fis != null){
try {
fis.close();
} catch (IOException e) {
// ("",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;
}
}
/**
* <B>⽅法名称:</B>上传⽅法<BR>
* <B>概要说明:</B><BR>
* @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 = Connection();
if (trackerServer == null) {
// ("getConnection return null");
return null;
}
storageServer = StoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
String fileid = storageClient1.upload_file1(data, getFileExt(fileName), meta_list); return fileid;
} catch (Exception ex) {
// ("",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();
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论