电子公文传输系统-个人贡献

发布时间 2023-12-17 11:18:34作者: 20211424罗杰逊

个人完成的工作

博客的撰写





后端程序代码的编写

PutinStorage.java

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.util.Vector;


import javax.swing.JOptionPane;


public class PutinStorage {

    // 得到数据库表数据

    public static Vector getRows() {

        String sql_url = "jdbc:mysql://127.0.0.1:3307/myfiletra?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8";    //数据库路径(一般都是这样写),test是数据库名称
        String name = "root";        //用户名
        String password = "123456";    //密码
        Connection conn;
        PreparedStatement preparedStatement = null;
        Vector rows = null;
        Vector columnHeads = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");    //连接驱动
            conn = DriverManager.getConnection(sql_url, name, password);    //连接数据库
            preparedStatement = conn.prepareStatement("select filename,filesize from file");
            ResultSet result1 = preparedStatement.executeQuery();
            rows = new Vector();
            ResultSetMetaData rsmd = result1.getMetaData();
            while (result1.next()) {
                rows.addElement(getNextRow(result1, rsmd));
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println("FAILED DRIVEN MYSQL");
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("FAILED FAILED MYSQL");
            e.printStackTrace();
        }
        return rows;
    }

    // 得到数据库表头
    public static Vector getHead() {
        String sql_url = "jdbc:mysql://127.0.0.1:3307/myfiletra?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2B8";    //数据库路径(一般都是这样写),test是数据库名称
        String name = "root";        //用户名
        String password = "123456";    //密码
        Connection conn;
        PreparedStatement preparedStatement = null;
        Vector rows = null;
        Vector columnHeads = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");        //连接驱动
            conn = DriverManager.getConnection(sql_url, name, password);    //连接数据库
            preparedStatement = conn.prepareStatement("select distinct filename,filesize from file");
            ResultSet result1 = preparedStatement.executeQuery();
            boolean moreRecords = result1.next();
            if (!moreRecords)
                JOptionPane.showMessageDialog(null, "NO RESULT");
            columnHeads = new Vector();
            ResultSetMetaData rsmd = result1.getMetaData();
            for (int i = 1; i <= rsmd.getColumnCount(); i++)
                columnHeads.addElement(rsmd.getColumnName(i));
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println("FAILED DRIVEN MYSQL");
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println("FAILED OPEN MYSQL");
            e.printStackTrace();
        }
        return columnHeads;
    }

    // 得到数据库中下一行数据
    private static Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd) throws SQLException {
        Vector currentRow = new Vector();
        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
            currentRow.addElement(rs.getString(i));
        }
        return currentRow;
    }

}

FileServer.java

import javax.xml.crypto.Data;
import java.io.*;

import java.net.*;


public class FileServer extends Thread{

	private static Socket s;
	private static ServerSocket serverSocket;
	private File source = new File("C:\\Users\\14914\\Desktop\\test\\moren55.txt");//服务端显示文件的存放位置
	static ServerSocket server;
	//6788端口负责向客户端发送文件
	static {
		try {
			server = new ServerSocket(6788);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public FileServer(Socket s, File source) throws IOException {
		super();
		this.s = s;
		this.source = source;
	}

	/**
	 * 获取想要客户端下载的目标文件的文件名
	 * @param s
	 * @return
	 * @throws IOException
	 */
	public static String getfilepath(Socket s) throws IOException {
		String name = null;
		InputStream is = null;
		try {
			//读取文件名
			is = s.getInputStream();
			int len1;
			byte[] bs = new byte[1024];
			len1 = is.read(bs);
			name = new String(bs, 0, len1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return name;
	}

	@Override
	public void run() {
		DataInputStream bis = null;
		DataOutputStream bos = null;
			try {
//				//获取源文件的输入流
				System.out.println(source.getAbsolutePath());
				bis = new DataInputStream(new FileInputStream(source));
				//获取socket的输出流并包装
				Socket s = server.accept();
				bos = new DataOutputStream(s.getOutputStream());
				byte[] b = new byte[1027 * 9];
				int len = 0;
				System.out.println("向 " + s.getInetAddress().getHostAddress() + "开始传输....");
				while ((len = bis.read(b)) != -1) {
					bos.write(b, 0, len);
				}
				System.out.println("向 " + s.getInetAddress().getHostAddress() + "传输完成!");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (bos != null) bos.flush();bos.close();
					if (bis != null) bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	}

	public void netStartServer() throws IOException {
		System.out.println("SOFEEM文件服务器已启动,等待连接...");
		//循环监听是否有客户端上传到服务器文件
		new FileTransferServer().start();
		while(true){
			File source = new File(new FileTree().serverfilename);
			Socket s = server.accept();
			System.out.println(s.getInetAddress().getHostAddress()+"进入服务器,准备传输...");
			source = new File(getfilepath(s));
			//根据每一个连接的客户端启动一条子线程
			new FileServer(s, source).start();
		}
	}

	public  static void main(String[] args) throws IOException {
		System.out.println("SOFEEM文件服务器已启动,等待连接...");
		//循环监听是否有客户端上传到服务器文件
		new FileTransferServer().start();
		while(true){
			File source = new File(new FileTree().serverfilename);
			Socket s = server.accept();
			System.out.println(s.getInetAddress().getHostAddress()+"进入服务器,准备传输...");
			source = new File(getfilepath(s));
			//根据每一个连接的客户端启动一条子线程
			new FileServer(s, source).start();
		}
	}

}

FileTransferClient.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * 从本地读取文件,然后向服务器上传文件
 *
 */
public class FileTransferClient {
    private String host = "127.0.0.1";
    private int port =6787;
    private int port1 =6788;
    private Socket clientSocket;

    /**
     * 对应6787端口的构造器:完成上传文件功能
     * @throws IOException
     */
    public FileTransferClient() throws IOException {
    	clientSocket = new Socket(host, port);
    }

    /**
     * 对应6788端口的构造器:完成下载文件功能
     * @param temp
     * @throws IOException
     */
    public FileTransferClient(String temp) throws IOException {
        clientSocket = new Socket(host, port1);
    }

    /**
     * 向服务器端发送想要下载的目标文件的文件名
     * @param sfilePath
     */
    void sendM(String sfilePath){
        DataOutputStream sdos = null;
        try {
            System.out.println(sfilePath);
            sdos = new DataOutputStream(clientSocket.getOutputStream());
            sdos.write(sfilePath.getBytes());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(sdos != null)
                try {
                    sdos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }

    /**
     * 向服务器端上传文件
     * @param sfilePath:对应本地文件路径
     */
    void sendFile(String sfilePath) {


                try {
                    File sfile = new File(sfilePath);
                    System.out.println(sfilePath);
                    DataInputStream sdis = new DataInputStream(new FileInputStream(sfilePath));
                    DataOutputStream sdos = new DataOutputStream(clientSocket.getOutputStream());
                    byte[] sbuf = new byte[1024 * 9];
                    int slen = 0;
                    sdos.write(sfile.getName().getBytes());
                    while ((slen = sdis.read(sbuf)) != -1) {
                        sdos.write(sbuf, 0, slen);
                    }
                    sdos.flush();
                    sdis.close();
                    sdos.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
}

上传代码至gitee

代码链接

分配一些工作

小组代码总行数

  • 2500行

我贡献的代码行数

  • 356行

小组总文档数

  • 12

我贡献的文档数

  • 2

链接

团队作业(四):描述设计
团队作业(五):冲刺总结5