• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

JPEG Image Upload

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ty! I solved the problem now

Thanks a lot
 
Could you hold this puppy for a sec? I need to adjust this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic