了解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<Doc> 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 jav