Subject: Facing problem with tomcat 5.5.9
Posted By: bhojrajjoshi Post Date: 5/15/2006 7:53:47 AM
Hi everybody.
I'm maintaing a system in java that transfers files from on machine to another. For actual data transfer it uses servlets. Previously it was running on tomcat 4.0 and OK. But, when I changed the server from tomcat 4.0 to tomcat 5.5.9 it's creating some problems in transferring data(files) in a network, however it's OK within the local system. I've successfully installed and configured the tomcat, but still in trouble. Could anyone kindly help me in the same.

Your prompt reply would be highly appreciated becoz its very urgent!
Thanks in advance.

raj

Bhoj Raj Joshi
Web/Application Developer,
KDK Infotech, New Delhi.
Reply By: enjay Reply Date: 5/18/2006 11:32:59 AM
Hi,

Can you outline the problems?

A stacktrace of the same will help

Regards,
eNJay

Reply By: bhojrajjoshi Reply Date: 5/19/2006 7:39:19 AM
Thanks for your quick reply.

Well, my system starts from a normal java class namely UploadFile.java which calls a servlet class namely RecvServlet_data.java like this way:

Context root is %CATALINA_HOME%/webapps/examples.

code of UploadFile.java:

import java.io.*;
import java.net.*;

public class UploadFile
{
    URLConnection con;
    
    public static void main(String []str)
    {        
        try
        {
            File srcFile = new File(str[0]);
            String destPath = str[1];
            
            String result = new UploadFile().transferFile(srcFile, destPath);
            System.out.println(result);
        }
        catch(Exception e)
        {
            System.out.println("Usage: sourcePath destPath");
            System.exit(1);
        }
    }
    
    public String transferFile(File sourceFile, String destPath)
    {
        try
        {                            
             String base = "http://127.0.0.1:8080/examples/servlet/RecvServlet_data";
            
            String fileName = sourceFile.getPath();
                                
            destPath += File.separator + sourceFile.getName();
                                               
            FileInputStream fin = new FileInputStream(sourceFile);
            
            URL url = new URL(base + "?name=" + URLEncoder.encode(fileName,"UTF-8") + "&path=" + URLEncoder.encode(destPath,"UTF-8"));
            con = url.openConnection();
              
            con.setRequestProperty("REQUEST_METHOD", "POST");
               
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(true);
            con.setRequestProperty("Content-Type", "multipart-formdata");
            
            DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
            
            int c = 30720;
                     
            byte b[] = new byte[c];
            
            for(int i = fin.read(b, 0, c); i >= 0; i = fin.read(b, 0, c))
            {
                dataOut.write(b, 0, i);
            }
                     
            dataOut.flush();
            dataOut.close();
            fin.close();
            
            return "Success";
        }
        catch(Exception e)
        {
            System.out.println("Error: " + e.toString());
            return "Failure";           
          }
        finally
        {
            con = null;
        }      
    }
}


Code of RecvServlet_data.java:

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

public class RecvServlet_data extends HttpServlet
{
    public RecvServlet_data()
       {
       }

      public void init()throws ServletException
      {
    }

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

