sftp上传文件java_Java 文件上传与SFTP上传

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值