Hello Friends,
What I actually want to do is to send a file to a
servlet through an
applet and that servlet will deliver it to another applet. [ Sending file from one user to another in my chat program if user selects, send file button ]. Before doing this I want to write a program where an applets sends a file to a servlet which in turn sends the same file to the applet itself. I incorporated a TextArea in applet which logs every stage of its execution and telling that it has sent file successfully to servlet and there is no response from sertvlet. When I executed servlet which loads a textfile and delivers it to a client, it worked fine but I cannot figure out what is the case with this applet and servlet communication. For the time being inorder to access a file thru an applet I have given permission for applets in Local Intranet to access file in thru settings and its working fine in accesing a file. Below is My Applet and servlet part. Kindly help me in overcoming this problem. It's posing problems to me since two days.
/* Applet Code */
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class FileClient extends Applet {
TextArea ta;
public void init() {
ta = new TextArea();
add(ta);
ta.append("--- STARTING ----");
String servletURL = "http://localhost:8080/examples/servlet/FileServer";
URL url;
URLConnection con;
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
url = new URL(servletURL);
ta.append(":
pening Connection");
con = url.openConnection();
ta.append("::Getting Output Stream");
bos = new BufferedOutputStream(con.getOutputStream());
ta.append(":
pening File");
bis = new BufferedInputStream(new FileInputStream("c:\\give.txt"));
byte[] buff = new byte[2048];
int bytesread;
ta.append("::Reading File And Writing To Stream");
while(-1 != (bytesread=bis.read(buff,0,buff.length)) ) {
bos.write(buff,0,bytesread);
}
ta.append("::Finished Reading File And Writing To Stream");
ta.append("--- OVER ----");
}
catch(final Exception e) {
ta.append("::Caught Exception " + e);
}
finally {
try {
if(bis!=null)
bis.close();
if(bos!=null)
bos.close();
}
catch(Exception e) {
ta.append("::Caught Exception During Closing " + e);
}
}
}
}
/* Servlet Code */
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FileServer extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ServletInputStream in = req.getInputStream();
ServletOutputStream out = res.getOutputStream();
res.setContentType("application/octet-stream");
res.setHeader("content-disposition","attachment; filename=give");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(in);
bos = new BufferedOutputStream(out);
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");
throw e;
}
catch(final IOException e) {
System.out.println("Caught IO Exception");
throw e;
}
finally {
if(bis!=null)
bis.close();
if(bos!=null)
bos.close();
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doPost(req,res);
}
}