File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes JPEG Image Upload Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "JPEG Image Upload" Watch "JPEG Image Upload" New topic
Author

JPEG Image Upload

Peter Wolfenstein
Greenhorn

Joined: Oct 23, 2006
Posts: 11
Hello!

I am trying to send a HTTP-Upload Request to a PHP-Script on a Webserver.

Basically what I am doing atm is:
1) Read the jpeg image into a byte array
2) Create a HTTP-Request
3) Send the JPEG bytewise to the Server

Here is my Code of the uploading method:


The jpeg image gets uploaded and stored in a folder by the PHP Script, however I can'T open the image after the upload on the server as I always get the message that it is corrupt....

Do you have any hints what I am doing wrong?

Thank you!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
Using a Writer (like BufferedWriter) is not appropriate for sending binary files (like images). You need to use the stream classes, or work with conn.getOutputStream() directly.


Ping & DNS - updated with new look and Ping home screen widget
Peter Wolfenstein
Greenhorn

Joined: Oct 23, 2006
Posts: 11
Does this mean i have to send the whole HTTP header bytewise?
I tried to send only the image with OutputStream and the rest with BufferedWriter, whoever then the whole fileupload seems to get crippled and the php script does not even recognize that a file upload is requested.

You need to use the stream classes, or work with conn.getOutputStream() directly.


Instead of the for loop i am using the following line of code now.



However this doesn't get it to work as well. I guess i have to write the whole HTTP-Request with the same writer/Stream.

So I need to somehow convert my HTTP-Request to a byte array and send it this way?

TY for you help. Sample Code would be much appreciated
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
Indeed, you can't mix using a stream with using a Writer based on that stream.

Instead of implementing all the details myself, I'd use the HttpClient library. There's even an example app that shows how to use it for file upload.
Peter Wolfenstein
Greenhorn

Joined: Oct 23, 2006
Posts: 11
Ty! I solved the problem now

Thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPEG Image Upload
 
Similar Threads
Strange "for" behaviour in Java 1.5
UNIX line break problem
delete and insert chars inside a along string
content-length
Input redirection in Java