从ftp服务器在jsp中显示图像

时间:2014-12-02 17:26:08

标签: jsp servlets ftp

我在ftp服务器上上传了一些文件,我想在我的网页上通过jsp / servlet显示。 我知道我们可以在webserver上使用“img src”标签,但我不知道如何从ftp服务器显示文件。 有人能告诉我怎么做吗?

我可以从ftp服务器检索网页中的文件名。 这是我的功能:

PrintWriter out;

protected void doPost(HttpServletRequest request,HttpServletResponse response)抛出ServletException,IOException {         // TODO自动生成的方法存根

    response.setContentType("text/html");
    out = response.getWriter();

    FtpConnection();

}

public void FtpConnection()
{
    // get an ftpClient object  
      FTPClient ftpClient = new FTPClient();          

      /*********  work only for Dedicated IP ***********/    
        final String FTP_HOST= "my.ftp.server.ip";

        /*********  FTP USERNAME ***********/
        final String FTP_USER = "my_user_name";

        /*********  FTP PASSWORD ***********/
        final String FTP_PASS  ="my_password";

      try {  
          // pass directory path on server to connect  
          ftpClient.connect(FTP_HOST, 21);

          // pass username and password, returned true if authentication is  
          // successful  
          boolean login = ftpClient.login(FTP_USER, FTP_PASS);  

          if (login) {  

              out.println("FTP Connection established...");
              out.println("Status: "+ftpClient.getStatus());

                     //my files are stored in folder named: uploads, so changing the folder here
              ftpClient.changeWorkingDirectory("/uploads/");


              out.println("list of FTP files is:");
              FTPFile fileList[]=ftpClient.listFiles(); 
              for (FTPFile ftpFile : fileList) {
                out.println(" Name: "+ftpFile.getName());
            }

              boolean logout = ftpClient.logout();  
              if (logout) {  
                  out.println("FTP Connection close..."); 
              }  
          } else {  
               out.println("FTP Connection fail..."); 
          }       
      } catch (SocketException e) {  
          e.printStackTrace();  
      } catch (IOException e) {  
          e.printStackTrace();  
      } finally {  
          try {  
              ftpClient.disconnect();  
          } catch (IOException e) {  
              e.printStackTrace();  
          }  
      }
}

0 个答案:

没有答案