📄 receivefile.java
字号:
/*
* Author: Naizheng Bian
* Version: 1.0
* Date: 12/09/2001
*/
package serverPackage;
import java.net.*;
import java.io.*;
import java.util.*;
import mediaPackage.*;
/**
* This class receives an uploaded file by the presenter, it extends Thread
*/
public class ReceiveFile extends Thread
{
private UserManager mUserManager;
private InformClient mInformClient;
private InetAddress mClientAddr;
private String mFileURL="";
private int mFileUpLoadPort;
private Hashtable mOutputStreamTable;
/**
* The constructor initializes the various parameters about the file
* @param aURL The file URL
* @param aClientAddr The client IP address
* @param aFileUpPort The port for uploading file
* @param aClientInformer The InformClient instance to inform the uploaded file
* @param anOutputStreamTable the output stream hashtable
*/
public ReceiveFile(String aURL, InetAddress aClientAddr, int aFileUpPort,
UserManager aUserManager, InformClient aClientInformer, Hashtable anOutputStreamTable)
{
mFileURL = aURL;
mClientAddr = aClientAddr;
mFileUpLoadPort = aFileUpPort;
mUserManager = aUserManager;
mInformClient = aClientInformer;
mOutputStreamTable = anOutputStreamTable;
}
/**
* This method runs to receive file
*/
public void run()
{
this.receivingFile();
}
/**
* This method creates a server socket to receive a file.
* @return true if the file reception can start successfully, otherwise returns false
*/
public boolean receivingFile()
{
boolean connected = false;
int NoOfTries = 0;
BufferedInputStream in = null;
FileOutputStream fileOutStream = null;
Socket s = null;
do
{
try {
s = new Socket(mClientAddr, mFileUpLoadPort);
in = new BufferedInputStream(s.getInputStream());
String fileName = getFileName(mFileURL);
System.out.println("connection good,file is:"+fileName);
this.mFileURL = Constants.SERVER_URL + fileName;
connected = true;
fileOutStream = new FileOutputStream(Constants.SERVER_STORE_LOCATION + fileName);
System.out.println(fileOutStream);
}
catch(UnknownHostException uhe)
{
System.err.println("Host not found: " + uhe.getMessage());
return false;
}
catch(IOException ioe)
{
if (NoOfTries < Constants.MAX_CONNECTION_TRIES)
{
NoOfTries++;
try {
Thread.sleep(Constants.SLEEP_TIME);
}
catch(InterruptedException ie) {
}
}
else
{
System.err.println("An IO error occured while Receiving File" + ioe.getMessage());
return false;
}
}
} while((!connected) && (NoOfTries < Constants.MAX_CONNECTION_TRIES));
try {
int ch;
while((ch = in.read()) != Constants.EOF_INT)
fileOutStream.write(ch);
in.close();
fileOutStream.close();
s.close();
System.out.println("receiving file successfully");
}
catch(IOException ioe)
{
System.err.println("An IO error occured while writing file " + ioe.getMessage());
return false;
}
if (mUserManager.addURL(mFileURL)) {
mInformClient.addToContentManager(mUserManager.getContentManagerTable(),
mUserManager.getUserConfigList(), mFileURL,
mOutputStreamTable);
return true;
} else {
return false;
}
}
/**
* This method gets the file name based on the input url string
* @param urlStr The URL string for returned file name
* @return a string as the obtained name
*/
private String getFileName(String urlStr)
{
if (urlStr == "" || urlStr == null)
{
showMessage("In ReceiveFile class: URL is empty.");
return null;
}
int nameStartIndex = urlStr.lastIndexOf("/");
//if(nameStartIndex == -1)
// nameStartIndex = urlStr.lastIndexOf("\\");
if(nameStartIndex == -1)
nameStartIndex = urlStr.lastIndexOf("//");
if (nameStartIndex == -1) nameStartIndex=urlStr.lastIndexOf("\\");
String fileName = null;
if(nameStartIndex != -1)
fileName = urlStr.substring(nameStartIndex + 1);
return fileName;
}
/**
* This method shows a message
* @param aString The message string which will be shown
*/
private void showMessage(String aString)
{
System.err.println("In ReceiveFile Class: " + aString);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -