File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPEG Image Upload

 
Peter Wolfenstein
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Wolfenstein
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ty! I solved the problem now

Thanks a lot
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic