package com.inbasis;
import java.rmi.RemoteException;
import java.util.List;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.rmi.server.UnicastRemoteObject;
public class LoadFileOptionService extends UnicastRemoteObject implements LoadServer {
private String currentDir = null;
public LoadFileOptionService()throws java.rmi.RemoteException {
super();
}
public void setCurrentDir(String currentDir) {
this.currentDir = currentDir;
}
public FileOption downLoadFile(String fileName) throws RemoteException {
FileOption fileInfo = null;
BufferedInputStream input = null;
try {
if (fileName == null) {
throw new RemoteException("the paramter is null ");
}
String filePath = this.currentDir + "\\" + fileName;
File file = new File(filePath);
if (!file.exists()) {
throw new RemoteException("the file whose name is " +
fileName + " not existed ");
}
byte[] content = new byte[(int) file.length()];
input = new BufferedInputStream(new FileInputStream(file));
input.read(content);
fileInfo = new FileOptionSer();
fileInfo.setInformation(fileName, content);
} catch (RemoteException ex) {
throw ex;
} catch (Exception ex) {
throw new RemoteException(ex.getLocalizedMessage());
} finally {
if (input != null) {
try {
input.close();
input = null;
} catch (Exception ex) {
}
}
}
return fileInfo;
}
public void upLoadFile(FileOption fileInfo) throws RemoteException {
BufferedOutputStream output = null;
try {
if (fileInfo == null) {
throw new RemoteException("the paramter is null ");
}
String fileName = fileInfo.getName();
byte[] content = fileInfo.getContent();
if (fileName == null || content == null) {
throw new RemoteException("the file or the contentis null ");
}
String filePath = this.currentDir + "\\" + fileName;
File file = new File(filePath);
if (!file.exists()) {
file.createNewFile();
}
output = new BufferedOutputStream(new FileOutputStream(file));
output.write(content);
} catch (RemoteException ex) {
throw ex;
} catch (Exception ex) {
throw new RemoteException(ex.getLocalizedMessage());
} finally {
if (output != null) {
try {
output.close();
output = null;
} catch (Exception ex) {
}
}
}
}
}
评论0