方式一:file.delete()
public boolean delFileOne(String fileUrl){
boolean delete_flag = false;
if (!StringUtil.isEmpty(fileUrl)) {
File file = new File(fileUrl);
if (file.exists()) {
delete_flag = file.delete();
}else {
delete_flag = false;
}
return delete_flag;
}
方式二:linux命令
public boolean delFileTwo(String fileUrl){
boolean delete_flag = null;
URL url = new URL(fileUrl);
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
if(200==urlcon.getResponseCode()){//判断文件是否存在
InputStream doInfo = null;
InputStream errorInfo = null;
BufferedReader br = null;
Process process = null;
try {
String cmd = "删除命令";
process = Runtime.getRuntime().exec(cmd);
doInfo= process.getInputStream();
errorInfo= process.getErrorStream();
br = new BufferedReader(new InputStreamReader(doInfo));
while (doInfo.read() != -1) {
String result = br.readLine();
System.out.print("result==[" + result + "]");
}
br = new BufferedReader(new InputStreamReader(errorInfo));
while (errorInfo.read() != -1) {
String errorResult = br.readLine();
System.out.print("errorResult==[" + errorResult + "]");
}
HttpURLConnection urlconTwo = (HttpURLConnection) url.openConnection();
if(200==urlconTwo.getResponseCode()) {
delete_flag = false;
}
} catch (Throwable e) {
log.error("执行linux命令出错:" + e.getMessage());
delete_flag = false;
e.printStackTrace();
}finally {
br.close();
doInfo.close();
//process.waitFor();
process.destroy();
}
}else{
delete_flag = false;
}
return delete_flag
}