PDA

View Full Version : Opening a PDF file from different URL


ynotlim
12-07-2006, 08:47 PM
I have a application that links to viewpdf.class. This class file will go to the FTP where the pdf file is located and open it. How do i do this using java? Here is what I have.. .i'm stuck

URL url = new URL("ftp://user01:pass1234@ftp.foo.com/"+ folder_name +"README.pdf;type=i");
URLConnection urlc = url.openConnection();
InputStream is = urlc.getInputStream();

thanks Tony

here is how it looks like if it is on the local host.


import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class viewpdf extends HttpServlet
{
private ResourceBundle rb;

public viewpdf()
{
rb = ResourceBundle.getBundle("LocalStrings");
}

public static void streamCopy(FileInputStream fileInputStream, ServletOutputStream servletOutputStream)
throws IOException
{
if(fileInputStream == null || servletOutputStream == null)
return;
byte abyte0[] = new byte[8192];
int i;
synchronized(fileInputStream)
{
synchronized(servletOutputStream)
{
while((i = fileInputStream.read(abyte0)) != -1)
servletOutputStream.write(abyte0, 0, i);
}
}
}

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{

ServletOutputStream servletOutputStream;
String filename;
String output;
FileInputStream fileInputStream;
servletOutputStream = res.getOutputStream();
res.setContentType("application/pdf");
ServletContext servletcontext = getServletContext();
filename = req.getParameter("file");
String folder_code = req.getParameter("fold");


try{

filename = servletcontext.getRealPath(rb.getString("file_location")) + "/" + fold + "/" + file;

fileInputStream = null;
fileInputStream = new FileInputStream(file);

streamCopy(fileInputStream, servletOutputStream);
}
catch(FileNotFoundException e)
{

filename = servletcontext.getRealPath(rb.getString("file_location")) + "/filenotfound.pdf";

fileInputStream = null;
fileInputStream = new FileInputStream(file);

streamCopy(fileInputStream, servletOutputStream);
}
}
}