parent
d28b028b46
commit
9d302e22e7
5 changed files with 448 additions and 5 deletions
@ -0,0 +1,126 @@ |
||||
package com.hisense.hiatmp.server_api.controller; |
||||
|
||||
import cn.hutool.core.io.FileUtil; |
||||
import com.hisense.hiatmp.model.common.ServerResponse; |
||||
import com.hisense.hiatmp.server_api.utils.FTPUtils; |
||||
import io.swagger.annotations.Api; |
||||
import io.swagger.annotations.ApiImplicitParam; |
||||
import io.swagger.annotations.ApiImplicitParams; |
||||
import io.swagger.annotations.ApiOperation; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.web.bind.annotation.*; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.InputStream; |
||||
import java.io.OutputStream; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
import java.util.UUID; |
||||
|
||||
@Api(tags="FTP操作") |
||||
@RequestMapping(value = "/ftp") |
||||
@RestController |
||||
public class FtpController { |
||||
private static final Logger log = LoggerFactory.getLogger(FtpController.class); |
||||
|
||||
/*@Autowired |
||||
private RedisUtil redisUtil;*/ |
||||
|
||||
@Autowired |
||||
private FTPUtils ftpUtils; |
||||
|
||||
/** |
||||
* 上传文件-通过本地路径上传文件到ftp |
||||
* */ |
||||
@ApiOperation(value="上传文件-通过本地路径上传文件到ftp") |
||||
@RequestMapping(value = "/uploadToFtp",method = RequestMethod.POST) |
||||
public ServerResponse uploadToFtp(@RequestBody String path) { |
||||
File file = new File(path); |
||||
//File file = new File("D:\\upload\\1.txt");
|
||||
String url = ""; |
||||
System.out.println(path+"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
||||
InputStream inputStream = FileUtil.getInputStream(file); |
||||
try { |
||||
String fileName = path.substring(path.lastIndexOf("/")+1); |
||||
url = ftpUtils.uploadToFtp(inputStream, fileName, false); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return ServerResponse.ok(url); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 上传文件-通过file上传到ftp |
||||
* */ |
||||
@ApiOperation(value="上传文件-通过file上传到ftp") |
||||
@RequestMapping(value = "/uploadFileToFtp",method = RequestMethod.POST) |
||||
@ApiImplicitParams({ |
||||
@ApiImplicitParam(paramType="form", name="file", dataType="MultipartFile", required=true, value="文件") |
||||
}) |
||||
public ServerResponse uploadFileToFtp(@RequestParam("file") MultipartFile mfile) { |
||||
List<String> list = Arrays.asList(".jpg",".jpeg",".png",".bmp",".avi" |
||||
,".mov",".rmvb",".rm",".mp4"); |
||||
//获取上传文件名,包含后缀
|
||||
String originalFilename = mfile.getOriginalFilename(); |
||||
//获取后缀
|
||||
String substring = originalFilename.substring(originalFilename.lastIndexOf(".")); |
||||
if(!list.contains(substring.toLowerCase())){ |
||||
return ServerResponse.error("请上传图片、视频文件"); |
||||
} |
||||
//保存的文件名
|
||||
String dFileName = UUID.randomUUID()+substring; |
||||
String url = ""; |
||||
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); |
||||
try { |
||||
File file = multipartFileToFile(mfile); |
||||
InputStream inputStream = FileUtil.getInputStream(file); |
||||
url = ftpUtils.uploadToFtp(inputStream, dFileName, false); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return ServerResponse.ok(url); |
||||
} |
||||
|
||||
public static File multipartFileToFile(MultipartFile file) throws Exception { |
||||
File toFile = null; |
||||
if (file.equals("") || file.getSize() <= 0) { |
||||
file = null; |
||||
} else { |
||||
InputStream ins = null; |
||||
ins = file.getInputStream(); |
||||
toFile = new File(file.getOriginalFilename()); |
||||
inputStreamToFile(ins, toFile); |
||||
ins.close(); |
||||
} |
||||
return toFile; |
||||
} |
||||
public static void inputStreamToFile(InputStream ins,File file) { |
||||
try { |
||||
OutputStream os = new FileOutputStream(file); |
||||
int bytesRead = 0; |
||||
byte[] buffer = new byte[8192]; |
||||
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { |
||||
os.write(buffer, 0, bytesRead); |
||||
} |
||||
os.close(); |
||||
ins.close(); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
/*@RequestMapping("/downloadFile") |
||||
public String downloadFile() { |
||||
try { |
||||
InputStream inputStream = ftpUtils.downloadFile("skills.txt"); |
||||
FileUtil.writeFromStream(inputStream, "E:\\skills.txt"); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return "SUCCESS"; |
||||
}*/ |
||||
} |
||||
@ -0,0 +1,20 @@ |
||||
package com.hisense.hiatmp.server_api.utils; |
||||
|
||||
import java.util.UUID; |
||||
|
||||
public class CommonUtils { |
||||
public static Double bearing(Double lat1, Double lon1, Double lat2, Double lon2){ |
||||
double longitude1 = lon1; |
||||
double longitude2 = lon2; |
||||
double latitude1 = Math.toRadians(lat1); |
||||
double latitude2 = Math.toRadians(lat2); |
||||
double longDiff = Math.toRadians(longitude2-longitude1); |
||||
double y = Math.sin(longDiff)*Math.cos(latitude2); |
||||
double x = Math.cos(latitude1)*Math.sin(latitude2)-Math.sin(latitude1)*Math.cos(latitude2)*Math.cos(longDiff); |
||||
return (Math.toDegrees(Math.atan2(y, x))+360)%360; |
||||
} |
||||
public static String getUUID() { |
||||
String uuid = UUID.randomUUID().toString(); |
||||
return uuid.replaceAll("-", ""); |
||||
} |
||||
} |
||||
@ -0,0 +1,287 @@ |
||||
package com.hisense.hiatmp.server_api.utils; |
||||
|
||||
import cn.hutool.core.date.DateUtil; |
||||
import cn.hutool.core.io.FileUtil; |
||||
import lombok.Data; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.net.ftp.*; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.util.Calendar; |
||||
import java.util.Date; |
||||
|
||||
@Component |
||||
@Data |
||||
@Slf4j |
||||
@Service |
||||
public class FTPUtils { |
||||
|
||||
public static final int imageCutSize = 300; |
||||
|
||||
@Value("${spring.ftp.username}") //用户名
|
||||
private String userName; |
||||
|
||||
@Value("${spring.ftp.password}") //密码
|
||||
private String passWord; |
||||
|
||||
@Value("${spring.ftp.host}") //ip地址
|
||||
private String ip; |
||||
|
||||
@Value("${spring.ftp.port}") //端口号
|
||||
private int port; |
||||
|
||||
@Value("${spring.ftp.currentdir}") |
||||
private String CURRENT_DIR; // 文件存放的目录
|
||||
|
||||
@Value("${spring.ftp-http.url}") |
||||
private String ftpHttpUrl; |
||||
public static final String DIRSPLIT = "/"; |
||||
|
||||
|
||||
private String DOWNLOAD_DIR; |
||||
private FTPClient ftpClient = new FTPClient(); |
||||
//上传
|
||||
public String uploadToFtp(InputStream buffIn, String fileName, boolean needDelete) |
||||
throws FTPConnectionClosedException, IOException, Exception { |
||||
boolean returnValue = false; |
||||
String url =""; |
||||
try { |
||||
|
||||
// 建立连接
|
||||
connectToServer(); |
||||
// 设置传输二进制文件
|
||||
setFileType(FTP.BINARY_FILE_TYPE); |
||||
int reply = ftpClient.getReplyCode(); |
||||
if (!FTPReply.isPositiveCompletion(reply)) { |
||||
ftpClient.disconnect(); |
||||
throw new IOException("failed to connect to the FTP Server:" + ip); |
||||
} |
||||
/*ftpClient.enterLocalPassiveMode();*/ |
||||
//ftpClient.changeWorkingDirectory(CURRENT_DIR);
|
||||
String date = DateUtil.format(new Date(),"yyyyMMdd"); |
||||
//ftpClient.makeDirectory(CURRENT_DIR + "/" + date);
|
||||
//进入文件目录
|
||||
ftpClient.changeWorkingDirectory(CURRENT_DIR); |
||||
// 上传文件到ftp
|
||||
returnValue = ftpClient.storeFile(fileName, buffIn); |
||||
if (needDelete) { |
||||
ftpClient.deleteFile(fileName); |
||||
} |
||||
// 输出操作结果信息
|
||||
if (returnValue) { |
||||
System.out.println("uploadToFtp INFO: upload file to ftp : succeed!"); |
||||
} else { |
||||
System.out.println("uploadToFtp INFO: upload file to ftp : failed!"); |
||||
} |
||||
url = ftpHttpUrl + fileName; |
||||
buffIn.close(); |
||||
// 关闭连接
|
||||
closeConnect(); |
||||
} catch (FTPConnectionClosedException e) { |
||||
System.out.println("ftp连接被关闭!"); |
||||
throw e; |
||||
} catch (Exception e) { |
||||
returnValue = false; |
||||
System.out.println("ERR : upload file to ftp : failed! "); |
||||
throw e; |
||||
} finally { |
||||
try { |
||||
if (buffIn != null) { |
||||
buffIn.close(); |
||||
} |
||||
} catch (Exception e) { |
||||
System.out.println("ftp关闭输入流时失败!"); |
||||
} |
||||
if (ftpClient.isConnected()) { |
||||
closeConnect(); |
||||
} |
||||
} |
||||
return url; |
||||
} |
||||
|
||||
//上传
|
||||
public boolean uploadToFtp(String[] path, boolean needDelete) |
||||
throws FTPConnectionClosedException, IOException, Exception { |
||||
boolean returnValue = false; |
||||
|
||||
try { |
||||
|
||||
// 建立连接
|
||||
connectToServer(); |
||||
// 设置传输二进制文件
|
||||
setFileType(FTP.BINARY_FILE_TYPE); |
||||
int reply = ftpClient.getReplyCode(); |
||||
if (!FTPReply.isPositiveCompletion(reply)) { |
||||
ftpClient.disconnect(); |
||||
throw new IOException("failed to connect to the FTP Server:" + ip); |
||||
} |
||||
/*ftpClient.enterLocalPassiveMode();*/ |
||||
//进入文件目录
|
||||
ftpClient.changeWorkingDirectory(CURRENT_DIR); |
||||
for (String str : path){ |
||||
File file = new File(str); |
||||
InputStream inputStream = FileUtil.getInputStream(file); |
||||
// 上传文件到ftp
|
||||
String uuid = CommonUtils.getUUID(); |
||||
returnValue = ftpClient.storeFile(uuid,inputStream); |
||||
if (needDelete) { |
||||
ftpClient.deleteFile(uuid); |
||||
} |
||||
// 输出操作结果信息
|
||||
if (returnValue) { |
||||
System.out.println("uploadToFtp INFO: upload file to ftp : succeed!"); |
||||
} else { |
||||
System.out.println("uploadToFtp INFO: upload file to ftp : failed!"); |
||||
} |
||||
inputStream.close(); |
||||
} |
||||
|
||||
// 关闭连接
|
||||
closeConnect(); |
||||
} catch (FTPConnectionClosedException e) { |
||||
System.out.println("ftp连接被关闭!"); |
||||
throw e; |
||||
} catch (Exception e) { |
||||
returnValue = false; |
||||
System.out.println("ERR : upload file to ftp : failed! "); |
||||
throw e; |
||||
} finally { |
||||
if (ftpClient.isConnected()) { |
||||
closeConnect(); |
||||
} |
||||
} |
||||
return returnValue; |
||||
} |
||||
|
||||
//下载
|
||||
public InputStream downloadFile(String filename) |
||||
throws IOException { |
||||
InputStream in = null; |
||||
try { |
||||
|
||||
// 建立连接
|
||||
connectToServer(); |
||||
ftpClient.enterLocalPassiveMode(); |
||||
// 设置传输二进制文件
|
||||
setFileType(FTP.BINARY_FILE_TYPE); |
||||
int reply = ftpClient.getReplyCode(); |
||||
if (!FTPReply.isPositiveCompletion(reply)) { |
||||
ftpClient.disconnect(); |
||||
throw new IOException("failed to connect to the FTP Server:" + ip); |
||||
} |
||||
ftpClient.changeWorkingDirectory(CURRENT_DIR); |
||||
|
||||
// ftp文件获取文件
|
||||
in = ftpClient.retrieveFileStream(filename); |
||||
|
||||
} catch (FTPConnectionClosedException e) { |
||||
System.out.println("ftp连接被关闭!"); |
||||
throw e; |
||||
} catch (Exception e) { |
||||
System.out.println("ERR : upload file " + filename + " from ftp : failed!"); |
||||
|
||||
} |
||||
return in; |
||||
} |
||||
|
||||
private void setFileType(int fileType) { |
||||
try { |
||||
ftpClient.setFileType(fileType); |
||||
} catch (Exception e) { |
||||
System.out.println("ftp设置传输文件的类型时失败!"); |
||||
|
||||
} |
||||
} |
||||
|
||||
public void closeConnect() { |
||||
try { |
||||
if (ftpClient != null) { |
||||
ftpClient.logout(); |
||||
ftpClient.disconnect(); |
||||
} |
||||
} catch (Exception e) { |
||||
System.out.println("ftp连接关闭失败!"); |
||||
|
||||
} |
||||
} |
||||
|
||||
private void connectToServer() throws FTPConnectionClosedException, Exception { |
||||
if (!ftpClient.isConnected()) { |
||||
int reply; |
||||
try { |
||||
ftpClient = new FTPClient(); |
||||
ftpClient.setControlEncoding("UTF-8"); |
||||
ftpClient.enterLocalPassiveMode(); |
||||
ftpClient.connect(ip, port); |
||||
ftpClient.login(userName, passWord); |
||||
reply = ftpClient.getReplyCode(); |
||||
|
||||
if (!FTPReply.isPositiveCompletion(reply)) { |
||||
ftpClient.disconnect(); |
||||
System.out.println("connectToServer FTP server refused connection."); |
||||
|
||||
} |
||||
|
||||
} catch (FTPConnectionClosedException ex) { |
||||
System.out.println("没有连接数!there are too many connected users,please try later"); |
||||
|
||||
throw ex; |
||||
} catch (Exception e) { |
||||
System.out.println("登录ftp服务器失败"); |
||||
throw e; |
||||
} |
||||
} |
||||
} |
||||
|
||||
// Check the path is exist; exist return true, else false.
|
||||
public boolean existDirectory(String path) throws IOException { |
||||
boolean flag = false; |
||||
FTPFile[] ftpFileArr = ftpClient.listFiles(path); |
||||
for (FTPFile ftpFile : ftpFileArr) { |
||||
if (ftpFile.isDirectory() |
||||
&& ftpFile.getName().equalsIgnoreCase(path)) { |
||||
flag = true; |
||||
break; |
||||
} |
||||
} |
||||
return flag; |
||||
} |
||||
|
||||
public boolean createDirectory(String pathName) throws IOException { |
||||
boolean isSuccess = false; |
||||
try { |
||||
isSuccess = ftpClient.makeDirectory(pathName); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return isSuccess; |
||||
} |
||||
|
||||
public static String getExtention(String fileName) { |
||||
int pos = fileName.lastIndexOf("."); |
||||
return fileName.substring(pos); |
||||
} |
||||
|
||||
public static String getNoPointExtention(String fileName) { |
||||
int pos = fileName.lastIndexOf("."); |
||||
return fileName.substring(pos + 1); |
||||
} |
||||
|
||||
public static String getDateDir(Date dateParam) { |
||||
Calendar cal = Calendar.getInstance(); |
||||
if (null != dateParam) { |
||||
cal.setTime(dateParam); |
||||
} |
||||
int currentYear = cal.get(Calendar.YEAR); |
||||
int currentMouth = cal.get(Calendar.MONTH) + 1; |
||||
int currentDay = cal.get(Calendar.DAY_OF_MONTH); |
||||
//int currentHour = cal.get(Calendar.HOUR_OF_DAY);
|
||||
//return currentYear+FtpOperation.DIRSPLIT+currentMouth+FtpOperation.DIRSPLIT+currentDay+FtpOperation.DIRSPLIT+currentHour;
|
||||
return currentYear + FTPUtils.DIRSPLIT + currentMouth + FTPUtils.DIRSPLIT + currentDay; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue