mirror of
https://github.com/Hello-hao/Tbed.git
synced 2024-04-21 12:32:10 +00:00
重构版功能实现
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package cn.hellohao.utils;
|
||||
|
||||
import cn.hellohao.pojo.Keys;
|
||||
import cn.hellohao.pojo.ReturnImage;
|
||||
import cn.hellohao.pojo.UploadConfig;
|
||||
import cn.hellohao.service.impl.KeysServiceImpl;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
@@ -11,127 +13,57 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class LocUpdateImg {
|
||||
public static void deleteLOCImg(String imagename){
|
||||
String filePath =File.separator + "HellohaoData" + File.separator+imagename;
|
||||
File file = new File(filePath);
|
||||
file.delete();
|
||||
public static boolean deleteLOCImg(String imagename){
|
||||
boolean isDele = false;
|
||||
try {
|
||||
String filePath =File.separator + "HellohaoData" + File.separator+imagename;
|
||||
File file = new File(filePath);
|
||||
isDele = file.delete();
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
isDele = false;
|
||||
}
|
||||
return isDele;
|
||||
}
|
||||
|
||||
public static Map<ReturnImage, Integer> ImageuploadLOC(Map<String, MultipartFile> fileMap, String username,
|
||||
Map<String, String> fileMap2,Integer setday) throws Exception {
|
||||
String filePath =File.separator + "HellohaoData" + File.separator;
|
||||
if(fileMap2==null){
|
||||
File file = null;
|
||||
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
|
||||
for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
|
||||
String uuid = "TOIMG"+UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位
|
||||
java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
|
||||
String times = format1.format(new Date());
|
||||
// 上传文件流。
|
||||
System.out.println("待上传的图片:"+username + File.separator + uuid+times + "N." + entry.getKey());
|
||||
File dest = new File(filePath + username + File.separator+ uuid+times + "N." + entry.getKey());
|
||||
File temppath = new File(filePath + username+ File.separator );
|
||||
if (!dest.getParentFile().exists()) {
|
||||
dest.getParentFile().mkdirs();
|
||||
}
|
||||
try {
|
||||
MultipartFile multipartFile = entry.getValue();
|
||||
//FileInputStream fileInputStream = (FileInputStream) multipartFile.getInputStream();
|
||||
InputStream fileInputStream = (InputStream)multipartFile.getInputStream();
|
||||
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
|
||||
byte[] bs = new byte[1024];
|
||||
int len;
|
||||
while ((len = fileInputStream.read(bs)) != -1) {
|
||||
bos.write(bs, 0, len);
|
||||
}
|
||||
bos.flush();
|
||||
bos.close();
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
returnImage.setImgname(username + "/" +uuid+times + "N." + entry.getKey());//entry.getValue().getOriginalFilename()
|
||||
returnImage.setImgurl(username + "/" + uuid+times + "N." + entry.getKey());
|
||||
ImgUrl.put(returnImage, (int) (entry.getValue().getSize()));
|
||||
if(setday>0) {
|
||||
String deleimg = DateUtils.plusDay(setday);
|
||||
DeleImg.charu(username + "/" + uuid + times + "N." + entry.getKey() + "|" + deleimg + "|" + "5");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("上传失败");
|
||||
}
|
||||
}
|
||||
return ImgUrl;
|
||||
}else{
|
||||
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
|
||||
for (Map.Entry<String, String> entry : fileMap2.entrySet()) {
|
||||
String uuid = "TOIMG"+UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位
|
||||
java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
|
||||
String times = format1.format(new Date());
|
||||
String oldfilePath = entry.getValue();
|
||||
String newfilePath = File.separator + "HellohaoData" + File.separator + username + File.separator+ uuid+times + "N." + entry.getKey();
|
||||
String tempfilePath = File.separator + "HellohaoData" + File.separator + username+ File.separator ;
|
||||
File file = new File(oldfilePath);
|
||||
File targetFile =new File(tempfilePath);
|
||||
if(!targetFile.getParentFile().exists()) {
|
||||
targetFile.mkdirs();
|
||||
}
|
||||
file.renameTo(new File(newfilePath));
|
||||
//采用数据流模式上传文件(节省内存),自动创建父级目录
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
//username + "/" + uuid+times + "." + entry.getKey()
|
||||
returnImage.setImgurl(username + "/" + uuid+times + "N." + entry.getKey());
|
||||
ImgUrl.put( returnImage, ImgUrlUtil.getFileSize2(new File(newfilePath)));
|
||||
if(setday>0) {
|
||||
String deleimg = DateUtils.plusDay(setday);
|
||||
DeleImg.charu(username + "/" + uuid + times + "." + entry.getKey() + "|" + deleimg + "|" + "5");
|
||||
}
|
||||
}
|
||||
return ImgUrl;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户端接口
|
||||
* */
|
||||
public static Map<ReturnImage, Integer> clientLocUpdateImg(Map<String, MultipartFile> fileMap, String username, UploadConfig uploadConfig) {
|
||||
public static ReturnImage ImageuploadLOC(Map<String, File> fileMap, String username,Integer keyID) throws Exception {
|
||||
KeysServiceImpl keysService = SpringContextHolder.getBean(KeysServiceImpl.class);
|
||||
final Keys key = keysService.selectKeys(keyID);
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
String filePath =File.separator + "HellohaoData" + File.separator;
|
||||
File file = null;
|
||||
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
|
||||
for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
|
||||
String uuid = "TOIMG"+UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位
|
||||
java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
|
||||
String times = format1.format(new Date());
|
||||
System.out.println("待上传的图片:"+username + "/" + uuid+times + "N." + entry.getKey());
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
if(entry.getValue().getSize()/1024<=uploadConfig.getFilesizeuser()*1024){
|
||||
File dest = new File(filePath + username + File.separator+ uuid+times + "N." + entry.getKey());
|
||||
if (!dest.getParentFile().exists()) {
|
||||
dest.getParentFile().mkdirs();
|
||||
for (Map.Entry<String, File> entry : fileMap.entrySet()) {
|
||||
String ShortUID = SetText.getShortUuid();
|
||||
System.out.println("待上传的图片:"+username + File.separator + ShortUID + "." + entry.getKey());
|
||||
File dest = new File(filePath + username + File.separator+ ShortUID + "." + entry.getKey());
|
||||
File temppath = new File(filePath + username+ File.separator );
|
||||
if (!dest.getParentFile().exists()) {
|
||||
dest.getParentFile().mkdirs();
|
||||
}
|
||||
try {
|
||||
InputStream fileInputStream = new FileInputStream(entry.getValue());
|
||||
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
|
||||
byte[] bs = new byte[1024];
|
||||
int len;
|
||||
while ((len = fileInputStream.read(bs)) != -1) {
|
||||
bos.write(bs, 0, len);
|
||||
}
|
||||
try {
|
||||
MultipartFile multipartFile = entry.getValue();
|
||||
FileInputStream fileInputStream = (FileInputStream) multipartFile.getInputStream();
|
||||
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
|
||||
byte[] bs = new byte[1024];
|
||||
int len;
|
||||
while ((len = fileInputStream.read(bs)) != -1) {
|
||||
bos.write(bs, 0, len);
|
||||
}
|
||||
bos.flush();
|
||||
bos.close();
|
||||
returnImage.setImgname(entry.getValue().getOriginalFilename());
|
||||
returnImage.setImgurl(username + "/" + uuid+times + "N." + entry.getKey());
|
||||
ImgUrl.put(returnImage, (int) (entry.getValue().getSize())/1024);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("上传失败");
|
||||
}
|
||||
}else{
|
||||
returnImage.setImgname(entry.getValue().getOriginalFilename());
|
||||
returnImage.setImgurl("文件超出系统设定大小,不得超过");
|
||||
ImgUrl.put(returnImage,-1);
|
||||
bos.flush();
|
||||
bos.close();
|
||||
returnImage.setUid(ShortUID);
|
||||
returnImage.setImgname(username + "/" +ShortUID + "." + entry.getKey());//entry.getValue().getOriginalFilename()
|
||||
returnImage.setImgurl(key.getRequestAddress() +"/ota/"+username + "/" + ShortUID + "." + entry.getKey());
|
||||
returnImage.setImgSize(entry.getValue().length());
|
||||
returnImage.setCode("200");
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
returnImage.setCode("500");
|
||||
System.err.println("上传失败");
|
||||
}
|
||||
}
|
||||
return ImgUrl;
|
||||
return returnImage;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user