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

How to pass image byte in servlets

P: 26
i come up with this


This is my getters and setters in a java class.

Expand|Select|Wrap|Line Numbers
  1. private byte[] picture;
  2.  
  3. public Goal() {}
  4.  
  5. public Goal(byte[] picture) {
  6. this.picture = picture;
  7.  
  8. }
  9.  
  10. public byte[] getPicture() {
  11. return picture;
  12. }
  13. public void setPicture(byte[] picture) {
  14. this.picture = picture;
  15. }
  16.  


my problem is how do i pass image as byte to servlet request parameter eg
assuming i have
Expand|Select|Wrap|Line Numbers
  1. String picture = request.getParameter("picture");
  2.  
how do i code it as byte and pass it in servlet request. Is it like this
Expand|Select|Wrap|Line Numbers
  1. byte[] picture = null;
  2.  
  3.  
Jan 5 '13 #1
Share this Question
Share on Google+
6 Replies


100+
P: 185
Use commons file upload and Multi-part form.

If you have a client application that can pass the file as string, you should encode it using base 64 encoding.
Jan 5 '13 #2

P: 26
i tried this
String encoded = Base64.encodeFromFile("data/inputImage.png");

but it returns error method encodeFromFile not found[b]
Jan 7 '13 #3

100+
P: 185
I don't know what Base64 implementation you are using. You may use the open source version from Apache Commons codec library. Here's how your code might look like using Commons-codec:
Expand|Select|Wrap|Line Numbers
  1. byte[] binaryData = null;
  2. // read data from the file into binaryData
  3. Base64.encodeBase64(binaryData );
  4.  
You can find the library @ http://commons.apache.org/codec/
Jan 7 '13 #4

P: 26
what about the headers please
thanks
Jan 8 '13 #5

100+
P: 185
Which headers do you mean?!
Jan 8 '13 #6

P: 26
what about the headers please
thanks
Jan 8 '13 #7

Post your reply

Sign in to post your reply or Sign up for a free account.