Java で RMI サーバーを実装しました。それは正常に動作します。JSP ページを使用してサーバーと対話したいのですが、機能しません。それを機能させるために何をすべきか誰か教えてもらえますか?
MyServer.java
package pack1;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class MyServer {
private void startServer(){
try{
Registry registry = LocateRegistry.createRegistry(2000);
registry.rebind("MyData", new MyClassImpl());
}catch(Exception ex){
ex.printStackTrace();
}
System.out.println("Server is connected and ready to go....!!");
System.out.println("Waiting for request...");
}
public static void main(String[] args){
MyServer server = new MyServer();
server.startServer();
}
}
MyClient.java
package pack1;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
class MyClient{
private void Test(){
try{
Registry registry = LocateRegistry.getRegistry(2000);
MyClass myClass = (MyClass)registry.lookup("MyData");
if(myClass.upload("AssadUllah","assad.xml")){
System.out.println("Successfully uploaded file");
}else{
System.out.println("Problem uploading file");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
MyClient client = new MyClient();
client.Test();
}
}
MyClass.java
package pack1;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface MyClass extends Remote{
public Boolean upload(String fileName, String fileData)throws RemoteException;
//public String download(String fileName)throws RemoteException;
//public String checkNumber(int num)throws RemoteException;
//public int factorial(int num)throws RemoteException;
}
MyClassImpl.java
package pack1;
import java.io.*;
import java.util.regex.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class MyClassImpl extends UnicastRemoteObject implements MyClass{
private int c;
public MyClassImpl()throws RemoteException{
c=0;
}
public Boolean upload(String fileName, String fileData)throws RemoteException{
String postfix="";
try{
Pattern pattern = Pattern.compile("[.][a-zA-Z]+");
Matcher matcher = pattern.matcher(fileData);
if(matcher.find()){
postfix = fileData.substring(matcher.start(), matcher.end());
}
File inputFile = new File("E:\\UploadedFiles\\"+fileData);
File outputFile = new File("E:\\DownloadedFiles\\"+fileName+postfix);
FileInputStream IStream = new FileInputStream(inputFile);
FileOutputStream OStream = new FileOutputStream(outputFile);
while((c = IStream.read()) != -1){
OStream.write(c);
}
IStream.close();
OStream.close();
return true;
}catch(IOException ex){
ex.printStackTrace();
}
return false;
}
}
私のjspコードpage.jsp
<%@ page import="pack1.*"%>
<%@ page import="java.rmi.registry.Registry"%>
<%@ page import="java.rmi.registry.LocateRegistry"%>
<%
out.println(request.getRemoteAddr());
try{
Registry registry = LocateRegistry.getRegistry(2000);
MyClass myClass = (MyClass)registry.lookup("MyData");
String fileName = request.getParameter("fileName");
String myFile = request.getParameter("myFile");
myClass.upload(fileName, myFile);
}catch(Exception ex){
ex.printStackTrace();
}
%>
これは、RMI サーバーでファイルをアップロードするために使用している JSP ページです。コードは JSP なしでもうまく機能しますが、JSP でも機能するようにしたいと考えています。