import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import com.ytdinfo.core.util.DateUtilEx;
import com.ytdinfo.core.util.ParsProperFile;
import com.ytdinfo.core.util.Sftp;
import com.ytdinfo.enumeration.EnumFileUpload;
import com.ytdinfo.web.common.WebUtil;public classAttachmentUtil {/**
* 文件服务器url*/
private static final String prefix = ParsProperFile.getApplicationProp("nginx.server.file.url");/**
* 上传文件物理路径*/
private static final String uploadFile = ParsProperFile.getApplicationProp("uploadFile");/**
* 是否SFTP上传文件*/
private static final String isEnableSftpFile = ParsProperFile.getApplicationProp("is.enable.sftp.file");private static final String host = ParsProperFile.getApplicationProp("filesystem.sftp.host");private static final int port = Integer.valueOf(ParsProperFile.getApplicationProp("filesystem.sftp.port"));private static final int timeout = Integer.valueOf(ParsProperFile.getApplicationProp("filesystem.sftp.timeout"));private static final String username = ParsProperFile.getApplicationProp("filesystem.sftp.username");private static final String password = ParsProperFile.getApplicationProp("filesystem.sftp.password");/**
* 通过SFTP上传到文件服务系统
*
* @param rootPath
* @param subPath
* @param fileName
* @param multipartFile*/
private staticboolean uploadFileSystem(String rootPath, String subPath, String date, String fileName,
MultipartFile multipartFile) {
Sftp sftp= newSftp(host, port, timeout, username, password);
boolean isLogin=sftp.login();if(isLogin) {try{
String path= rootPath +subPath;if (!sftp.exist(uploadFile, subPath.replace("/", ""))) {
sftp.makeDir(path);
}
path= path + date + "/";if (!sftp.exist(uploadFile +subPath, date)) {
sftp.makeDir(path);
}returnsftp.uploadFile(path, fileName, multipartFile.getInputStream());
}catch(Exception e) {
e.printStackTrace();
}finally{
sftp.logout();
}
}return false;
}private staticboolean uploadFileSystem(String rootPath, String subPath, String date, String uuid, String fileName,
MultipartFile multipartFile) {
Sftp sftp= newSftp(host, port, timeout, username, password);
boolean isLogin=sftp.login();if(isLogin) {try{
String path= rootPath +subPath;if (!sftp.exist(uploadFile, subPath.replace("/", ""))) {
sftp.makeDir(path);
}
path= path + date + "/";if (!sftp.exist(uploadFile +subPath, date)) {
sftp.makeDir(path);
}
path= path + uuid + "/";if (!sftp.exist(uploadFile + subPath + date + "/", uuid)) {
sftp.makeDir(path);
}returnsftp.uploadFile(path, fileName, multipartFile.getInputStream());
}catch(Exception e) {
e.printStackTrace();
}finally{
sftp.logout();
}
}return false;
}/**
* 上传图片需要显示
*
* @param multipartFile
* @param request
* @param enumFile
* @return
* @throws Exception*/
public staticString uploadDisplayImage(MultipartFile multipartFile, HttpServletRequest request,
EnumFileUpload enumFile) throws Exception {
String imagePath= "";
String httpPath= "";
String date= DateUtilEx.formatDate(newDate(), DateUtilEx.yyyyMMdd);if (multipartFile != null && multipartFile.getSize() > 0) {
String imageName=multipartFile.getOriginalFilename();
String rootPath=uploadFile;
String path= rootPath + enumFile.getCode() +date;
String aimPath=path;
String fileName= WebUtil.uuid() + imageName.substring(imageName.lastIndexOf("."), imageName.length());
imagePath= path + "/" +fileName;
httpPath= prefix + enumFile.getCode() + date + "/" +fileName;//不启用文件服务器 说明直接保存到本地
if (isEnableSftpFile.equals("N")) {
File localFile= newFile(aimPath);if (!localFile.exists()) {//如果文件夹不存在,自动创建
localFile.mkdirs();
}
multipartFile.transferTo(newFile(imagePath));
}else{//启用文件服务器的话 就必须上传到文件服务器上面
if (!uploadFileSystem(uploadFile, enumFile.getCode(), date, fileName, multipartFile)) {return "";
}
}
}returnhttpPath;
}public static MapuploadAttachmentEx(MultipartFile multipartFile, HttpServletRequest request,
EnumFileUpload enumFile) throws Exception {
String filePath= "";
String fileName= "";
String fileType= "";
String httpPath= "";
String date= DateUtilEx.formatDate(newDate(), DateUtilEx.yyyyMMdd);
String uuid=WebUtil.uuid();if (multipartFile != null) {
fileName=multipartFile.getOriginalFilename();
fileType= fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
String rootPath=uploadFile;
String path= rootPath + enumFile.getCode() + date + "/" +uuid;
filePath= path + "/" +fileName;
httpPath= prefix + enumFile.getCode() + date + "/" + uuid + "/" +fileName;//不启用文件服务器 说明直接保存到本地
if (isEnableSftpFile.equals("N")) {
String aimPath=path;
File localFile= newFile(aimPath);if (!localFile.exists()) {//如果文件夹不存在,自动创建
localFile.mkdirs();
}
multipartFile.transferTo(newFile(filePath));
}else{//启用文件服务器的话 就必须上传到文件服务器上面
if (!uploadFileSystem(uploadFile, enumFile.getCode(), date, uuid, fileName, multipartFile)) {return null;
}
}
}
Map map = new HashMap();
map.put("filePath", filePath);
map.put("httpPath", httpPath);
map.put("fileName", fileName);
map.put("fileType", fileType);returnmap;
}
}