Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

retreive pdf or html file on client side from server using rmi

843793Nov 30 2007 — edited Nov 30 2007
Hi

I created rmi program for accessing the file from the server but i am able to see it on my local system but if i created this on another system as a client then the file path is searching on client side.and displaying error File.not.found, any body can help . i am sending my code plz see it and suggest me solution.

import java.rmi.server.UnicastRemoteObject;
import java.io.*;
import java.rmi.*;


public class testImpl extends UnicastRemoteObject implements test
{

public File getVal() {
String path="C:/Test/P4Win-gs.pdf";
//String fileName="P4Win-gs.pdf";
File f=null;
try {
f=new File(path);
}

catch(Exception e){
e.printStackTrace();
}
return f;
}
public testImpl()throws RemoteException{

super();
}

}

and client file is

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.rmi.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.*;

public class testServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public testServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//PrintWriter out = response.getWriter();
try

{
System.out.println(" the client file");

System.setProperty("java.security.policy","policy");
if (System.getSecurityManager() == null){
System.setSecurityManager ( new RMISecurityManager() );
}

//System.setSecurityManager(new SecurityManager());
test servic=(test) Naming.lookup("rmi://localhost/Remote");
File f1= servic.getVal();
String s1=f1.getPath();
String fileType = s1.substring(s1.indexOf(".")+1,s1.length());

File f = f1;
FileInputStream istr = new FileInputStream(f);
BufferedInputStream bstr = new BufferedInputStream(istr);

int size = (int) f.length();
// get the file size (in bytes)
byte[] data = new byte[size]; // allocate byte array of right size
bstr.read( data, 0, size ); // read into byte array
bstr.close();
if (fileType.trim().equalsIgnoreCase("pdf")){
//response.reset();
response.setContentType(fileType);
response.setContentType( "application/pdf" );
}else if (fileType.trim().equalsIgnoreCase("html")){
response.setContentType("text/html");
}


response.setHeader("Content-Disposition","inline; filename='"+s1+"'");
OutputStream outStrm = response.getOutputStream();

outStrm.write(data);
outStrm.flush();
outStrm.close();

System.out.println("this is the first program "+s1);
}catch(Exception e)
{
e.printStackTrace();
}

}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 28 2007
Added on Nov 30 2007
1 comment
127 views