<div class="iteye-blog-content-contain" style="font-size: 14px">
@SuppressWarnings("unused")
public Response deleteDoc(HttpServletRequest request, HttpServletResponse response) {
log.info("ServiceWebServiceImpl deleteDoc method start");
Map<String , Object> responseMap = new HashMap<String , Object>();
String docName = request.getParameter("docName");
String serviceid = request.getParameter("serviceid");
log.info("docName>>>>>>>=============="+docName);
log.info("serviceid>>>>>>>=============="+serviceid);
FTPClient ftpClient = new FTPClient();
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
conf.setServerLanguageCode("zh");
ftpClient.configure(conf);
boolean flag = false;
FileOutputStream fos = null;
try {
ftpClient.connect(Constants.FTP_HOST,Constants.FTP_PORT);
log.info("host :"+ Constants.FTP_HOST+" port :"+Constants.FTP_PORT);
// ftpClient.setControlEncoding("UTF-8");
ftpClient.login(Constants.FTP_USERNAME, "admin".equals(Constants.FTP_PASSWORD)?Constants.FTP_PASSWORD:"");
log.info("username :"+Constants.FTP_USERNAME+" passowrd:"+Constants.FTP_PASSWORD);
//检测连接是否成功
int reply = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.err.println("FTP server refused connection.");
log.info("FTP server refused connection.");
System.exit(1);
}
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.changeWorkingDirectory(Constants.SERVICE_DOC_DOWNLOAD_PATH);
log.info("directory :"+Constants.SERVICE_DOC_DOWNLOAD_PATH);
String remoteFileName = null;
try {
remoteFileName = java.net.URLDecoder.decode(docName, "utf-8");
log.info("remoteFileName==================="+remoteFileName);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
FTPFile[] ftpFiles = ftpClient.listFiles();
int length = ftpFiles.length;
int num =0;
log.info("length==================="+length);
if(ftpFiles!=null){
// flag = ftpClient.deleteFile(new String(docName.getBytes("ISO-8859-1"), "utf-8"));
for(FTPFile ff:ftpFiles){
log.info("FTPFile :"+new String(ff.getName().getBytes("ISO-8859-1"), "utf-8") +" ->"+remoteFileName);
String ftpName = new String(ff.getName().getBytes("ISO-8859-1"), "utf-8");
log.info("转码后文件名:"+ftpName);
if(ftpName.equals(remoteFileName)){
//设置删除权限
ftpClient.enterLocalPassiveMode();
flag = ftpClient.deleteFile(ff.getName());
num++;
log.info("flag===================="+flag);
ftpClient.logout();
}
}
}
if(num==0){
//删除数据库此文档
serviceDocManage.deleteServiceDocByID(Long.parseLong(serviceid));
responseMap.put("code", 0);
responseMap.put("message", "删除文件成功");
log.info("数据库====================删除文件成功");
}else if(flag){
//删除数据库此文档
serviceDocManage.deleteServiceDocByID(Long.parseLong(serviceid));
responseMap.put("code", 0);
responseMap.put("message", "删除文件成功");
log.info("数据库====================删除文件成功");
}else{
responseMap.put("code", 01);
responseMap.put("message", "删除文件失败");
log.info("数据库====================删除文件失败");
}
} catch (IOException e) {
log.error("FTP删除出错!", e);
flag = false;
responseMap.put("code", 1);
responseMap.put("message", "FTP删除出错!");
return Response.ok(responseMap, "application/json").build();
} finally {
try {
// fos.close();
ftpClient.disconnect();
} catch (IOException e) {
log.error("关闭FTP连接发生异常!", e);
responseMap.put("code", 2);
responseMap.put("message", "关闭FTP连接发生异常!");
return Response.ok(responseMap, "application/json").build();
}
}
return Response.ok(responseMap, "application/json").build();
}
</div>