Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java Open Source > Apache Tomcat
|
Apache Tomcat General discussion of the Apache Tomcat servlet container. For discussions specific to the Professional Apache Tomcat book, please see the book discussion forum for that book.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Apache Tomcat section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 17th, 2006, 05:18 AM
Registered User
 
Join Date: May 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
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
 
Old July 17th, 2006, 05:46 AM
Registered User
 
Join Date: May 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
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
 
Old September 14th, 2006, 06:09 AM
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?



 
Old September 14th, 2006, 06:11 AM
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?

 
Old September 26th, 2006, 04:44 AM
Registered User
 
Join Date: May 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
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:
quote:
Is it possible to send files from java application to ftp server or not?
Sure, you can.
Quote:
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/tutor...rking/sockets/
http://www.javaworld.com/javaworld/j...2-sockets.html
http://jakarta.apache.org/commons/net/

raj





Similar Threads
Thread Thread Starter Forum Replies Last Post
Facing Problem in WSAD mvharish J2EE 0 May 25th, 2007 04:24 AM
i am facing a problem mkazim85 SQL Language 2 April 20th, 2007 07:28 AM
Facing problem with commandtimeout altaf SQL Server ASP 0 June 27th, 2003 08:33 AM
Facing problem with commandtimeout altaf Classic ASP Databases 0 June 27th, 2003 08:27 AM
Facing problem with commandtimeout altaf SQL Server 2000 0 June 27th, 2003 08:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.