-
java实现自动上传文件到服务器上_Java实现上传文件到指定服务器指定目录
2021-03-19 10:19:58前言需求使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。具体实现引入的pomch.ethz.ganymedganymed-ssh...前言需求
使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。
具体实现
引入的pom
ch.ethz.ganymed
ganymed-ssh2
262
com.jcraft
jsch
0.1.55
建立实体类
public class ResultEntity {
private String code;
private String message;
private File file;
public ResultEntity(){}
public ResultEntity(String code,String message,File file) {
super();
this.code = code;
this.message = message;
this.file = file;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}
public class ScpConnectEntity {
private String userName;
private String passWord;
private String url;
private String targetPath;
public String getTargetPath() {
return targetPath;
}
public void setTargetPath(String targetPath) {
this.targetPath = targetPath;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
建立文件上传工具类
@Configuration
public class FileUploadUtil {
@Value("${remoteServer.url}")
private String url;
@Value("${remoteServer.password}")
private String passWord;
@Value("${remoteServer.username}")
private String userName;
@Async
public ResultEntity uploadFile(File file,String targetPath,String remoteFileName) throws Exception{
ScpConnectEntity scpConnectEntity=new ScpConnectEntity();
scpConnectEntity.setTargetPath(targetPath);
scpConnectEntity.setUrl(url);
scpConnectEntity.setPassWord(passWord);
scpConnectEntity.setUserName(userName);
String code = null;
String message = null;
try {
if (file == null || !file.exists()) {
throw new IllegalArgumentException("请确保上传文件不为空且存在!");
}
if(remoteFileName==null || "".equals(remoteFileName.trim())){
throw new IllegalArgumentException("远程服务器新建文件名不能为空!");
}
remoteUploadFile(scpConnectEntity,file,remoteFileName);
code = "ok";
message = remoteFileName;
} catch (IllegalArgumentException e) {
code = "Exception";
message = e.getMessage();
} catch (JSchException e) {
code = "Exception";
message = e.getMessage();
} catch (IOException e) {
code = "Exception";
message = e.getMessage();
} catch (Exception e) {
throw e;
} catch (Error e) {
code = "Error";
message = e.getMessage();
}
return new ResultEntity(code,message,null);
}
private void remoteUploadFile(ScpConnectEntity scpConnectEntity,File file,String remoteFileName) throws JSchException,IOException {
Connection connection = null;
ch.ethz.ssh2.Session session = null;
SCPOutputStream scpo = null;
FileInputStream fis = null;
try {
createDir(scpConnectEntity);
}catch (JSchException e) {
throw e;
}
try {
connection = new Connection(scpConnectEntity.getUrl());
connection.connect();
if(!connection.authenticateWithPassword(scpConnectEntity.getUserName(),scpConnectEntity.getPassWord())){
throw new RuntimeException("SSH连接服务器失败");
}
session = connection.openSession();
SCPClient scpClient = connection.createSCPClient();
scpo = scpClient.put(remoteFileName,file.length(),scpConnectEntity.getTargetPath(),"0666");
fis = new FileInputStream(file);
byte[] buf = new byte[1024];
int hasMore = fis.read(buf);
while(hasMore != -1){
scpo.write(buf);
hasMore = fis.read(buf);
}
} catch (IOException e) {
throw new IOException("SSH上传文件至服务器出错"+e.getMessage());
}finally {
if(null != fis){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != scpo){
try {
scpo.flush();
// scpo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != session){
session.close();
}
if(null != connection){
connection.close();
}
}
}
private boolean createDir(ScpConnectEntity scpConnectEntity ) throws JSchException {
JSch jsch = new JSch();
com.jcraft.jsch.Session sshSession = null;
Channel channel= null;
try {
sshSession = jsch.getSession(scpConnectEntity.getUserName(),scpConnectEntity.getUrl(),22);
sshSession.setPassword(scpConnectEntity.getPassWord());
sshSession.setConfig("StrictHostKeyChecking","no");
sshSession.connect();
channel = sshSession.openChannel("sftp");
channel.connect();
} catch (JSchException e) {
e.printStackTrace();
throw new JSchException("SFTP连接服务器失败"+e.getMessage());
}
ChannelSftp channelSftp=(ChannelSftp) channel;
if (isDirExist(scpConnectEntity.getTargetPath(),channelSftp)) {
channel.disconnect();
channelSftp.disconnect();
sshSession.disconnect();
return true;
}else {
String pathArry[] = scpConnectEntity.getTargetPath().split("/");
StringBuffer filePath=new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
try {
if (isDirExist(filePath.toString(),channelSftp)) {
channelSftp.cd(filePath.toString());
} else {
// 建立目录
channelSftp.mkdir(filePath.toString());
// 进入并设置为当前目录
channelSftp.cd(filePath.toString());
}
} catch (SftpException e) {
e.printStackTrace();
throw new JSchException("SFTP无法正常操作服务器"+e.getMessage());
}
}
}
channel.disconnect();
channelSftp.disconnect();
sshSession.disconnect();
return true;
}
private boolean isDirExist(String directory,ChannelSftp channelSftp) {
boolean isDirExistFlag = false;
try {
SftpATTRS sftpATTRS = channelSftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isDirExistFlag = false;
}
}
return isDirExistFlag;
}
}
属性我都写在Spring的配置文件里面了。将这个类托管给spring容器。
如果在普通类里面使用这个类,就需要看一下上篇博客了。哈哈。
总结
在我们使用的时候,主要调uploadFile这个方法即可。传递File文件,目标路径及文件名称。
-
java实现自动上传文件到服务器上_Java如何实现上传文件到服务器指定目录
2021-03-19 10:19:40Java如何实现上传文件到服务器指定目录发布于 2020-12-9|复制链接摘记: 前言需求使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很...Java如何实现上传文件到服务器指定目录
发布于 2020-12-9|
复制链接
摘记: 前言需求
使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。具体实现
引入的pom ..
前言需求使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。具体实现
引入的pom
```xml
ch.ethz.ganymed
ganymed-ssh2
262
com.jcraft
jsch
0.1.55
```
建立实体类
```java
public class ResultEntity {
private String code;
private String message;
private File file;
public ResultEntity(){}
public ResultEntity(String code, String message, File file) {
super();
this.code = code;
this.message = message;
this.file = file;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}
```
```java
public class ScpConnectEntity {
private String userName;
private String passWord;
private String url;
private String targetPath;
public String getTargetPath() {
return targetPath;
}
public void setTargetPath(String targetPath) {
this.targetPath = targetPath;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
```
建立文件上传工具类
@Configuration
```java
@Configuration
public class FileUploadUtil {
@Value("${remoteServer.url}")
private String url;
@Value("${remoteServer.password}")
private String passWord;
@Value("${remoteServer.username}")
private String userName;
@Async
public ResultEntity uploadFile(File file, String targetPath, String remoteFileName) throws Exception{
ScpConnectEntity scpConnectEntity=new ScpConnectEntity();
scpConnectEntity.setTargetPath(targetPath);
scpConnectEntity.setUrl(url);
scpConnectEntity.setPassWord(passWord);
scpConnectEntity.setUserName(userName);
String code = null;
String message = null;
try {
if (file == null || !file.exists()) {
throw new IllegalArgumentException("请确保上传文件不为空且存在!");
}
if(remoteFileName==null || "".equals(remoteFileName.trim())){
throw new IllegalArgumentException("远程服务器新建文件名不能为空!");
}
remoteUploadFile(scpConnectEntity, file, remoteFileName);
code = "ok";
message = remoteFileName;
} catch (IllegalArgumentException e) {
code = "Exception";
message = e.getMessage();
} catch (JSchException e) {
code = "Exception";
message = e.getMessage();
} catch (IOException e) {
code = "Exception";
message = e.getMessage();
} catch (Exception e) {
throw e;
} catch (Error e) {
code = "Error";
message = e.getMessage();
}
return new ResultEntity(code, message, null);
}
private void remoteUploadFile(ScpConnectEntity scpConnectEntity, File file,
String remoteFileName) throws JSchException, IOException {
Connection connection = null;
ch.ethz.ssh2.Session session = null;
SCPOutputStream scpo = null;
FileInputStream fis = null;
try {
createDir(scpConnectEntity);
}catch (JSchException e) {
throw e;
}
try {
connection = new Connection(scpConnectEntity.getUrl());
connection.connect();
if(!connection.authenticateWithPassword(scpConnectEntity.getUserName(),scpConnectEntity.getPassWord())){
throw new RuntimeException("SSH连接服务器失败");
}
session = connection.openSession();
SCPClient scpClient = connection.createSCPClient();
scpo = scpClient.put(remoteFileName, file.length(), scpConnectEntity.getTargetPath(), "0666");
fis = new FileInputStream(file);
byte[] buf = new byte[1024];
int hasMore = fis.read(buf);
while(hasMore != -1){
scpo.write(buf);
hasMore = fis.read(buf);
}
} catch (IOException e) {
throw new IOException("SSH上传文件至服务器出错"+e.getMessage());
}finally {
if(null != fis){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != scpo){
try {
scpo.flush();
// scpo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != session){
session.close();
}
if(null != connection){
connection.close();
}
}
}
private boolean createDir(ScpConnectEntity scpConnectEntity ) throws JSchException {
JSch jsch = new JSch();
com.jcraft.jsch.Session sshSession = null;
Channel channel= null;
try {
sshSession = jsch.getSession(scpConnectEntity.getUserName(), scpConnectEntity.getUrl(), 22);
sshSession.setPassword(scpConnectEntity.getPassWord());
sshSession.setConfig("StrictHostKeyChecking", "no");
sshSession.connect();
channel = sshSession.openChannel("sftp");
channel.connect();
} catch (JSchException e) {
e.printStackTrace();
throw new JSchException("SFTP连接服务器失败"+e.getMessage());
}
ChannelSftp channelSftp=(ChannelSftp) channel;
if (isDirExist(scpConnectEntity.getTargetPath(),channelSftp)) {
channel.disconnect();
channelSftp.disconnect();
sshSession.disconnect();
return true;
}else {
String pathArry[] = scpConnectEntity.getTargetPath().split("/");
StringBuffer filePath=new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
try {
if (isDirExist(filePath.toString(),channelSftp)) {
channelSftp.cd(filePath.toString());
} else {
// 建立目录
channelSftp.mkdir(filePath.toString());
// 进入并设置为当前目录
channelSftp.cd(filePath.toString());
}
} catch (SftpException e) {
e.printStackTrace();
throw new JSchException("SFTP无法正常操作服务器"+e.getMessage());
}
}
}
channel.disconnect();
channelSftp.disconnect();
sshSession.disconnect();
return true;
}
private boolean isDirExist(String directory,ChannelSftp channelSftp) {
boolean isDirExistFlag = false;
try {
SftpATTRS sftpATTRS = channelSftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isDirExistFlag = false;
}
}
return isDirExistFlag;
}
}
```
属性我都写在Spring的配置文件里面了。将这个类托管给spring容器。
-
java 上传文件到指定目录_Java实现上传文件到指定服务器指定目录
2021-02-12 23:58:36前言需求使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。具体实现引入的pomch.ethz.ganymedganymed-ssh...前言需求
使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。
具体实现
引入的pom
ch.ethz.ganymed
ganymed-ssh2
262
com.jcraft
jsch
0.1.55
建立实体类
public class ResultEntity {
private String code;
private String message;
private File file;
public ResultEntity(){}
public ResultEntity(String code, String message, File file) {
super();
this.code = code;
this.message = message;
this.file = file;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}
public class ScpConnectEntity {
private String userName;
private String passWord;
private String url;
private String targetPath;
public String getTargetPath() {
return targetPath;
}
public void setTargetPath(String targetPath) {
this.targetPath = targetPath;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
建立文件上传工具类
@Configuration
public class FileUploadUtil {
@Value("${remoteServer.url}")
private String url;
@Value("${remoteServer.password}")
private String passWord;
@Value("${remoteServer.username}")
private String userName;
@Async
public ResultEntity uploadFile(File file, String targetPath, String remoteFileName) throws Exception{
ScpConnectEntity scpConnectEntity=new ScpConnectEntity();
scpConnectEntity.setTargetPath(targetPath);
scpConnectEntity.setUrl(url);
scpConnectEntity.setPassWord(passWord);
scpConnectEntity.setUserName(userName);
String code = null;
String message = null;
try {
if (file == null || !file.exists()) {
throw new IllegalArgumentException("请确保上传文件不为空且存在!");
}
if(remoteFileName==null || "".equals(remoteFileName.trim())){
throw new IllegalArgumentException("远程服务器新建文件名不能为空!");
}
remoteUploadFile(scpConnectEntity, file, remoteFileName);
code = "ok";
message = remoteFileName;
} catch (IllegalArgumentException e) {
code = "Exception";
message = e.getMessage();
} catch (JSchException e) {
code = "Exception";
message = e.getMessage();
} catch (IOException e) {
code = "Exception";
message = e.getMessage();
} catch (Exception e) {
throw e;
} catch (Error e) {
code = "Error";
message = e.getMessage();
}
return new ResultEntity(code, message, null);
}
private void remoteUploadFile(ScpConnectEntity scpConnectEntity, File file,
String remoteFileName) throws JSchException, IOException {
Connection connection = null;
ch.ethz.ssh2.Session session = null;
SCPOutputStream scpo = null;
FileInputStream fis = null;
try {
createDir(scpConnectEntity);
}catch (JSchException e) {
throw e;
}
try {
connection = new Connection(scpConnectEntity.getUrl());
connection.connect();
if(!connection.authenticateWithPassword(scpConnectEntity.getUserName(),scpConnectEntity.getPassWord())){
throw new RuntimeException("SSH连接服务器失败");
}
session = connection.openSession();
SCPClient scpClient = connection.createSCPClient();
scpo = scpClient.put(remoteFileName, file.length(), scpConnectEntity.getTargetPath(), "0666");
fis = new FileInputStream(file);
byte[] buf = new byte[1024];
int hasMore = fis.read(buf);
while(hasMore != -1){
scpo.write(buf);
hasMore = fis.read(buf);
}
} catch (IOException e) {
throw new IOException("SSH上传文件至服务器出错"+e.getMessage());
}finally {
if(null != fis){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != scpo){
try {
scpo.flush();
// scpo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != session){
session.close();
}
if(null != connection){
connection.close();
}
}
}
private boolean createDir(ScpConnectEntity scpConnectEntity ) throws JSchException {
JSch jsch = new JSch();
com.jcraft.jsch.Session sshSession = null;
Channel channel= null;
try {
sshSession = jsch.getSession(scpConnectEntity.getUserName(), scpConnectEntity.getUrl(), 22);
sshSession.setPassword(scpConnectEntity.getPassWord());
sshSession.setConfig("StrictHostKeyChecking", "no");
sshSession.connect();
channel = sshSession.openChannel("sftp");
channel.connect();
} catch (JSchException e) {
e.printStackTrace();
throw new JSchException("SFTP连接服务器失败"+e.getMessage());
}
ChannelSftp channelSftp=(ChannelSftp) channel;
if (isDirExist(scpConnectEntity.getTargetPath(),channelSftp)) {
channel.disconnect();
channelSftp.disconnect();
sshSession.disconnect();
return true;
}else {
String pathArry[] = scpConnectEntity.getTargetPath().split("/");
StringBuffer filePath=new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
try {
if (isDirExist(filePath.toString(),channelSftp)) {
channelSftp.cd(filePath.toString());
} else {
// 建立目录
channelSftp.mkdir(filePath.toString());
// 进入并设置为当前目录
channelSftp.cd(filePath.toString());
}
} catch (SftpException e) {
e.printStackTrace();
throw new JSchException("SFTP无法正常操作服务器"+e.getMessage());
}
}
}
channel.disconnect();
channelSftp.disconnect();
sshSession.disconnect();
return true;
}
private boolean isDirExist(String directory,ChannelSftp channelSftp) {
boolean isDirExistFlag = false;
try {
SftpATTRS sftpATTRS = channelSftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isDirExistFlag = false;
}
}
return isDirExistFlag;
}
}
属性我都写在Spring的配置文件里面了。将这个类托管给spring容器。
如果在普通类里面使用这个类,就需要看一下上篇博客了。哈哈。
总结
在我们使用的时候,主要调uploadFile这个方法即可。传递File文件,目标路径及文件名称。
-
java附件上传到指定文件夹_Java实现上传文件到指定服务器指定目录
2021-03-08 09:10:46前言需求使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。具体实现引入的pomch.ethz.ganymedganymed-ssh...前言需求
使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。
具体实现
引入的pom
ch.ethz.ganymed
ganymed-ssh2
262
com.jcraft
jsch
0.1.55
建立实体类
public class ResultEntity {
private String code;
private String message;
private File file;
public ResultEntity(){}
public ResultEntity(String code,String message,File file) {
super();
this.code = code;
this.message = message;
this.file = file;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}
public class ScpConnectEntity {
private String userName;
private String passWord;
private String url;
private String targetPath;
public String getTargetPath() {
return targetPath;
}
public void setTargetPath(String targetPath) {
this.targetPath = targetPath;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
建立文件上传工具类
@Configuration
public class FileUploadUtil {
@Value("${remoteServer.url}")
private String url;
@Value("${remoteServer.password}")
private String passWord;
@Value("${remoteServer.username}")
private String userName;
@Async
public ResultEntity uploadFile(File file,String targetPath,String remoteFileName) throws Exception{
ScpConnectEntity scpConnectEntity=new ScpConnectEntity();
scpConnectEntity.setTargetPath(targetPath);
scpConnectEntity.setUrl(url);
scpConnectEntity.setPassWord(passWord);
scpConnectEntity.setUserName(userName);
String code = null;
String message = null;
try {
if (file == null || !file.exists()) {
throw new IllegalArgumentException("请确保上传文件不为空且存在!");
}
if(remoteFileName==null || "".equals(remoteFileName.trim())){
throw new IllegalArgumentException("远程服务器新建文件名不能为空!");
}
remoteUploadFile(scpConnectEntity,file,remoteFileName);
code = "ok";
message = remoteFileName;
} catch (IllegalArgumentException e) {
code = "Exception";
message = e.getMessage();
} catch (JSchException e) {
code = "Exception";
message = e.getMessage();
} catch (IOException e) {
code = "Exception";
message = e.getMessage();
} catch (Exception e) {
throw e;
} catch (Error e) {
code = "Error";
message = e.getMessage();
}
return new ResultEntity(code,message,null);
}
private void remoteUploadFile(ScpConnectEntity scpConnectEntity,File file,String remoteFileName) throws JSchException,IOException {
Connection connection = null;
ch.ethz.ssh2.Session session = null;
SCPOutputStream scpo = null;
FileInputStream fis = null;
try {
createDir(scpConnectEntity);
}catch (JSchException e) {
throw e;
}
try {
connection = new Connection(scpConnectEntity.getUrl());
connection.connect();
if(!connection.authenticateWithPassword(scpConnectEntity.getUserName(),scpConnectEntity.getPassWord())){
throw new RuntimeException("SSH连接服务器失败");
}
session = connection.openSession();
SCPClient scpClient = connection.createSCPClient();
scpo = scpClient.put(remoteFileName,file.length(),scpConnectEntity.getTargetPath(),"0666");
fis = new FileInputStream(file);
byte[] buf = new byte[1024];
int hasMore = fis.read(buf);
while(hasMore != -1){
scpo.write(buf);
hasMore = fis.read(buf);
}
} catch (IOException e) {
throw new IOException("SSH上传文件至服务器出错"+e.getMessage());
}finally {
if(null != fis){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != scpo){
try {
scpo.flush();
// scpo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != session){
session.close();
}
if(null != connection){
connection.close();
}
}
}
private boolean createDir(ScpConnectEntity scpConnectEntity ) throws JSchException {
JSch jsch = new JSch();
com.jcraft.jsch.Session sshSession = null;
Channel channel= null;
try {
sshSession = jsch.getSession(scpConnectEntity.getUserName(),scpConnectEntity.getUrl(),22);
sshSession.setPassword(scpConnectEntity.getPassWord());
sshSession.setConfig("StrictHostKeyChecking","no");
sshSession.connect();
channel = sshSession.openChannel("sftp");
channel.connect();
} catch (JSchException e) {
e.printStackTrace();
throw new JSchException("SFTP连接服务器失败"+e.getMessage());
}
ChannelSftp channelSftp=(ChannelSftp) channel;
if (isDirExist(scpConnectEntity.getTargetPath(),channelSftp)) {
channel.disconnect();
channelSftp.disconnect();
sshSession.disconnect();
return true;
}else {
String pathArry[] = scpConnectEntity.getTargetPath().split("/");
StringBuffer filePath=new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
try {
if (isDirExist(filePath.toString(),channelSftp)) {
channelSftp.cd(filePath.toString());
} else {
// 建立目录
channelSftp.mkdir(filePath.toString());
// 进入并设置为当前目录
channelSftp.cd(filePath.toString());
}
} catch (SftpException e) {
e.printStackTrace();
throw new JSchException("SFTP无法正常操作服务器"+e.getMessage());
}
}
}
channel.disconnect();
channelSftp.disconnect();
sshSession.disconnect();
return true;
}
private boolean isDirExist(String directory,ChannelSftp channelSftp) {
boolean isDirExistFlag = false;
try {
SftpATTRS sftpATTRS = channelSftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isDirExistFlag = false;
}
}
return isDirExistFlag;
}
}
属性我都写在Spring的配置文件里面了。将这个类托管给spring容器。
如果在普通类里面使用这个类,就需要看一下上篇博客了。哈哈。
总结
在我们使用的时候,主要调uploadFile这个方法即可。传递File文件,目标路径及文件名称。
相关文章
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250
-
java 文件上传目录_Java实现上传文件到服务器指定目录的方法
2021-03-05 23:41:29Java实现上传文件到服务器指定目录的方法发布时间:2020-08-03 10:49:31来源:亿速云阅读:74作者:小猪这篇文章主要讲解了Java实现上传文件到服务器指定目录的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下... -
java 文件上传目录_Java实现上传文件到指定服务器指定目录
2021-03-04 01:42:11前言需求使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。具体实现引入的pomch.ethz.ganymedganymed-ssh... -
Java实现上传文件到指定服务器指定目录
2020-07-03 08:46:19使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。 具体实现 引入的pom <dependency> <groupId&... -
java如何上传文件到指定目录_Java如何实现上传文件到服务器指定目录
2021-03-22 17:49:37前言需求使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。具体实现引入的pomch.ethz.ganymedganymed-ssh... -
java 上传文件到别的目录_Java如何实现上传文件到服务器指定目录
2021-03-09 01:45:49前言需求使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。具体实现引入的pomch.ethz.ganymedganymed-ssh... -
java jsch isdir_Java实现上传文件到指定服务器指定目录
2021-03-08 15:31:59前言需求使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。具体实现引入的pomch.ethz.ganymedganymed-ssh... -
java上传文件服务器_Java如何实现上传文件到服务器指定目录
2021-02-12 17:19:03前言需求使用freemarker生成的静态文件,统一存储在某个服务器上。本来一开始打算使用ftp实现的,奈何老连接不上,改用jsch。毕竟有现成的就很舒服,在此介绍给大家。具体实现引入的pomch.ethz.ganymedganymed-ssh... -
java文件上传流程_Java上传文件到服务器指定文件夹实现过程图解
2021-02-28 09:17:10如何分,利用强大的js库,来减轻我们的工作,市场上已经能有关于大文件分块的轮子,虽然程序员的天性曾迫使我重新造轮子。但是因为时间的关系还有工作的关系,我只能罢休了。最后我选择了百度的WebUploader来实现... -
java上传图片到服务器某个文件夹下面_java上传文件到服务器指定文件夹问题
2021-02-26 14:48:56如何分,利用强大的js库,来减轻我们的工作,市场上已经能有关于大文件分块的轮子,虽然程序员的天性曾迫使我重新造轮子。但是因为时间的关系还有工作的关系,我只能罢休了。最后我选择了百度的WebUploader... -
java上传文件到服务器指定文件夹问题
2020-07-20 10:32:21如何分,利用强大的js库,来减轻我们的工作,市场上已经能有关于大文件分块的轮子,虽然程序员的天性曾迫使我重新造轮子。但是因为时间的关系还有工作的关系,我只能罢休了。最后我选择了百度的WebUploader来实现... -
java samba_用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器...
2021-02-28 08:22:11实践:对samba服务器上的操作无外乎2种:一种是从Samba服务器上下载某文件到本地目录,一种是上传本地文件到Samba服务器上指定目录,为此我们写了一个工具类,来完成这两件事情。package com.ch... -
用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器...
2017-01-04 14:26:00引入: 在我们昨天架设好了...对samba服务器上的操作无外乎2种:一种是从Samba服务器上下载某文件到本地目录,一种是上传本地文件到Samba服务器上指定目录,为此我们写了一个工具类,来完成这两件事情。 package ... -
java如何上传文件到服务器指定位置_Java 的单个 Spring boot 项目快速瘦身到几十k至1M以下...
2020-12-18 09:16:04开篇最近公司开发的微服务项目多了,开发出来的项目各个百十来M,而且是用的云服务器,上传云服务器的时候上传半天。恨得我压根直痒痒!所以只好看看有没有使jar包瘦身的方法。结果一搜还真得挺多!今天就拿一个网上... -
<实战> 用java 代码下载Samba服务器上的文件到本地目录以及上传本地文件到Samba服务器...
2017-11-16 19:20:00引入: 在我们昨天架设好了Samba服务器上并且创建了一个 Samba 账户...对samba服务器上的操作无外乎2种:一种是从Samba服务器上下载某文件到本地目录,一种是上传本地文件到Samba服务器上指定目录,为此我们写了一个... -
JAVA利用FTPClient上传到阿里云centos服务器遇到的各种坑
2019-08-12 03:07:02说明 如果你是在利用java的FTPClient上传...楼主最近在和别人搞项目,负责的是将上传的图片资源利用FTPClient上传到Nginx服务器的指定位置,刚开始,根据教程在centos上下载FTP,配置完用户后,能正常上传文件到linux上... -
linux centos8 将指定本地目录下的文件上传到阿里云OSS上
2020-08-12 20:12:16使用场景:为了解决高并发问题,尤其带宽限制(阿里云最高200M,当前首页一个用户请求4M的资源,导致带宽跟不上),jenkins打包vue下的前端静态资源上传到阿里云OSS上 前提工作:因工具是java开发,所以提前在ECS... -
Java上传图片到服务器路径获取系列之--图片保存到Tomcat的webapps目录下
2017-08-01 20:27:46上一篇介绍了Java web项目中把客户端上传的图片保存到服务器中web工程下的指定文件夹里,但是这样存在一个弊端:因为保存图片的文件夹在项目工程文件下,所以,当项目文件更新版本的时候,就会覆盖掉原来保存图片的... -
java 文件上传漏洞_Web安全 - 文件上传漏洞
2021-02-28 15:25:56文件上传漏洞就是利用网页代码中的文件上传路径变量过滤不严将可执行的文件上传到一个到服务器中,再通过URL去访问以执行恶意代码。二、检测与绕过客户端检测(Javascript检测)在网页上写一段Javascript脚本,校验... -
java保存图片到tomcat_Java上传图片到服务器路径获取系列之--图片保存到Tomcat的webapps目录下...
2021-03-03 12:20:58上一篇介绍了Java web项目中把客户端上传的图片保存到服务器中web工程下的指定文件夹里,但是这样存在一个弊端:因为保存图片的文件夹在项目工程文件下,所以,当项目文件更新版本的时候,就会覆盖掉原来保存图片的... -
java使用JSch包进行上传图片到远程服务器
2018-10-31 15:09:37在最近一个项目上接触到一个上传图片的功能,主要是前段选择图片,点击上传,后台接收到图片进行连接到远程的服务器进行上传到指定的路径,中间使用了JSch包进行图片的上传以及连接服务器。遇到一个Kerberos口令...