package com.sam.task; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSchException; import com.sam.mongodb.MongoDbUtil; import com.sam.officechange.ConvertToPdf; import com.sam.officechange.Pdf2Img; import com.sam.tool.FileUtil; import com.sam.tool.FtpUtils; import com.sam.tool.SFTPChannel; import com.sam.tool.StrUtils; import org.nutz.json.Json; import org.nutz.json.JsonFormat; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.*; /** * 網絡通知的轉換監聽 */ public class WebNoticeTask { public static String FTP_IP = null; public static String FTP_PORT = null; public static String FTP_PASSWORD = null; public static String FTP_USERNAME = null; public static String SOCKET_PORT = null; static{ FileInputStream in = null; try{ Properties properties = new Properties(); in = new FileInputStream(System.getProperty("user.dir")+"/src/base.properties"); properties.load(in); FTP_IP = properties.getProperty("FTP_IP"); FTP_PORT = properties.getProperty("FTP_PORT"); FTP_PASSWORD = properties.getProperty("FTP_PASSWORD"); FTP_USERNAME = properties.getProperty("FTP_USERNAME"); SOCKET_PORT = properties.getProperty("SOCKET_PORT"); System.out.println("讀取配置信息成功!"); }catch(Exception e){ e.printStackTrace(); System.out.println("讀取配置信息失敗!"); }finally{ if(in != null){ try{ in.close(); }catch(Exception e){ e.printStackTrace(); } } } } public static void main(String[] args) throws Exception { ServerSocket ss = new ServerSocket(Integer.valueOf(SOCKET_PORT)); while (true){ System.out.println("服務器啓動,等待連接中...."); Socket s = ss.accept(); Runnable runnable = () -> { Map message2Client = new HashMap(); try { System.out.println("客戶端:"+s.getInetAddress().getLocalHost()+"已連接到服務器"); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(),"UTF-8")); //讀取客戶端發送來的消息 String mess = br.readLine(); System.out.println("客戶端:"+mess); List<Map<String,String>> filelist = (List<Map<String, String>>)Json.fromJson(mess); String floderName = String.valueOf(System.currentTimeMillis()); //根據文件名創建文件 FileUtil.createDir(System.getProperty("user.dir") + "/webNotice/"+floderName); String docId = ""; List<String> filePathList = new ArrayList<>(); String mainName = "";//正文名字 for(Map fileMap : filelist){ docId = "".equals(fileMap.get("mainid").toString())||null==fileMap.get("mainid").toString()?docId:fileMap.get("mainid").toString(); String filename = fileMap.get("name").toString(); String mongoId = fileMap.get("mongoid").toString(); File file = new File(System.getProperty("user.dir") + "/webNotice/"+floderName+"/"+filename); MongoDbUtil.downloadFile(mongoId,file); if("1".equals(fileMap.get("type").toString())){//正文轉換 //發起轉換 fileToImg(file, System.getProperty("user.dir") + "/webNotice/" + floderName); mainName = StrUtils.deleteFileSufix(file.getName()); }else if("2".equals(fileMap.get("type").toString())){ } filePathList.add(System.getProperty("user.dir") + "/webNotice/" + floderName + "/" + filename); } List<String> ImgfileNameList = FileUtil.getFileName(System.getProperty("user.dir") + "/webNotice/" + floderName+"/"+mainName); filePathList.addAll(ImgfileNameList); //轉換完成,上傳ftp服務器 FtpUtils ftp = null; try { //連接ftp ftp = new FtpUtils(FTP_IP,Integer.valueOf(FTP_PORT),FTP_USERNAME,FTP_PASSWORD); if(true==ftp.open()){ ftp.mkDir(docId);//創建目錄 //上傳 for(String filepath : filePathList){ ftp.changeDir("/" + docId); ftp.upload(filepath); } ftp.close(); } } catch (Exception e) { e.printStackTrace(); throw e; }finally { if(ftp!=null){ ftp.close(); } } message2Client.put("result","true"); } catch (IOException e) { e.printStackTrace(); message2Client.put("result", "false"); }finally { try{ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(),"UTF-8")); String rs = Json.toJson(message2Client, JsonFormat.compact()); System.err.println("rs:"+rs); bw.write(rs+"\n"); bw.flush(); }catch (Exception e){ e.printStackTrace(); } } }; new Thread(runnable).start(); } } /** * 將文件轉換爲pdf後轉換爲圖片 * @param file * @return */ public static boolean fileToImg(File file,String floder){ String pdfName = StrUtils.nameToPdf(file.getName()); if(!"pdf".equals(StrUtils.getFileSufix(file.getName()))){ ConvertToPdf d = new ConvertToPdf(); d.convert2PDF(file.getPath(),floder+"/"+pdfName); file = new File(floder + "/" + pdfName); } try { Pdf2Img test = new Pdf2Img("jpg", file.getAbsolutePath(),StrUtils.deleteFileSufix(file.getName())); } catch (Exception e) { e.printStackTrace(); } return true; } }
socket接到請求,調用ftp上傳
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.