📄 contentdisplay.java
字号:
package clientPackage;
import mediaPackage.*;
import java.awt.Point;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.*;
public class ContentDisplay extends JEditorPane{
private static final String SERVER = "127.0.0.1";
public void addPropertyChangeListener(ClientUI clientUI) {
// TODO Auto-generated method stub
}
public void showFile(String newURL) {
getThePage(newURL);
}
private void getThePage(String url){
String fileName=null;
if (url.indexOf("file:") != -1)
try{
fileName=getFileName(url);
System.out.println("file name is:"+fileName);
FileInputStream fileIn=new FileInputStream("E:/slides/"+fileName);
fileIn.close();
}catch(FileNotFoundException e0){
System.out.println("file not exist,download the file from server now!");
try{
Socket socket=new Socket(SERVER,Constants.FILEPORT);
ObjectOutputStream output=new ObjectOutputStream(socket.getOutputStream());
FileOutputStream out=new FileOutputStream("E:/slides/"+fileName);
BufferedInputStream in=new BufferedInputStream(socket.getInputStream());
output.writeObject(fileName);
//output.close();
int ch;
while ((ch=in.read())!=-1) out.write(ch);
out.close();
in.close();
socket.close();
System.out.println("downloading file successfully");
}catch(Exception e2){
System.err.println("error in downloading file"+e2.getMessage());
e2.printStackTrace();
return;
}
}catch(IOException e1){
System.err.println("ioexception in setpage");
}
try{
setPage(url);
}catch(IOException e0){
System.err.println("can not get the page!"+url);
}
}
public void drawRedDot(Point point) {
// TODO Auto-generated method stub
}
private String getFileName(String urlStr)
{
if (urlStr == "" || urlStr == null)
{
System.out.println("In contentDisplay class: URL is empty.");
return null;
}
int 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);
else
{
System.out.println("URL form error: " + urlStr
+ "\n URL should be in the form of protocol://Path/FileName");
}
return fileName;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -