aspose file tools*
The moose likes Applets and the fly likes Sending A File From An Applet To A Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Sending A File From An Applet To A Servlet" Watch "Sending A File From An Applet To A Servlet" New topic
Author

Sending A File From An Applet To A Servlet

ch praveen
Ranch Hand

Joined: Mar 08, 2004
Posts: 67
Hi Friends,
I want to send a file to servlet from an applet which will be sent to another client. [ Sending a file from one user to other through a servlet in my chatprogram ]. Iam getting a security exception and being restricted from accessing a file through an applet. As Iam implementing an encryption mechanism , I must load a file, encrypt it and must send it to servlet, which will be sent to other user.
The below is the code for an applet(encryption not included now).Plz give me your valuable suggestions.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class DispatchFile extends Applet {
public void init() {
String servletURL = "http://localhost:8080/examples/servlet/DispatchFileServlet";
URL url;
URLConnection con;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
url = new URL(servletURL);
con = url.openConnection();
bos = new BufferedOutputStream(con.getOutputStream());
bis = new BufferedInputStream(new FileInputStream("give.txt"));
byte[] buff = new byte[2048];
int bytesread;
while(-1 != (bytesread=bis.read(buff,0,buff.length)) ) {
bos.write(buff,0,bytesread);
}
}
catch(final MalformedURLException e) {
System.out.println("Caught Malformed URL Exception");
}
catch(final IOException e) {
System.out.println("Caught IO Exception");
}
finally {
try {
if(bis!=null)
bis.close();
if(bos!=null)
bos.close();
}
catch(Exception e) { }
}
}
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sending A File From An Applet To A Servlet