了解Java中I/O流的概念和种类;掌握字节流处理和字符流处理,包括File类,InputStream/OutputStream及其子类,Reader/Writer及其子类;熟练掌握文件的顺序处理,随机访问处理;熟悉对象串行化的概念和方法。
编写程序,实现档案管理系统中的文件上传/下载模块。要求用户登录系统后,可根据系统存储数据,浏览已有档案资料信息;可根据档案号,下载对应档案文件至指定目录;可输入并在系统中记录新档案信息,并将对应的档案文件上传至指定目录。要求如下:
(1)完善showFileList()方法,实现档案信息浏览,在未讲数据库之前,系统中已存在档案信息放置在Hashtable中,提供新版DataProcessing类,该类实现了对应数据的查找、插入操作。
(2)完善uploadFile()方法,实现档案数据的上传,在未讲网络之前,该方法只需实现在指定目录中读取文件,并将其拷贝至其他目录中,此外还需将相关档案信息写入对应Hashtable中。
(3)完善downloadFile(),实现档案数据下载,目前只需要实现根据档案号,在Hashtable中查找得到文件位置,然后读取文件并将其拷贝至指定目录中。
import java.sql.SQLException;
import java.util.Enumeration;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public abstract class User {
private String name;
private String password;
private String role;
String uploadpath="C:\\uploadfile\\";
String downloadpath="C:\\downloadfile\\";
User(String name,String password,String role){
this.name=name;
this.password=password;
this.role=role;
}
public abstract void showMenu(String name);
public void showFileList() throws SQLException{
/*
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in accessing file DB");
*/
Enumeration e=null;
try {
e=DataProcessing.getAllDocs();
}
catch(SQLException e1) {
System.out.println(e1.getMessage());
}
Doc doc;
while(e.hasMoreElements()) {
doc=e.nextElement();
System.out.println("ID:"+doc.getID()+"\tCreator:"+doc.getCreator()+"\tTime:"+
doc.getTimestamp()+"\tFilename:"+doc.getFilename()+"\tDescription:"+doc.getDescription());
}
}
public boolean downloadFile(String ID) throws IOException{
/*
double ranValue=Math.random();
if(ranValue>0.5) throw new IOException("Error in accessing file");
*/
byte[] buffer=new byte[1024];
Doc doc=null;
try {
doc = DataProcessing.searchDoc(ID);
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
if(doc==null) return false;
File tempFile=new File(uploadpath+doc.getFilename());
String filename=tempFile.getName();
BufferedInputStream infile=new BufferedInputStream(new FileInputStream(tempFile));
BufferedOutputStream targetfile=new BufferedOutputStream(new FileOutputStream(new File(downloadpath+filename)));
while(true) {
int byteRead=infile.read(buffer);
if(byteRead==-1)
break;
targetfile.write(buffer,0,byteRead);
}
infile.close();
targetfile.close();
return true;
}
public boolean changeSelfInfo(String password) throws SQLException{
if(DataProcessing.update(name,password,role)) {
this.password=password;
System.out.println("修改成功!");
return true;
}
else {
System.out.println("修改失败!");
return false;
}
}
public void exitSystem() {
System.out.println("系统退出,谢谢使用!");
System.exit(0);
}
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
public void setPassword(String password) {
this.password=password;
}
public String getPassword() {
return password;
}
public void setRole(String role) {
this.role=role;
}
public String getRole() {
return role;
}
}
import java.util.Enumeration;
import java.util.Hashtable;
import java.sql.*;
public class DataProcessing {
private static boolean connectToDB=false;
static Hashtable users;
static Hashtable docs;
static {
users=new Hashtable();
users.put("hjy", new Operator("hjy","111","operator"));
users.put("cr", new Browser("cr","000","browser"));
users.put("myk", new Administrator("myk","250","administrator"));
Init();
Timestamp timestamp=new Timestamp(System.currentTimeMillis());
docs=new Hashtable();
docs.put("0001", new Doc("0001","hjy",timestamp,"Doc Source Java","Doc.java"));
}
public static void Init() {
//connect to database
//update database connection status
/*
if(Math.random()>0.2) connectToDB=true;
else connectToDB=false;
*/
}
public static Doc searchDoc(String ID)throws SQLException{
if(docs.containsKey(ID)) {
Doc temp=docs.get(ID);
return temp;
}
return null;
}
public static Enumeration getAllDocs() throws SQLException{
Enumeration e=docs.elements();
return e;
}
public static boolean insertDoc(String ID,String creator,Timestamp timestamp,String description,String filename)throws SQLException{
Doc doc;
if(docs.containsKey(ID)) return false;
else {
doc=new Doc(ID,creator,timestamp,description,filename);
docs.put(ID, doc);
return true;
}
}
public static User searchUser(String name) throws SQLException{
/*
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
*/
if(users.containsKey(name)) {
return users.get(name);
}
return null;
}
public static User search(String name,String password) throws SQLException{
/*
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
*/
if(users.containsKey(name)) {
User temp=users.get(name);
if(temp.getPassword().equals(password))
return temp;
}
return null;
}
public static Enumeration getAllUser() throws SQLException{
/*
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
*/
Enumeration e=users.elements();
return e;
}
public static boolean update(String name,String password,String role) throws SQLException{
User user;
/*
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
*/
if(users.containsKey(name)) {
if(role.equalsIgnoreCase("administrator"))
user=new Administrator(name,password,role);
else if(role.equalsIgnoreCase("operator"))
user=new Operator(name,password,role);
else
user=new Browser(name,password,role);
users.put(name, user);
return true;
}
else return false;
}
public static boolean insert(String name,String password,String role) throws SQLException{
User user;
/*
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in executing Query");
*/
if(users.containsKey(name))
return false;
else {
if(role.equalsIgnoreCase("administrator"))
user=new Administrator(name,password,role);
else if(role.equalsIgnoreCase("operator"))
user=new Operator(name,password,role);
else
user=new Browser(name,password,role);
users.put(name, user);
return true;
}
}
public static boolean delete(String name) throws SQLException{
/*
if(!connectToDB) throw new SQLException("Not Connected to Database");
double ranValue=Math.random();
if(ranValue>0.5) throw new SQLException("Error in excecuting Query");
*/
if(users.containsKey(name)) {
users.remove(name);
return true;
}
else return false;
}
public void disconnectFromDB() {
if(connectToDB) {
//close Statement and Connection
try {
/*
if(Math.random()>0.5) throw new SQLException("Error in disconnecting DB");
}
catch(SQLException sqlException) {
sqlException.printStackTrace();
*/
}finally {
connectToDB=false;
}
}
}
}
import java.sql.Timestamp;
class Doc{
private String ID;
private String creator;
private Timestamp timestamp;
private String description;
private String filename;
public Doc(String ID, String creator, Timestamp timestamp, String description, String filename) {
super();
this.ID = ID;
this.creator = creator;
this.timestamp = timestamp;
this.description = description;
this.filename=filename;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
}
import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Scanner;
public class Administrator extends User{
private Scanner sc;
Administrator(String name, String password, String role) {
super(name, password, role);
}
public void changeUserInfo() {
System.out.println("请输入要修改的用户姓名:");
String cn=sc.next();
System.out.println("请输入新的用户密码:");
String cm=sc.next();
System.out.println("请输入新的用户角色:");
String cr=sc.next();
try {
if(DataProcessing.update(cn, cm, cr)) {
System.out.println("修改用户信息成功!");
}
else System.out.println("修改失败!");
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void delUser() {
System.out.println("请输入要删除的用户姓名:");
String dn=sc.next();
try {
if(DataProcessing.delete(dn)) {
System.out.println("删除成功!");
}
else System.out.println("删除失败!该用户不存在");
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void addUser() {
System.out.println("请输入要增加的用户姓名:");
String an=sc.next();
System.out.println("请输入要增加的用户密码:");
String am=sc.next();
System.out.println("请输入要增加的用户角色:");
String ar=sc.next();
try {
if(DataProcessing.insert(an, am, ar)) {
System.out.println("增加用户"+an+"成功!");
}
else System.out.println("增加用户"+an+"失败!");
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public void listUser() {
Enumeration e = null;
try {
e = DataProcessing.getAllUser();
}
catch (SQLException e1) {
System.out.println(e1.getMessage());
}
User user;
while(e.hasMoreElements()) {
user=e.nextElement();
System.out.println("姓名:"+user.getName()+"密码:"+user.getPassword()+"角色:"+user.getRole());
}
}
public void showMenu(String name) {
System.out.println("欢迎进入档案操作员菜单!");
System.out.println("1.修改用户");
System.out.println("2.删除用户");
System.out.println("3.新增用户");
System.out.println("4.列出用户");
System.out.println("5.下载文件");
System.out.println("6.文件列表");
System.out.println("7.修改(本人)密码");
System.out.println("8.退出");
sc=new Scanner(System.in);
int i=sc.nextInt();
switch(i) {
case 1:{
changeUserInfo();
break;
}
case 2:{
delUser();
break;
}
case 3:{
addUser();
break;
}
case 4:{
listUser();
break;
}
case 5:{
System.out.println("请输入档案编号:");
String ID=sc.next();
try {
if(downloadFile(ID)) {
System.out.println("下载成功!");
}
else System.out.println("下载失败!");
}
catch (IOException e) {
System.out.println(e.getMessage());
}
break;
}
case 6:{
try {
showFileList();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
break;
}
case 7:{
System.out.println("请输入新密码:");
String psd=sc.next();
try {
changeSelfInfo(psd);
}
catch (SQLException e) {
System.out.println("文件访问错误"+e.getMessage());
}
break;
}
case 8:{
exitSystem();
}
default:{
System.out.println("输入非法!请重新输入!");
break;
}
}
showMenu(name);
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Scanner;
public class Operator extends User{
Operator(String name, String password, String role) {
super(name, password, role);
}
public void showMenu(String name) {
System.out.println("欢迎进入档案操作员菜单!");
System.out.println("1.上传文件");
System.out.println("2.下载文件");
System.out.println("3.文件列表");
System.out.println("4.修改密码");
System.out.println("5.退出");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int i=sc.nextInt();
switch(i) {
case 1:{
try {
uploadFile(name);
}
catch (IOException e) {
System.out.println(e.getMessage());
}
System.out.println("文件上传成功!");
break;
}
case 2:{
System.out.println("请输入档案编号:");
String ID=sc.next();
try {
if(downloadFile(ID)) {
System.out.println("下载成功!");
}
else System.out.println("下载失败!");
}
catch (IOException e) {
System.out.println(e.getMessage());
}
break;
}
case 3:{
try {
showFileList();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
break;
}
case 4:{
System.out.println("请输入新密码:");
String psd=sc.next();
try {
changeSelfInfo(psd);
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
break;
}
case 5:{
exitSystem();
}
default:{
System.out.println("输入非法!请重新输入!");
break;
}
}
showMenu(name);
}
public void uploadFile(String name) throws IOException{
System.out.println("请输入源文件名:");
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
String filename=sc.next();
System.out.println("请输入档案号");
String ID=sc.next();
System.out.println("请输入档案描述");
String description=sc.next();
byte[] buffer=new byte[1024];
Timestamp timestamp=new Timestamp(System.currentTimeMillis());
File tempFile=new File(filename.trim());
String fileName=tempFile.getName();
try {
if(DataProcessing.insertDoc(ID,name,timestamp,description,fileName)) ;
else System.out.println("存入数据库失败!");
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
BufferedInputStream infile = null;
infile = new BufferedInputStream(new FileInputStream(filename));
BufferedOutputStream targetfile = null;
targetfile = new BufferedOutputStream(new FileOutputStream(new File(uploadpath+fileName)));
while(true) {
int byteRead = 0;
byteRead = infile.read(buffer);
if(byteRead==-1)
break;
targetfile.write(buffer,0,byteRead);
}
infile.close();
targetfile.close();
}
}
import java.io.IOException;
import java.sql.SQLException;
import java.util.Scanner;
public class Browser extends User{
Browser(String name, String password, String role) {
super(name, password, role);
}
public void showMenu(String name) {
System.out.println("欢迎进入档案浏览员菜单!");
System.out.println("1.下载文件");
System.out.println("2.文件列表");
System.out.println("3.修改密码");
System.out.println("4.退出");
System.out.print("请选择:");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int i=sc.nextInt();
switch(i) {
case 1:{
System.out.println("请输入档案编号:");
String ID=sc.next();
try {
if(downloadFile(ID)) {
System.out.println("下载成功!");
}
else System.out.println("下载失败!");
}
catch (IOException e) {
System.out.println(e.getMessage());
}
break;
}
case 2:{
try {
showFileList();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
break;
}
case 3:{
System.out.println("请输入新密码:");
String psd=sc.nextLine();
try {
changeSelfInfo(psd);
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
break;
}
case 4:{
exitSystem();
}
default:{
System.out.println("输入非法!请重新输入!");
break;
}
}
showMenu(name);
}
}
import java.sql.SQLException;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
while(true) {
System.out.println("欢迎进入档案管理系统!");
System.out.println("1.登录");
System.out.println("2.退出");
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
if(i==1) {
System.out.println("请输入用户名:");
String name=sc.next();
try {
if(DataProcessing.searchUser(name)!=null) {
System.out.println("请输入密码:");
String password=sc.next();
try {
if(DataProcessing.search(name ,password)!=null)
DataProcessing.search(name, password).showMenu(name);
else System.out.println("密码错误!");
}
catch(SQLException e) {
System.out.println(e.getMessage());
}
}
else {
System.out.println("用户不存在!");
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}
else if(i==2) {
System.exit(0);
}
else {
System.out.println("输入非法!请重新输入。");
}
}
}
}
以下是需要完成的结果展示:Java面向对象与多线程综合实验(三)之封装、继承与多态