Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2005

Tip: Looking for answers? Try searching our database.

[Applet] Image-Upload to Server via Applet+PHP

Thread view: 
Roland Poellinger - 11 Mar 2005 09:20 GMT
hello!

i know that this is a question that comes up very often - but i have never
found a complete working modifiable example, therefore i am posting this
question here again:

right now i am working on an online paint-program as java applet (where you
can choose pencil shape, choose color, hit one single button "save"). the
"save" button should send the java intern format Image or BufferedImage over
to a php-script as jpeg/gif/png. the php-script is the same that i am using
for image upload from a html/php-form in my cms (the picture has a fixed
destination - it is always the same location - simply overwriting the old
picture). the applet and the script are in the same folder on the same
server.
maybe somebody knows a configurable source example!?
i enclosed a bit of code from another forum about which somebody might be
able to tell me what would have to be modified to be able to send
Image/BufferedImage to php.
thankfully -
roland

EXAMPLE FOR TEXT FROM APPLET TO PHP:
===================================

import java.net.*;
import java.io.*;
public class srvHead implements Runnable{
String c = "";

public srvHead(){
new Thread(this).start();
}
public void run(){
try{
// run this as an application first, when it works than make an applet of it
URL u = new URL("http://yourserver/yourpage.php");
URLConnection c = u.openConnection();
// post multipart data
c.setDoOutput(true);
c.setDoInput(true);
c.setUseCaches(false);
// set some request headers
c.setRequestProperty("Connection", "Keep-Alive");
// TODO: get codebase of the this (the applet) to use for referer
c.setRequestProperty("HTTP_REFERER", "http://applet.getcodebase");
c.setRequestProperty("Content-Type", "multipart/form-data;
boundary=****4353");
DataOutputStream dstream = new DataOutputStream(c.getOutputStream());
// TODO: next line sends form data like a text input, to send binary data I
have too look in to the request protocol for multipart form data
// TODO: when sending a file a FileInputStream needs to be opened
(Jfilechooser ?)
dstream.writeBytes("--****4353\r\nContent-Disposition: form-data;
name=\"uniqueURL\"\r\n\r\n1086774178716\r\n--****4353--\r\n\r\n");
dstream.flush();
dstream.close();
// reading all the header fields of the response
try{
int i = c.getHeaderFields().size()-1;
while(i>-1){
try{
System.out.print(c.getHeaderFieldKey(i));
System.out.print("=");
System.out.println(c.getHeaderField(i));
}catch(Exception e){
}
i--;
}
}catch(Exception e){
}
try{
DataInputStream in =
new DataInputStream(
new BufferedInputStream(c.getInputStream()));
String sIn = in.readLine();
boolean b = true;
// TODO: this will loop forever unless you make sure your server page
// sends a last line like "I am done"
// than you can do wile(sIn.compareTo("I am done")!=0){
while(b){
if(sIn!=null){
System.out.println(sIn);
}
sIn = in.readLine();
}
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
new srvHead();
}
}
===============================================
Ryan Dillon - 12 Mar 2005 07:23 GMT
I would recommend using Jakarta Commons HttpClient:
http://jakarta.apache.org/commons/httpclient/
which makes things like this fairly trivial.

However, if you are trying to minimize the applet jar size,
then have a look at the example half way down this page:
http://www.jguru.com/faq/view.jsp?EID=62798
It seems to do what you are wanting.

Cheers

--
Ryan Dillon
Code Canvas Technologies
Sign up for the RapidJ beta!
http://www.codecanvas.com.au/rapidj/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.