     public void doPost(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException
     {       
        String fileName = req.getParameter("name");
        String destIP = req.getParameter("path");
        
        System.out.println("File : " + fileName + " is ready to transfer to " + destIP);
                 
        File destFile = new File(destIP);
        
        DataInputStream dataIn = new DataInputStream(req.getInputStream());
       
        BufferedOutputStream buffOut = new BufferedOutputStream(new FileOutputStream(destFile));
        
        int c = 30720;
             
        byte b[] = new byte[c];
            
        try
        {
            for(int j = dataIn.read(b, 0, c); j >= 0; j = dataIn.read(b, j, c))
            {
                buffOut.write(b, 0, j);
                c += j;
               }
           }
        catch(Exception e)
        {
            System.out.println("Error in Servlet : " + e.getMessage());
           }
       }
}

web.xml file is configured as:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
     
<servlet>
    <servlet-name>
            RecvServlet_data
    </servlet-name>
        <servlet-class>
            RecvServlet_data
           </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>
        RecvServlet_data
    </servlet-name>
    <url-pattern>
        /servlet/RecvServlet_data
    </url-pattern>
</servlet-mapping>
   
</web-app>

Now, when i'm trying to run UploadFile file, i just get "Success" message and nothing else done.

This is just that part of my system which actually transfers files. I don't know whether my code has been changed or any other problem...please help me.


Bhoj Raj Joshi
Web/Application Developer,
KDK Infotech, New Delhi.
Reply By: enjay Reply Date: 5/19/2006 3:29:43 PM
Did you look at the port you are trying to open in the class?

http://127.0.0.1:8080/examples/servlet/RecvServlet_data";

You are trying to access the servlet on the local machine. No wonder why it works on the machine that has the server. Replace it with the IP address of the machine where servlet is. That should work.

Regards,
eNJay
Reply By: bhojrajjoshi Reply Date: 5/22/2006 1:52:31 AM
I'm doing so because the servlet class is in the same local system and tomcat as well. I've also done what u suggested me i.e. replacing 127.0.0.1 with the IP address of the system on which tomcat is running with my servlet class, but i again end up with the same problem.

Alternatively, is there any machanism to grant privilleges to a tomcat server for accessing files or other resources in a network?

raj
Reply By: enjay Reply Date: 5/24/2006 7:20:27 AM
Can you do a quick as to what is being received at the servlet end? I see that you are writing it to a file. Is that file created at all?

Regards,
eNJay

Reply By: bhojrajjoshi Reply Date: 5/25/2006 12:52:06 AM
Hi eNJay,
Thanks very much for ur curious attachment with my problem. I got the point now, the problem was with my tomcat version rather than code.
Previously, i was using tomcat 4.0 console version and then migrated to tomcat 5.5 running as a service.
This was the main problem. But when i run tomcat 5.5 from within console, IT WORKED as before.
THANKS anyway.

raj
Reply By: enjay Reply Date: 5/30/2006 1:18:00 AM
Thanx for the update.

Interesting. Just wondering as to what might have broken when the server was running as service..

Regards,
eNJay

Reply By: Mr. Ram Reply Date: 7/12/2006 11:30:45 PM
Raj,

Pls let me know the command, how u r executing this program.  I am trying as below :

java UploadFile classpath.txt \\sys1203\\Ram

Reply By: panacea Reply Date: 7/13/2006 2:53:27 AM
Maybe I'm completely confused, but why aren't you calling the connect() method on your URLConnection object?  I'm surprised the code worked without it on Tomcat 4.x.

Jon Emerson
http://www.zoominfo.com/JonEmerson
Reply By: bhojrajjoshi Reply Date: 7/17/2006 5:18:20 AM
quote:
Originally posted by Mr. Ram

Raj,

Pls let me know the command, how u r executing this program.  I am trying as below :

java UploadFile classpath.txt \\sys1203\\Ram





Hi Mr. Ram,

In fact the code that you used was just a part of my whole project, that's why there were some missing statements in the code. If you interested in such kindly go through the following java source files that are properly tested and complete in itself while other things remain the same.

UploadFile.java

import java.io.*;
import java.net.*;

public class UploadFile
{
    URLConnection con;
    
    public static void main(String []str)
    {        
        try
        {
            File srcFile = new File(str[0]);
            String destPath = str[1];
            
            new UploadFile().transferFile(srcFile, destPath);            
        }
        catch(Exception e)
        {
            System.out.println("Usage: sourcePath destPath");
            System.exit(1);
        }
    }
    
