前提是有权限访问FTP,那么以下2种判断文件是否存在和删除文件的方法,哪种是正确的?或者都正确?其中1号都是我问了同学之后写的,有个老员工死活说我是错的,一定要用第二种,所以求助一下,是不是我错了?
判断文件是否存在:
1.
public boolean bLockExist()
{
File f = new File("/123.312.123.123/folder/abc.txt");
if(!file.exists())
{
return false;
}
return true;
}
2.
public boolean existFile(String filepath) {
TelnetInputStream fget = null;
try {
aftp.cd(this.rootDir);
fget = aftp.get(filepath);
return true;
} catch (Exception e) {
return false;
} finally {
try {
fget.close();
} catch (Exception e) {
}
}
}删除文件也是:
1.
public void DeleteLock(String remotePath)
{
String cmd= "dele "+remotePath+" Token.lock \r\n";
this.ftpClient.sendServer(cmd);
}
2.
public void deleteFile(String fileName) throws Exception {
try {
aftp.cd(this.rootDir); String[] dirList = fileName.split("/");
for (int i = 0; i < dirList.length - 1; i++) {
if (!dirList[i].equals("") && dirList[i] != null)
aftp.cd(dirList[i]);
}
aftp.sendServer("DELE " + dirList[dirList.length - 1] + "\r\n");
aftp.readServerResponse();
} catch (Exception e) {
throw new Exception("在ftp服务器上删除文件[" + fileName + "]失败" + e);
}
}
----------
顺带问一下,在FTP上创建文件是这样写吗?
创建文件:
public void CreateLock(String remotePath)
{
try
{
File file=new File(remotePath+"Token.lock");
if(!file.exists())
{
file.createNewFile();
}
}catch (IOException ex) {
System.out.println("Create Lock Error");
System.out.println(ex);
}
}
判断文件是否存在:
1.
public boolean bLockExist()
{
File f = new File("/123.312.123.123/folder/abc.txt");
if(!file.exists())
{
return false;
}
return true;
}
2.
public boolean existFile(String filepath) {
TelnetInputStream fget = null;
try {
aftp.cd(this.rootDir);
fget = aftp.get(filepath);
return true;
} catch (Exception e) {
return false;
} finally {
try {
fget.close();
} catch (Exception e) {
}
}
}删除文件也是:
1.
public void DeleteLock(String remotePath)
{
String cmd= "dele "+remotePath+" Token.lock \r\n";
this.ftpClient.sendServer(cmd);
}
2.
public void deleteFile(String fileName) throws Exception {
try {
aftp.cd(this.rootDir); String[] dirList = fileName.split("/");
for (int i = 0; i < dirList.length - 1; i++) {
if (!dirList[i].equals("") && dirList[i] != null)
aftp.cd(dirList[i]);
}
aftp.sendServer("DELE " + dirList[dirList.length - 1] + "\r\n");
aftp.readServerResponse();
} catch (Exception e) {
throw new Exception("在ftp服务器上删除文件[" + fileName + "]失败" + e);
}
}
----------
顺带问一下,在FTP上创建文件是这样写吗?
创建文件:
public void CreateLock(String remotePath)
{
try
{
File file=new File(remotePath+"Token.lock");
if(!file.exists())
{
file.createNewFile();
}
}catch (IOException ex) {
System.out.println("Create Lock Error");
System.out.println(ex);
}
}
解决方案 »
- ff js event undefined
- 怎样获取文本域的值?以便在其他.jsp页面显示
- WebService返回值的压缩问题
- dojox grid里的列 如何按照拼音排序啊
- oracle查询数据问题
- 救命啊~~~~javascript怎么弹出钱箱(j2eePOS前台)
- 100分求教!如何用自定义标签实现一个可编辑的下拉列表框?分不够再加!
- SOS:SOS:想开始学JSP,但是背景一片空白,大家教我啊~~~~~~~~~
- 在JSP中更新数据库的UPDATE怎么进行动态使用?
- 在jsp中如何实现“文件上传”和“浏览”功能???
- 如何将为Excel的数据加载到Oracle中?
- jspsmartupload 执行upload()方法报错
没测过,我是oracle数据库的,没有测试环境,但是昨天那个老员工还是给了我这个JAVA的任务,所以也没法验证呢……