mirror of
https://github.com/Hello-hao/Tbed.git
synced 2024-04-21 12:32:10 +00:00
重构版功能实现
This commit is contained in:
@@ -20,76 +20,43 @@ public class FTPImageupload {
|
||||
static FTPClient ftpClient1 ;
|
||||
static Keys key;
|
||||
|
||||
public Map<ReturnImage, Integer> ImageuploadFTP(Map<String, MultipartFile> fileMap, String username,
|
||||
Map<String, String> fileMap2,Integer setday) {
|
||||
public ReturnImage ImageuploadFTP(Map<String, File> fileMap, String username,Integer keyID) {
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
Keys key = null;
|
||||
String[] host = key.getEndpoint().split("\\:");
|
||||
String h = host[0];
|
||||
Integer p = Integer.parseInt(host[1]);
|
||||
//创建FTP客户端,所有的操作都基于FTPClinet
|
||||
FTPUtils ftps = new FTPUtils(h, p, key.getAccessKey(), key.getAccessSecret());
|
||||
boolean flag = ftps.open();
|
||||
if(fileMap2==null){
|
||||
File file = null;
|
||||
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
|
||||
ftps.mkDir(File.separator+username);
|
||||
try {
|
||||
for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
|
||||
String uuid = 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());
|
||||
file = SetFiles.changeFile(entry.getValue());
|
||||
String userkey =username + "/"+ uuid+times + "." + entry.getKey();
|
||||
if (flag) {
|
||||
ftps.upload(file, "/"+userkey, "");
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
returnImage.setImgname(userkey);//entry.getValue().getOriginalFilename()
|
||||
File file = null;
|
||||
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
|
||||
ftps.mkDir(File.separator+username);
|
||||
try {
|
||||
for (Map.Entry<String, File> entry : fileMap.entrySet()) {
|
||||
String ShortUID = SetText.getShortUuid();
|
||||
file = entry.getValue();
|
||||
String userkey =username + "/"+ ShortUID + "." + entry.getKey();
|
||||
if (flag) {
|
||||
boolean isUpload = ftps.upload(file, "/" + userkey, "");
|
||||
if(isUpload){
|
||||
returnImage.setUid(ShortUID);
|
||||
returnImage.setImgname(userkey);
|
||||
returnImage.setImgurl(key.getRequestAddress() + "/"+ userkey);
|
||||
ImgUrl.put(returnImage, (int) (entry.getValue().getSize()));
|
||||
if(setday>0) {
|
||||
String deleimg = DateUtils.plusDay(setday);
|
||||
DeleImg.charu(username + "/" + uuid + times + "." + entry.getKey() + "|" + deleimg + "|" + "7");
|
||||
}
|
||||
ftps.close();
|
||||
returnImage.setImgSize(entry.getValue().length());
|
||||
returnImage.setCode("200");
|
||||
}else{
|
||||
returnImage.setCode("500");
|
||||
}
|
||||
|
||||
Print.Normal("要上传的文件路径:"+"/"+userkey);
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
ImgUrl.put(null, 500);
|
||||
Print.Normal("要上传的文件路径:/"+userkey);
|
||||
}
|
||||
return ImgUrl;
|
||||
}else{
|
||||
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
|
||||
try {
|
||||
for (Map.Entry<String, String> entry : fileMap2.entrySet()) {
|
||||
String uuid = 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 imgurl = entry.getValue();
|
||||
File file = new File(imgurl);
|
||||
String userkey =username + File.separator+ uuid+times + "." + entry.getKey();
|
||||
ftps.mkDir(File.separator+username);
|
||||
if (flag) {
|
||||
ftps.upload(file, "/"+userkey, "");
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
returnImage.setImgurl(key.getRequestAddress() + File.separator+ userkey);
|
||||
ImgUrl.put(returnImage, ImgUrlUtil.getFileSize2(new File(imgurl)));
|
||||
if(setday>0) {
|
||||
String deleimg = DateUtils.plusDay(setday);
|
||||
DeleImg.charu(username + "/" + uuid + times + "." + entry.getKey() + "|" + deleimg + "|" + "7");
|
||||
}
|
||||
ftps.close();
|
||||
}
|
||||
boolean bb= new File(imgurl).getAbsoluteFile().delete();
|
||||
Print.Normal("删除情况"+bb);
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
ImgUrl.put(null, 500);
|
||||
}
|
||||
return ImgUrl;
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
returnImage.setCode("500");
|
||||
// ImgUrl.put(null, 500);
|
||||
}
|
||||
return returnImage;
|
||||
|
||||
}
|
||||
|
||||
//初始化FTP对象存储
|
||||
@@ -98,85 +65,38 @@ public class FTPImageupload {
|
||||
if(k.getEndpoint()!=null && k.getAccessSecret()!=null && k.getEndpoint()!=null && k.getRequestAddress()!=null ) {
|
||||
if(!k.getEndpoint().equals("") && !k.getAccessSecret().equals("") && !k.getEndpoint().equals("") && !k.getRequestAddress().equals("") ) {
|
||||
FTPClient ftp = new FTPClient();
|
||||
ftp.setConnectTimeout(0);
|
||||
int flag = k.getEndpoint().indexOf(":");
|
||||
if(flag>0){
|
||||
String[] host = k.getEndpoint().split("\\:");
|
||||
String h = host[0];
|
||||
Integer p = Integer.parseInt(host[1]);
|
||||
try {
|
||||
if(!ftp.isConnected()){
|
||||
ftp.connect(h,p);
|
||||
}
|
||||
ftp.login(k.getAccessKey(), k.getAccessSecret());
|
||||
//获取服务器返回的状态码
|
||||
int reply = ftp.getReplyCode();
|
||||
/*
|
||||
* 判断是否连接成功
|
||||
* 所有以2开头的代码是正完成响应。
|
||||
* FTP服务器将在最终发送一个肯定的完成响应成功完成命令。
|
||||
*/
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
System.out.println("FTP - Waiting for configuration");
|
||||
ftp.disconnect();
|
||||
return -1;
|
||||
}
|
||||
ftpClient1 = ftp;
|
||||
key = k;
|
||||
ret = 1;
|
||||
// if(ftp.printWorkingDirectory()==null){
|
||||
// Print.warning("FTP初始化失败,配置项错误");
|
||||
// return -1;
|
||||
// }
|
||||
} catch (IOException e) {
|
||||
System.out.println("FTP - Waiting for configuration");
|
||||
if(flag>0){
|
||||
String[] host = k.getEndpoint().split("\\:");
|
||||
String h = host[0];
|
||||
Integer p = Integer.parseInt(host[1]);
|
||||
try {
|
||||
if(!ftp.isConnected()){
|
||||
ftp.connect(h,p);
|
||||
}
|
||||
ftp.login(k.getAccessKey(), k.getAccessSecret());
|
||||
int reply = ftp.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
System.out.println("FTP Object Is null");
|
||||
ftp.disconnect();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
/**
|
||||
* 客户端接口
|
||||
* */
|
||||
public Map<ReturnImage, Integer> clientuploadFTP(Map<String, MultipartFile> fileMap, String username, UploadConfig uploadConfig) throws Exception {
|
||||
String[] host = key.getEndpoint().split("\\:");
|
||||
String h = host[0];
|
||||
Integer p = Integer.parseInt(host[1]);
|
||||
//创建FTP客户端,所有的操作都基于FTPClinet
|
||||
FTPUtils ftps = new FTPUtils(h, p, key.getAccessKey(), key.getAccessSecret());
|
||||
boolean flag = ftps.open();
|
||||
File file = null;
|
||||
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
|
||||
for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
|
||||
String uuid = 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());
|
||||
file = SetFiles.changeFile_c(entry.getValue());
|
||||
String userkey =username + File.separator+ uuid+times + "." + entry.getKey();
|
||||
ftps.mkDir(File.separator+username);
|
||||
if (flag) {
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
if(entry.getValue().getSize()/1024<=uploadConfig.getFilesizeuser()*1024){
|
||||
ftps.upload(file, File.separator+userkey, "");
|
||||
|
||||
returnImage.setImgname(entry.getValue().getOriginalFilename());
|
||||
returnImage.setImgurl(key.getRequestAddress() + File.separator+ userkey);
|
||||
ImgUrl.put(returnImage, (int) (entry.getValue().getSize()));
|
||||
ftps.close();
|
||||
}else{
|
||||
returnImage.setImgname(entry.getValue().getOriginalFilename());
|
||||
returnImage.setImgurl("文件超出系统设定大小,不得超过");
|
||||
ImgUrl.put(returnImage, -1);
|
||||
ftps.close();
|
||||
ret = 1;
|
||||
key = k;
|
||||
// KeyObjectMap objectMap = new KeyObjectMap(ftp,k);
|
||||
} catch (IOException e) {
|
||||
System.out.println("FTP Object Is null");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ImgUrl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static void fuzhi(String p1,String p2){
|
||||
String a=p1;//定义要进行复制的文件路径
|
||||
String a=p1;
|
||||
try{
|
||||
File afile=new File(a);
|
||||
File bfile=new File(p2);//定义一个复制后的文件路径
|
||||
@@ -186,14 +106,32 @@ public class FTPImageupload {
|
||||
byte[] date=new byte[512];//定义一个byte数组
|
||||
int i=0;
|
||||
while((i=c.read(date))>0){//判断有没有读取到文件末尾
|
||||
d.write(date);//写数据
|
||||
d.write(date);
|
||||
}
|
||||
c.close();//关闭流
|
||||
d.close();//关闭流
|
||||
c.close();
|
||||
d.close();
|
||||
System.out.println("文件复制成功");}
|
||||
catch(IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean delFTP(Integer keyID, String fileName) {
|
||||
boolean b = true;
|
||||
try {
|
||||
String[] host = key.getEndpoint().split("\\:");
|
||||
String h = host[0];
|
||||
Integer p = Integer.parseInt(host[1]);
|
||||
//创建FTP客户端,所有的操作都基于FTPClinet
|
||||
FTPUtils ftps = new FTPUtils(h, p, key.getAccessKey(), key.getAccessSecret());
|
||||
ftps.open();
|
||||
b = ftps.deleteFile(fileName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
b = false;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user