    public void transferFile(File sourceFile, String destPath)
    {
        try
        {                            
            String base = "http://127.0.0.1:8080/test/servlet/RecvServlet_data";
            
            String fileName = sourceFile.getPath();
                                
            destPath += File.separator + sourceFile.getName();
                                               
            FileInputStream fin = new FileInputStream(sourceFile);
            
            URL url = new URL(base + "?name=" + fileName + "&path=" + destPath);            
               
               con = url.openConnection();
              
            con.setRequestProperty("REQUEST_METHOD", "POST");
               
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setUseCaches(true);
            con.setRequestProperty("Content-Type", "multipart-formdata");
                                    
            DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
            
            int c = 30720;
                     
            byte b[] = new byte[c];
            
            for(int i = fin.read(b, 0, c); i >= 0; i = fin.read(b, 0, c))
            {
                dataOut.write(b, 0, i);            
            }
                     
            
            dataOut.close();
            fin.close();
            
            InputStream input = con.getInputStream();
            
            BufferedReader br = new BufferedReader(new InputStreamReader(input));
            
            String strRes = null;
            while((strRes = br.readLine()) != null)
                System.out.println(strRes);
                
            input.close();
            br.close();
        
        }
        catch(Exception e)
        {
            System.out.println("Error: " + e.toString());            
        }
        finally
        {
            con = null;
        }      
    }
}


RecvServlet_data.java (Servlet File)

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

public class RecvServlet_data extends HttpServlet
{
      
    public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
             doPost(req, res);
    }

     public void doPost(HttpServletRequest req, HttpServletResponse res)
         throws ServletException, IOException
    {       
        String fileName = req.getParameter("name");
        String destIP = req.getParameter("path");
        
        res.setContentType("text/plain");
        PrintWriter pw = res.getWriter();
                        
        System.out.println("File : " + fileName + " is ready to transfer to " + destIP);
                 
        File destFile = new File(destIP);
        
        DataInputStream dataIn = new DataInputStream(req.getInputStream());
       
        BufferedOutputStream buffOut = new BufferedOutputStream(new FileOutputStream(destFile));
        
        int c = 30720;
             
        byte b[] = new byte[c];
            
        try
        {
            for(int j = dataIn.read(b, 0, c); j >= 0; j = dataIn.read(b, 0, c))
            {
                buffOut.write(b, 0, j);               
            }
            
            dataIn.close();
            buffOut.close();
            
            System.out.println("Now, done...");
            pw.write("File transfer completed");
            
        }
        catch(Exception e)
        {
            System.out.println("Error in Servlet : " + e.getMessage());
            pw.write("Error in transferring file...");
        }
    }
}


Now try to execute UploadFile.java by giving the absolute paths rather than relative paths.

eg. - java UploadFile c:\\classpath.txt d:\\test

If still getting any problem, let me know.

Raj
Reply By: bhojrajjoshi Reply Date: 7/17/2006 5:46:54 AM
quote:
Originally posted by panacea

Maybe I'm completely confused, but why aren't you calling the connect() method on your URLConnection object?  I'm surprised the code worked without it on Tomcat 4.x.

Jon Emerson
http://www.zoominfo.com/JonEmerson



Jon,

Probably you didn't notice a statement in the code:

con = url.openConnection();

this statement does exactly what connect() method of URLConnection objet does. However, some code statements were missing in my code that i have corrected now and sent to Mr. Ram. You may refer to the code if needed.

Thanks for your suggestion.

Raj
Reply By: mar4ik Reply Date: 9/14/2006 6:09:14 AM
Hi,

I've tried to use this same source for ftp-transfers. It doesn't work.
Is it possible to send files to a ftp-server by using a java application or a servlet?



Reply By: mar4ik Reply Date: 9/14/2006 6:11:36 AM
Hello,

I've tried to use the same source for the transferring files to the ftp-server. It doesn't work.


Is it possible to send files from java application to ftp server or not?

If it's possible, could anyone help me with that, please?

Reply By: bhojrajjoshi Reply Date: 9/26/2006 4:44:42 AM
quote:
Originally posted by mar4ik


I've tried to use the same source for the transferring files to the ftp-server. It doesn't work.



The code shown above is based on HTTP. That's why it may not work for FTP server.
quote:

Is it possible to send files from java application to ftp server or not?


Sure, you can.
quote:

If it's possible, could anyone help me with that, please?


Refer to the following sites:

http://java.sun.com/docs/books/tutorial/essential/io/
http://java.sun.com/docs/books/tutorial/networking/sockets/
http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html
http://jakarta.apache.org/commons/net/

raj

Go to topic 50244

Return to index page 165
Return to index page 164
Return to index page 163
Return to index page 162
Return to index page 161
Return to index page 160
Return to index page 159
Return to index page 158
Return to index page 157
Return to index page 156