Results 1 to 2 of 2
  1. #1
    milkman128 is offline Member
    Join Date
    Oct 2008
    Posts
    6
    Rep Power
    0

    Default Applet > servlet file transfer

    I am trying to get an applet to transfer files to a servlet. My problem is getting the applet to pass the information to the servlet

    I am using resin as a container
    applet servlet code below



    Applet code


    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    import javax.swing.*;

    public class FileUploadApplet extends JApplet implements ActionListener
    {

    JButton jbutton = null;
    public void init()
    {
    jbutton = new JButton("Send file");
    jbutton.addActionListener(this);
    this.getContentPane().add(jbutton);
    }

    public void actionPerformed(ActionEvent ae)
    {
    if(ae.getSource() == jbutton)
    {
    try
    {
    File file = new File("C:\\NewToledo.txt");

    FileInputStream in = new FileInputStream(file);
    byte[] buf=new byte[in.available()];
    int bytesread = 0;

    String toservlet = "localhost8080/test/etc..";

    URL servleturl = new URL(toservlet);
    URLConnection servletconnection = servleturl.openConnection();
    servletconnection.setDoInput(true);
    servletconnection.setDoOutput(true);
    servletconnection.setUseCaches(false);
    servletconnection.setDefaultUseCaches(false);

    DataOutputStream out = new DataOutputStream(servletconnection.getOutputStream ());

    while( (bytesread = in.read( buf )) > -1 )
    {
    out.write( buf, 0, bytesread );
    }

    out.flush();
    out.close();
    in.close();

    DataInputStream inputFromClient = new DataInputStream(servletconnection.getInputStream() );
    //get what you want from servlet
    //.......
    inputFromClient.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }

    }
    }

    }

    -------------------------------------------------------------------
    Servlet code


    package test;

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

    public class FileUpload extends HttpServlet
    {
    public void doPost(HttpServletRequest req,HttpServletResponse res)
    {

    ServletContext sc = this.getServletContext();
    try
    {
    String fileName = "C:\\NewToledo.txt";
    String path = fileName;

    File yourFile = new File(path);
    FileOutputStream toFile = new FileOutputStream( yourFile );
    DataInputStream fromClient = new DataInputStream( req.getInputStream() );

    byte[] buff = new byte[1024];
    int cnt = 0;
    while( (cnt = fromClient.read( buff )) > -1 ) {
    toFile.write( buff, 0, cnt );
    }
    toFile.flush();
    toFile.close();
    fromClient.close();

    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }





    here is the log info from resin

    Http[13] POST /servlet/MyServlet HTTP/1.1
    Http[13] Remote-IP: 127.0.0.1:3828
    Http[13] Cache-Control: no-cache
    Http[13] Pragma: no-cache
    Http[13] User-Agent: Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_07
    Host: localhost:8080
    Http[13] Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Http[13] Connection: keep-alive
    Http[13] Content-type: application/x-www-form-urlencoded
    Http[13] Content-Length: 20311
    Http[13] HTTP/1.1 404 Not Found
    Http[13] Content-Type: text/html; charset=utf-8
    Http[13] Transfer-Encoding: chunked
    Http[13] write-chunk(184)
    Http[13] write-chunk(7)
    Http[13] finish/keepalive
    Http[13] keepalive


    It appears to grab the content just not connect or pass content. I have tried to define the URL via multiple file separators no luck.

    any suggestions thanks for your time.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post write(int i) does the cast

    Quote Originally Posted by milkman128 View Post
    ...My problem is getting the applet to pass the information to the servlet.
    Me too.
    Quote Originally Posted by milkman128 View Post
    I have tried to define the URL via multiple file separators
    What does this mean?
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    import javax.swing.*;
    
    public class FileUploadApplet extends JApplet implements ActionListener
    {
        // 
        JButton jbutton = null;
        public void init()
        {
            jbutton = new JButton("Send file");
            jbutton.addActionListener(this);
            this.getContentPane().add(jbutton);
        }
        //
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource() == jbutton)
            {
                try
                {
                    FileInputStream in = new File("C:\\NewToledo.txt");
                    byte[] buf=new byte[in.available()];
                    int bytesread = 0;
                    // meaningless string for posting...
                    String toservlet = "..........";
                    //
                    URL servleturl = new URL(toservlet);
                    URLConnection servletconnection = servleturl.openConnection();
                    /**
                    * The default value of this field is <code>true</code>. 
                    */
                    servletconnection.setDoInput(true);
                    /**
                    * The default value of this field is <code>false</code>. 
                    */
                    servletconnection.setDoOutput(true);
                    if(servletconnection.getAllowUserInteraction())
                    {
                        servletconnection.setAllowUserInteraction(true);
                    }
                    servletconnection.setUseCaches(false);
                    servletconnection.setDefaultUseCaches(false);
                    //
                    DataOutputStream out = new DataOutputStream(servletconnection.getOutputStream());
                    //
                    while( (bytesread = in.read( buf )) > -1 )
                    {
                        // Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.
                        out.write(bytesread);
                    }
                    //
                    out.flush();
                    out.close();
                    in.close();
                    //...deletions for posting.
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
    Mods to file read, see docs for DataOutputStream
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. To transfer a file from client to server
    By phani in forum Networking
    Replies: 4
    Last Post: 10-12-2010, 07:15 PM
  2. data loss in the secure file transfer using java
    By antony75 in forum Networking
    Replies: 4
    Last Post: 02-09-2009, 07:56 AM
  3. Java File Transfer
    By lks0912 in forum Advanced Java
    Replies: 0
    Last Post: 10-23-2008, 01:53 PM
  4. Replies: 0
    Last Post: 08-30-2008, 12:52 PM
  5. File and Message transfer over sockets!
    By rameshraj in forum Networking
    Replies: 3
    Last Post: 05-14-2008, 09:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •