Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-13-2008, 01:41 AM
Member
 
Join Date: Oct 2008
Posts: 6
Rep Power: 0
milkman128 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-15-2008, 03:21 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 1,018
Rep Power: 3
Nicholas Jordan is on a distinguished road
Post write(int i) does the cast
Originally Posted by milkman128 View Post
...My problem is getting the applet to pass the information to the servlet.
Me too.
Originally Posted by milkman128 View Post
I have tried to define the URL via multiple file separators
What does this mean?
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
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
data loss in the secure file transfer using java antony75 Networking 4 02-09-2009 06:56 AM
Java File Transfer lks0912 Advanced Java 0 10-23-2008 12:53 PM
Is there any way to transfer the file Streams from one servlet to another servlet . HenaPriyadarsini Java Servlet 0 08-30-2008 11:52 AM
File and Message transfer over sockets! rameshraj Networking 3 05-14-2008 08:13 PM
To transfer a file from client to server phani Networking 1 04-16-2008 01:39 AM


All times are GMT +2. The time now is 03:02 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org