socket接到请求,调用ftp上传

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;
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值