Skip to content

Commit

Permalink
修改 com.feilong.net.filetransfer.AbstractFileTransfer.upload(FileInput…
Browse files Browse the repository at this point in the history
…Stream,

String) fix #735
优化 com.feilong.net.filetransfer.AbstractFileTransfer.uploadFile(String,
String, String) 使用 try-with-resources 关闭inputstream流写法  fix #734
  • Loading branch information
venusdrogon committed Jun 21, 2024
1 parent 68552f4 commit c56618d
Showing 1 changed file with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feilong.core.DefaultRuntimeException;
import com.feilong.core.Validate;
import com.feilong.core.util.MapUtil;
import com.feilong.io.FileUtil;
import com.feilong.io.FilenameUtil;
import com.feilong.io.IOUtil;
import com.feilong.io.entity.FileInfoEntity;
import com.feilong.json.JsonUtil;

Expand Down Expand Up @@ -342,10 +342,6 @@ protected boolean cd(String remoteDirectory){
/**
* 将指定<code>fileInputStream</code>上传到指定的文件<code>toFileName</code>.
*
* <p>
* 该方法不会关闭<code>fileInputStream</code>,请自行关闭
* </p>
*
* @param fileInputStream
* the file input stream
* @param toFileName
Expand Down Expand Up @@ -568,14 +564,22 @@ private boolean uploadDontClose(String localFileFullPath,String remoteDirectory)
private boolean uploadFile(String localFileFullPath,String remoteDirectory,String localFileName){
LOGGER.debug(log("begin put:[{}] to remoteDirectory:[{}]", localFileName, remoteDirectory));

FileInputStream fileInputStream = FileUtil.getFileInputStream(localFileFullPath);
//use try-with-resources
try (FileInputStream fileInputStream = FileUtil.getFileInputStream(localFileFullPath)){
boolean isSuccess = upload(fileInputStream, localFileName);

boolean isSuccess = upload(fileInputStream, localFileName);
logInfoOrError(isSuccess, "put [{}] to [{}] [{}]", localFileFullPath, remoteDirectory, toResultString(isSuccess));

logInfoOrError(isSuccess, "put [{}] to [{}] [{}]", localFileFullPath, remoteDirectory, toResultString(isSuccess));

IOUtil.closeQuietly(fileInputStream);
return isSuccess;
return isSuccess;
}catch (Exception e){
throw new DefaultRuntimeException(
log(
"localFileFullPath:[{}] to remoteDirectory:[{}] localFileName:[{}]",
localFileFullPath,
remoteDirectory,
localFileName),
e);
}
}

//---------------------------------------------------------------
Expand Down

0 comments on commit c56618d

Please sign in to comment.