File target = new File(FileLocation); Thread t2=new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { new FTPServer().start(target,UUID); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } },"传输线程启动"); t2.start();
package download; import sun.management.ThreadInfoCompositeData; import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.sql.*; import java.util.UUID; import java.util.logging.Logger; public class FTPServer { public static final String URL = "jdbc:derby://localhost:1527/db;create=false"; public static final String USER = "test"; public static final String PASSWORD = "test"; protected static Logger LOG=Logger.getLogger("FTPServer"); public static String getfileLocation(String UUID){ //2. 获得数据库连接 Connection conn = null; try { conn = DriverManager.getConnection(URL, USER, PASSWORD); } catch (SQLException e) { e.printStackTrace(); } //3.操作数据库,实现增删改查 Statement stmt = null; try { stmt = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } String sql="select q.location,q.file_name,q.file_type from qys q where uuid=\'"+UUID+"\'"; System.out.println(sql); String FileLocation=null; String FileName=null; String FileType=null; ResultSet set=null; try { set=stmt.executeQuery(sql); while(set.next()){ FileLocation=set.getString("location"); FileName=set.getString("file_name"); FileType=set.getString("file_type"); System.out.println(FileLocation+"\\"+FileName+FileType); } } catch (SQLException e) { e.printStackTrace(); }finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } try { stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } return FileLocation+"\\"+UUID+"."+FileType; } public static Boolean deletefile(String UUID){ String location=getfileLocation(UUID); // System.out.println("delete file locatin"+location); File file = new File(location); if (file.isFile() && file.exists()) { // System.out.println("sahnchu wenjain "); file.delete(); return true; } return false; } public void start(File target,String UUID) throws IOException { //创建一个服务器 ServerSocket ss = new ServerSocket(5678); //开始监听 Socket s = ss.accept(); LOG.info("开始监听"); //获取基于socket的输出流 PrintStream ps = new PrintStream(s.getOutputStream()); //获取目标文件的输入流 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(target)); LOG.info("开始传输..."); byte[] b = new byte[1024]; int len = 0; while((len = bis.read(b)) != -1){ ps.write(b, 0, len); } LOG.info("传输完成!"); bis.close(); ps.close(); ss.close(); Thread t1=new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub deletefile(UUID); } },"删除线程"); t1.start(); LOG.info("删除源文件!"); } }