Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7670219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:46:19+00:00 2026-05-31T15:46:19+00:00

I am trying to write an Android application that will take a picture, put

  • 0

I am trying to write an Android application that will take a picture, put the data (byte[]) in an object along with some metadata, and post that to an AppEngine server where it will get persisted in a datastore as a blob. I don’t really want to save the image as a file on Android (unless it’s absolutely necessary). I searched around for solutions but nothing clear or specific enough came up. My questions are:

  1. how do I post the object to my servlet? Specifically how to properly serialize the object and get the serialized output to an HttpPost or something similar.
  2. once I persist the blob in the datastore, how will I be able to retrieve it as an image to display on a webpage?

Code examples would be very helpful. Also, if the approach I am taking is too complicated or problematic, please suggest other approaches (e.g. saving image as file, posting to server, then deleting).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:46:20+00:00Added an answer on May 31, 2026 at 3:46 pm

    You just need to do a Http-FileUpload which in a special case of a POST.
    There is no need to uuencode the file.
    No need to use a special lib/jar
    No need to save the object to disk (regardless that the following example is doing so)

    You find a very good explanation of Http-Command and as your special focus “file upload” under

    Using java.net.URLConnection to fire and handle HTTP requests

    The file upload sample from there follows (watch “send binary file”)
    and it is possible to add some companion data either


    String param = "value";
    File textFile = new File("/path/to/file.txt");
    File binaryFile = new File("/path/to/file.bin");
    String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
    String CRLF = "\r\n"; // Line separator required by multipart/form-data.
    
    URLConnection connection = new URL(url).openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    PrintWriter writer = null;
    try {
        OutputStream output = connection.getOutputStream();
        writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important!
    
        // Send normal param.
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
        writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
        writer.append(CRLF);
        writer.append(param).append(CRLF).flush();
    
        // Send text file.
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"textFile\"; filename=\"" + textFile.getName() + "\"").append(CRLF);
        writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
        writer.append(CRLF).flush();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(textFile), charset));
            for (String line; (line = reader.readLine()) != null;) {
                writer.append(line).append(CRLF);
            }
        } finally {
            if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
        }
        writer.flush();
    
        // Send binary file.
        writer.append("--" + boundary).append(CRLF);
        writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + binaryFile.getName() + "\"").append(CRLF);
        writer.append("Content-Type: " +     URLConnection.guessContentTypeFromName(binaryFile.getName()).append(CRLF);
        writer.append("Content-Transfer-Encoding: binary").append(CRLF);
        writer.append(CRLF).flush();
        InputStream input = null;
        try {
            input = new FileInputStream(binaryFile);
            byte[] buffer = new byte[1024];
            for (int length = 0; (length = input.read(buffer)) > 0;) {
                output.write(buffer, 0, length);
            }
            output.flush(); // Important! Output cannot be closed. Close of writer will close output as well.
        } finally {
            if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
        }
        writer.append(CRLF).flush(); // CRLF is important! It indicates end of binary boundary.
    
        // End of multipart/form-data.
        writer.append("--" + boundary + "--").append(CRLF);
    } finally {
        if (writer != null) writer.close();
    }
    

    Regarding the second part of your question. When successful uploading the file (I use apache common files), it is not a big deal to deliver a blob as an image.

    This is how to accept a file in a servlet

    public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse)
            throws ServletException, IOException {
        ServletFileUpload upload = new ServletFileUpload();
    
        try {
            FileItemIterator iter = upload.getItemIterator (pRequest);
    
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
    
                String fieldName = item.getFieldName();
    
                    InputStream stream = item.openStream();
                ....
                    stream.close();
                }
        ...
    

    And this code delivers an image

    public void doGet (HttpServletRequest pRequest, HttpServletResponse pResponse)  throws IOException {
    
        try {
            Blob img = (Blob) entity.getProperty(propImg);
    
            pResponse.addHeader ("Content-Disposition", "attachment; filename=abc.png");
            pResponse.addHeader ("Cache-Control", "max-age=120");
    
            String enc = "image/png";
            pResponse.setContentType (enc);
            pResponse.setContentLength (img.getBytes().length);
            OutputStream out = pResponse.getOutputStream ();
            out.write (img.getBytes());
            out.close();
    

    I hope this code fragments help to answer your questions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write an android application that will load a PDF as
I've been trying to write a little application that recognizes custom events in Android:
I am trying to write a very simple Android application that checks the signal
I am trying to write an application for Android that functions as a service
I am currently trying to write a class in Android that will Scan for
I'm trying to write an android application that has two main activities: login screen
I'm trying to write an application that will work well on all screen sizes,
I'm trying to write some JUnit tests for an Android application. I've read online
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
Trying to write a couple of functions that will encrypt or decrypt a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.