[英]File is copied on the FTP server not moved in java
使用下面的程序,我能夠在ftp服務器上上傳zip文件。 但是它會復制zip文件並將其上傳到ftp服務器上。 我希望它應該從本地系統刪除文件並將其復制到服務器,即它應該移動文件而不是復制。請指導
public class UploadFile {
public static void main(String args[])
{
FTPClient ftp=new FTPClient();
try {
int reply;
ftp.connect("ipadddress");
ftp.login("abc", "abc");
reply = ftp.getReplyCode();
System.out.println("reply1" + reply);
if(!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
}
System.out.println("FTP server connected.");
ftp.setFileType(FTP.BINARY_FILE_TYPE);
InputStream input= new FileInputStream("D:\\testencrypted.zip");
String dirTree="/Vel";
boolean dirExists = true;
String[] directories = dirTree.split("/");
for (String dir : directories )
{
if (!dir.isEmpty() )
{
if (dirExists)
{
dirExists = ftp.changeWorkingDirectory(dir);
}
else if (!dirExists)
{
System.out.println("dir tree" + ftp.printWorkingDirectory());
if (!ftp.makeDirectory(dir))
{
throw new IOException("Unable to create remote directory '" + dir + "'. error='" + ftp.getReplyString()+"'");
}
if (!ftp.changeWorkingDirectory(dir))
{
throw new IOException("Unable to change into newly created remote directory '" + dir + "'. error='" + ftp.getReplyString()+"'");
}
System.out.println("dir tree" + ftp.printWorkingDirectory());
}
}
}
ftp.storeFile(dirTree+"/t1.zip",input);
input.close();
ftp.logout();
}
catch(Exception e)
{
System.out.println("err"+ e);
}
finally
{
if(ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch(Exception ioe)
{
}
}
}
}
}
因此,完成上傳后(並確保上傳成功),只需使用File.delete()從本地磁盤中刪除文件即可。
File sourceFile = new File("D:\\testencrypted.zip");
InputStream input= new FileInputStream(sourceFile);
// Upload the file...
// Make sure you close the input stream first ;)
if (!sourceFile.delete()) {
System.out.println("Failed to delete " + sourceFile + " from local disk");
sourceFile.deleteOnExit(); // try and delete on JVM exit...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.