@Override public void run() { try { ServerSocket server = new ServerSocket(listenPort); // 开始循环 while (true) { Socket socket = server.accept(); cs.submit(new HandleThread(socket)); try { UploadObject result = cs.take().get(); // 如何实现接收完文件后上传到服务器ftp,因为这是多线程,如果有回调方法可能比较好 try { FtpUploadFiles ftpUploadFiles = FtpUploadFiles.getInstance(); PathManage pathManage = FilePathConstants.pathMap.get(result.getFileType()); if (pathManage != null && !pathManage.getFileType().equals(FileType.TempDataType)) { String tpFtpDir = pathManage.getFtpPath(); if (result.getCreateDir() != null) { tpFtpDir += result.getCreateDir(); } ftpUploadFiles.uploadMmsFile(result.getPath(), result.getNewLocalFile(), tpFtpDir); } } catch (Exception e) { e.printStackTrace(); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } }
// 从流中读取内容并保存 private void readAndSave(InputStream is) throws IOException { uploadObject = new UploadObject(); String fileType = getFileType(is); System.out.println("fileType:" + fileType); String createDir = getCreateDir(is); String filename = getFileName(is); int file_len = readInteger(is); System.out.println("接收文件:" + filename + ",长度:" + file_len); // 本地路径 StringBuilder path = new StringBuilder(); PathManage pathManage = FilePathConstants.pathMap.get(fileType); if (pathManage != null) { path.append(pathManage.getLocalPath()); } if (!"null".equals(createDir)) { path.append(createDir + "/"); uploadObject.setCreateDir(createDir + "/"); } String filePath = path + filename; FileUtil.checkDirectory(path.toString()); readAndSave0(is, filePath, file_len, fileType, filePath); System.out.println("文件保存成功(" + file_len + "字节)。"); uploadObject.setFileType(fileType); uploadObject.setPath(path.toString()); uploadObject.setNewLocalFile(filename); System.out.println("set uploadObject" + uploadObject.toString()); }