By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,553 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,553 IT Pros & Developers. It's quick & easy.

Applet should write JPG-Image to Webserver

P: n/a
hi everyone,

i have a big problem, writing a image from my applet to my apache
webserver. i tried three way's of writing that file. every way was
described in forums to solve this problem, but non of them worked and
i don't know why. i'll give you the code of my writing-methods and
describe, what happen when i test them, in order someone of you can
give me a usefull tip, where the problem is.

as inputparameter i give my method a new URL referring to
http://localhost/test.jpg (this is the same directory, where my applet
is loaded from, so i should have reading and writing permission,
havn't i? while i'm developing, my applet runs on the same pc as my
webserver, just in case you're wondering about localhost) and a
selfmade BufferedImage (i already testet if it is not null and shows
the correct things ... all ok).

1. try:

private void writeImageToServer(URL fileURL,BufferedImage img){
try {
URLConnection urlConnection = fileURL.openConnection();
urlConnection.setDoOutput(true);

OutputStream urlout = urlConnection.getOutputStream();

BufferedOutputStream out = new BufferedOutputStream(urlout);

ImageIO.write(img,"jpg",out);

out.close(); // i also tried without this line -> same result
// additionally a question: do i need
out.close()?
}
catch( IOException e ){
e.printStackTrace();
}
}

result:
test.jpg doesn't appear in the webroot. but some very strange messages
in the error.log of my apacheserver:

[Tue Jun 08 11:40:22 2004] [error] [client 127.0.0.1] File does not
exist: c:/programme/apache
group/apache/htdocs/meta-inf/services/javax.imageio.spi.ImageOutputStreamSpi
[Tue Jun 08 11:40:22 2004] [error] [client 127.0.0.1] File does not
exist: c:/programme/apache
group/apache/htdocs/meta-inf/services/javax.imageio.spi.ImageReaderSpi
[Tue Jun 08 11:40:22 2004] [error] [client 127.0.0.1] File does not
exist: c:/programme/apache
group/apache/htdocs/meta-inf/services/javax.imageio.spi.ImageInputStreamSpi
[Tue Jun 08 11:40:22 2004] [error] [client 127.0.0.1] File does not
exist: c:/programme/apache
group/apache/htdocs/meta-inf/services/javax.imageio.spi.ImageWriterSpi
[Tue Jun 08 11:40:22 2004] [error] [client 127.0.0.1] File does not
exist: c:/programme/apache
group/apache/htdocs/meta-inf/services/javax.imageio.spi.ImageTranscoderSpi

i cannot explain this lines to myself, because my apache should have
nothing to do with java. all my javacode is executed on the client
side in the browser. do this messages mean i have to add the ImageIO
package from the sdk to my jar-applet. the jre, used by my iexplorer,
doesn't contain this files in the meta-inf/services directory of
rt.jar, but that's version 1.4.2_03, the same as my sdk, and the
rt.jar contains the corresponding classfiles at javax.imageio.spi. so
i'm realy confused by this messages.

2. try:

private void writeImageToServer(URL fileURL,BufferedImage img){
try {
URLConnection urlConnection = fileURL.openConnection();
urlConnection.setDoOutput(true);

OutputStream urlout = urlConnection.getOutputStream();

BufferedOutputStream out = new BufferedOutputStream(urlout);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(img);

out.close(); // same comments as above
}
catch( IOException e ){
e.printStackTrace();
}
}

result:
nothing. no error-messages in the error.log, no exceptions in the
java-console and no test.jpg in the webroot. i searched my whole
harddrives for it: nothing. isn't this the way, the JPEGImageEncoder
works?
3. try:

private void writeImageToServer(URL fileURL,BufferedImage img){
try {

File file = new File(fileURL.toString);
file.createNewFile();

BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(file));

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(img);
out.close(); // same comments as above
}
catch( Exception e ){
e.printStackTrace();
}
}

result:
the SecurityManager denies this action with "access denied" while
calling createNewFile(). well, this way was dedicated to run from an
application, not from an applet. i'd have to sign my applet to get the
rights to do this, or i can edit java.policy on my client, what i
don't want, because i cannot do this on every client, the applet will
run, when i'm finished with it. this brings me to the question: does
anybody know's how to sign my applet and give it full access to the
harddrive and the webserver without paying 400$ to VeriSign for a
commercial CA? i want to do this by myself, without paying anything
and without giving a lot of information to another company.
i would realy appreciate, if someone could give me a hint where i am
wrong or how to do this correct.

thank you very much

wayne
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Wayne,

You seem to have a fundamental misunderstanding about web servers. You
cannot simply open a URL to a web server and send it a file; this would
be a huge security hole. (Note that HTTP may provide some commands for
uploading files but most web servers do not implement them.) If you
want to upload files to your web server, you must augment the web server
with some sort of CGI program. If you would like to do this in Java,
look into servlets and/or JSP pages. On the client side, you have some
work to do as well; you will have to format the request in a way the
server will understand it. Exactly how will depend on what server-side
technology you select, but I suspect the best way will be to use the
HTTP client library from Apache.

Ray
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.