首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Java文件上传管理器 控制台

上传文件下载管理器项目技术

JDBC + IO + Socket

实现功能

客户端注册:通过输入用户名,密码,确认密码并且校验完成以后将用户信息储存在数据库中。

客户端登录:通过输入用户名和密码到数据库中校验,校验完成进入文件上传下载管理器。

上传文件:通过输入上传文件的路径上传到数据库,支持多个文件上传。

查看文件:通过登录的用户名查找上传的文件。

下载文件:通过输入文件的编号和下载的路径,从数据库进行下载。

删除文件:通过输入文件的编号进行单个文件的删除。

(PS:这次花了一天才写完,不过收获也很多,不足之处,多多交流)

完整代码

undefined

User.java

包com.xk; import java.io.Serializable; @SuppressWarnings(“serial”) public class User implements Serializable { private String username; 私人字符串密码; public String getUsername(){ return username; } public void setUsername(String username){ this.username = username; } public String getPassword(){ return password; } public void setPassword(String password){ this.password = password; } }

未定义

FileEntity.java

包com.xk; import java.io.Serializable; @SuppressWarnings(“serial”) public class FileEntity implements Serializable { private String username; private String fileName; 私有int id private byte [] fileContent; public FileEntity(int id,String fileName){ this.id = id; this.fileName = fileName; } public FileEntity(){ } public int getId(){ return id; } public void setId(int id){ this.id = id; } public String getUsername(){ return username; } public void setUsername(String username){ this.username = username; } public String getFileName(){ return fileName; } public void setFileName(String fileName){ this.fileName = fileName; } public byte [] getFileContent(){ return fileContent; } public void setFileContent(byte [] fileContent){ this.fileContent = fileContent; } @Override public String toString(){ return id +“\ t”+ fileName; } }

未定义

CommandRelay.java

包com.xk; import java.io.Serializable; import java.util.ArrayList; @SuppressWarnings(“serial”) public class CommandRelay实现Serializable { private String command; 私有布尔标志 private Object userObject = null; private Object fileObject = null; private byte [] bytes = null; private ArrayList fileList = null; public String getCommand(){ return command; } public void setCommand(String command){ this.command = command; } public Object getUserObject(){ return userObject; } public void setUserObject(Object userObject){ this.userObject = userObject; } public Object getFileObject(){ return fileObject; } public void setFileObject(Object fileObject){ this.fileObject = fileObject; } public boolean isFlag(){ return flag; } public void setFlag(boolean flag){ this.flag = flag; } public ArrayList getFileList(){ return fileList; } public void setFileList(ArrayList fileList){ this.fileList = fileList; } public byte [] getBytes(){ return bytes; } public void setBytes(byte [] bytes){ this.bytes = bytes; } }

未定义

DBUtil.java

包com.xk; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtil { private static final String DRIVER =“com.mysql.jdbc.Driver”; private static final String URL =“jdbc:mysql:// localhost:3306 / FileUpload”; private static final String NAME =“root”; private static final String PASSWORD =“root”; / * *数据库连接 * / public连接getConnection(){ try { Class.forName(DRIVER); Connection conn = DriverManager.getConnection(URL,NAME,PASSWORD); 返回conn } catch(ClassNotFoundException e){ e.printStackTrace(); 返回null; } catch(SQLException e){ e.printStackTrace(); 返回null; } } / * *数据库关闭 * / public void close(Connection conn,PreparedStatement ps,ResultSet rs){ try { if(rs!= null){ rs.close(); } if(ps!= null){ ps.close(); } if(conn!= null){ conn.close(); } } catch(SQLException e){ e.printStackTrace(); } } }

未定义

DBTools.java

包com.xk; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; public class DBTools { DBUtil dbu = new DBUtil(); 连接con = dbu.getConnection(); / * *用户登录 * / public boolean login(User user)throws Exception { String sql =“select * from users where username =?and password =?”; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1,user.getUsername()); ps.setString(2,user.getPassword()); ResultSet rs = ps.executeQuery(); if(rs.next()){ return true; } else { return false; } } / * *用户注册 * / public boolean register(User user){ String sql =“insert into users(username,password)value(?,?)”; 尝试{ PreparedStatement ps = con.prepareStatement(sql); ps.setString(1,user.getUsername()); ps.setString(2,user.getPassword()); ps.executeUpdate(); } catch(SQLException e){ e.printStackTrace(); 返回假 } return true; } / * *上传文件 * / public boolean uploadFile(FileEntity fe){ String sql =“insert into file(username,filename,filecontent)VALUES(?,?,?)”; 尝试{ PreparedStatement ps = con.prepareStatement(sql); ps.setString(1,fe.getUsername()); ps.setString(2,fe.getFileName()); ps.setBytes(3,fe.getFileContent()); ps.executeUpdate(); } catch(SQLException e){ e.printStackTrace(); 返回假 } return true; } / * *下载文件 * / public byte [] downloadFile(FileEntity fe)throws Exception { String sql =“从文件中选择filecontent id =?”; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1,fe.getId()); ResultSet rs = ps.executeQuery(); InputStream is = null; if(rs.next()){ is = rs.getBinaryStream(1); ByteArrayOutputStream(); byte [] buffer = new byte [is.available()]; int len while((len = is.read(buffer))!= -1){ baos.write(buffer,0,len); } return buffer; } return null; } / * *查看文件 * / public ArrayList viewFile(FileEntity fe)throws Exception { ArrayList fileList = new ArrayList (); String sql =“select id,filename from file where username =?”; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1,fe.getUsername()); ResultSet rs = ps.executeQuery(); while(rs.next()){ fileList.add(new FileEntity(rs.getInt(1),rs.getString(2))); } return fileList; } / * *删除文件 * / public boolean deleteFile(FileEntity fe)throws Exception { String sql =“从文件中删除id =?”; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1,fe.getId()); ps.execute(); if(ps.getUpdateCount()> 0){ return true; } return false; } }

未定义

Server.java

未定义

ServerThread.java

包com.xk; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; import java.util.ArrayList; public class ServerThread extends Thread { Socket socket = null; public ServerThread(Socket socket){ this.socket = socket; } public void run(){ try { ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); CommandRelay cr =(CommandRelay)ois.readObject(); 尝试{ cr = inquire(cr); oos.writeObject(CR); } catch(Exception e){ e.printStackTrace(); } } catch(ClassNotFoundException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } }

运行效果

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20171216A06LDV00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券