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.
write(int i) does the cast
Quote:
Originally Posted by
milkman128
...My problem is getting the applet to pass the information to the servlet.
Me too.
Quote:
Originally Posted by
milkman128
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