File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Applets and the fly likes sending image file from applet to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "sending image file from applet to servlet" Watch "sending image file from applet to servlet" New topic
Author

sending image file from applet to servlet

A Bhat
Greenhorn

Joined: May 05, 2009
Posts: 7
I have seen several posts in this forum and couldn't find out that whether am supposed to send serialised object,multipart stream or octet stream as content type when transferring image from applet to servlet.I have done this applet to servlet variable value transfer.But while transferring images am not at all getting a clue.I need the image file to be in fileformat at the server's end.
This what is my code for sending a variable(password) content to servlet from applet.Can you tell what are the changes in this code i shall do for sending image file to servlet from applet??

try {
// get input data for sending
String input = pass.getText();

// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
oos.flush();
oos.close();

// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();

// show result
//JOptionPane.showMessageDialog(aComponent,result);
if (result.equalsIgnoreCase("You are wrong try again")) {
jLabel11.setText(result);

} else {
// jLabel11.setText(result);
givedetails();
}


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


//THIS IS THE FUNCTION THAT I USED ABOVE FOR CONNECTION
private URLConnection getServletConnection()
throws MalformedURLException, IOException {


URL urlServlet = new URL(getCodeBase(), "APP");
URLConnection con = urlServlet.openConnection();


con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(
"Content-Type",
"application/x-java-serialized-object");


return con;
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41800
    
  62
If you want to use serialization, then you can serialize the byte[] that holds the image data. Then you can store that to a file using FileOutputStream on the server, assuming that the byte[] holds the data in a format that corresponds to a valid image file format.


Ping & DNS - my free Android networking tools app
A Bhat
Greenhorn

Joined: May 05, 2009
Posts: 7
Which process will be easiest one??serialisation?? if so how will i store a content of a image file into byte array
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41800
    
  62
Are you saying that on the client side you have a file as well? If so, is this a signed applet (it would need to be in order to handle files)?

Then I'd use an HTTP file upload; on the client side you can use the Apache Commons HttpClient library, while the server side is handled by the Apache Commons FileUpload library.
A Bhat
Greenhorn

Joined: May 05, 2009
Posts: 7
yes the file is in the client side .i have used policy tool to set the permission to my localhost.But how will i use this library.I have dis library.I don't know how to use it.Little code will be expected.If you don't mind.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41800
    
  62
I'd start by reading the documentation (extensive for both libraries) and http://faq.javaranch.com/java/FileUpload, which actually links to an article showing both libraries in action.
A Bhat
Greenhorn

Joined: May 05, 2009
Posts: 7
thanks.will luk at it.lets c
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sending image file from applet to servlet