// 从服务器上获取模板正文信息 public static String getModualContent(String modualid, String ftpurl) { String modualcontent = ""; // 上传文件到ftp FtpUtil ftp = new FtpUtil(); String serverpath = PropertyHelper.getWebAppPath() + "ueditor" + File.separator + modualid + ".txt"; try { ftp.connectServer( PropertyHelper.getProperty("ueditorftpip"), PropertyHelper.getProperty("ueditorftpuser"), PropertyHelper.getProperty("ueditorftppassword"), PropertyHelper.getProperty("ueditorftppath")); ftp.download(ftpurl, serverpath); modualcontent = readTxtFile(serverpath); return modualcontent; } catch (Exception e) { e.printStackTrace(); return modualcontent; } finally { ftp.closeServer(); // 删除本地原文件 deleteFile(serverpath); } }
// 存储到服务器绝对路径 public static void saveModualContent( String modualcontent, String modualid, String serverpath, String ftpurl) { OutputStreamWriter fwriter = null; File modual = new File(serverpath); Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String dateNowStr = sdf.format(d); try { if (!modual.exists()) // 如果文件不存在,则新建. { File parentDir = new File(modual.getParent()); if (!parentDir.exists()) // 如果所在目录不存在,则新建. { parentDir.mkdirs(); } modual.createNewFile(); } fwriter = new OutputStreamWriter(new FileOutputStream(serverpath, true), "UTF-8"); fwriter.write(modualcontent); fwriter.flush(); } catch (IOException ex) { ex.printStackTrace(); } finally { if (null != fwriter) { try { fwriter.close(); // 上传文件到ftp FtpUtil ftp = new FtpUtil(); try { ftp.connectServer( PropertyHelper.getProperty("ueditorftpip"), PropertyHelper.getProperty("ueditorftpuser"), PropertyHelper.getProperty("ueditorftppassword"), PropertyHelper.getProperty("ueditorftppath")); String ext = modual.getAbsolutePath().substring(modual.getAbsolutePath().lastIndexOf(".")); if (".txt".equals(ext)) { ftp.upload(serverpath, ftpurl, modualid + ".txt"); } else if (".html".equals(ext)) { ftp.upload(serverpath, ftpurl, modualid + ".html"); } } catch (Exception e) { e.printStackTrace(); } finally { ftp.closeServer(); // 删除本地原文件 deleteFile(serverpath); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }