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 7642943
In Process

The Archive Base Latest Questions

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

I want to upload one image to the database by using Google app engine

  • 0

I want to upload one image to the database by using Google app engine and JDO.
I got one code from the internet.

http://code.google.com/appengine/articles/java/serving_dynamic_images.html

I tried that example. But the url what I am passing to URLFetchService fetch method is throwing Malformed exception. So here what is this url value supposed to be expected.
I gave the value of url value as the file upload value from the jsp page.

Please find the code below:

package com.google.appengine.demo.domain;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
 * JDO-annotated model class for storing movie properties; movie's promotional
 * image is stored as a Blob (large byte array) in the image field.
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Movie {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String title;

    @Persistent
    @Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
    private String imageType;

    @Persistent
    private Blob image;



    public Long getId() {
        return key.getId();
    }

    public String getTitle() {
        return title;
    }

    public String getImageType() {
        return imageType;
    }

    public byte[] getImage() {
        if (image == null) {
            return null;
        }

        return image.getBytes();
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setImageType(String imageType) {
        this.imageType = imageType;
    }

    public void setImage(byte[] bytes) {
        this.image = new Blob(bytes);
    }

    //...
}

JSP

<html>

<head>
<title>Image Upload</title>
</head>

<body>
    <form action="/addMovie" method="get" enctype="multipart/form-data"
        name="productForm" id="productForm">
        <br>
        <br>
        <table width="400px" align="center" border=0
            style="background-color: ffeeff;">
            <tr>
                <td align="center" colspan=2
                    style="font-weight: bold; font-size: 20pt;">Image Details</td>
            </tr>

            <tr>
                <td align="center" colspan=2>&nbsp;</td>
            </tr>

            <tr>
                <td>Image Link:</td>
                <td><input type="file" name="url" ><td>
            </tr>

            <tr>
                <td></td>
                <td><input type="submit" name="Submit" value="Submit">
                </td>
            </tr>
            <tr>
                <td colspan="2">&nbsp;</td>
            </tr>

        </table>
    </form>
</body>

</html>

Servlet

package com.google.appengine.demo.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pmf.PMF;

import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
import com.google.appengine.demo.domain.Movie;

/**
 * GET requests fetch the image at the URL specified by the url query string
 * parameter, then persist this image along with the title specified by the
 * title query string parameter as a new Movie object in App Engine's
 * datastore.
 */
public class StoreMovieServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;



     public void doPost(HttpServletRequest request, HttpServletResponse response)
     throws IOException {


            PrintWriter out = response.getWriter();
            try {          
               String url1 = request.getParameter("url");           
               String url ="http://localhost:8877/addMovie?url="+url1;

             //  url = "C:\\my_photo.jpg";

               System.out.println("url:: +"+url);            
               URLFetchService fetchService =
                   URLFetchServiceFactory.getURLFetchService();
               HTTPResponse fetchResponse = fetchService.fetch(new URL(
                       request.getParameter(url1)));
               String fetchResponseContentType = null;
               for (HTTPHeader header : fetchResponse.getHeaders()) {
                   // For each request header, check whether the name equals
                   // "Content-Type"; if so, store the value of this header
                   // in a member variable
                   if (header.getName().equalsIgnoreCase("content-type")) {
                       fetchResponseContentType = header.getValue();
                       break;
                   }
               }     
               Movie movie = new Movie();
              // movie.setTitle(req.getParameter("title"));
               movie.setImageType(fetchResponseContentType);

               // Set the movie's promotional image by passing in the bytes pulled
               // from the image fetched via the URL Fetch service
               movie.setImage(fetchResponse.getContent());
               PersistenceManager pm = PMF.get().getPersistenceManager();
               try {
                   // Store the image in App Engine's datastore
                   pm.makePersistent(movie);
               } finally {
                   pm.close();
               }



            }catch(Exception ex){
                ex.printStackTrace();
            }
            finally {            
                out.close();
            }


     }



    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        PrintWriter out = response.getWriter();
        try {          
           String url1 = request.getParameter("url");           
           String url ="http://localhost:8877/addMovie?url="+url1;

         //  url = "C:\\my_photo.jpg";

           System.out.println("url:: +"+url);            
           URLFetchService fetchService =
               URLFetchServiceFactory.getURLFetchService();
           HTTPResponse fetchResponse = fetchService.fetch(new URL(
                   request.getParameter(url1)));
           String fetchResponseContentType = null;
           for (HTTPHeader header : fetchResponse.getHeaders()) {
               // For each request header, check whether the name equals
               // "Content-Type"; if so, store the value of this header
               // in a member variable
               if (header.getName().equalsIgnoreCase("content-type")) {
                   fetchResponseContentType = header.getValue();
                   break;
               }
           }     
           Movie movie = new Movie();
          // movie.setTitle(req.getParameter("title"));
           movie.setImageType(fetchResponseContentType);

           // Set the movie's promotional image by passing in the bytes pulled
           // from the image fetched via the URL Fetch service
           movie.setImage(fetchResponse.getContent());
           PersistenceManager pm = PMF.get().getPersistenceManager();
           try {
               // Store the image in App Engine's datastore
               pm.makePersistent(movie);
           } finally {
               pm.close();
           }



        }catch(Exception ex){
            ex.printStackTrace();
        }
        finally {            
            out.close();
        }

    }

    private void storeImage(HttpServletRequest req) throws IOException,
            MalformedURLException {
        URLFetchService fetchService =
            URLFetchServiceFactory.getURLFetchService();

        // Fetch the image at the location given by the url query string parameter
        HTTPResponse fetchResponse = fetchService.fetch(new URL(
                req.getParameter("url")));

        String fetchResponseContentType = null;
        for (HTTPHeader header : fetchResponse.getHeaders()) {
            // For each request header, check whether the name equals
            // "Content-Type"; if so, store the value of this header
            // in a member variable
            if (header.getName().equalsIgnoreCase("content-type")) {
                fetchResponseContentType = header.getValue();
                break;
            }
        }

        if (fetchResponseContentType != null) {
            // Create a new Movie instance
            Movie movie = new Movie();
            movie.setTitle(req.getParameter("title"));
            movie.setImageType(fetchResponseContentType);

            // Set the movie's promotional image by passing in the bytes pulled
            // from the image fetched via the URL Fetch service
            movie.setImage(fetchResponse.getContent());

            //...

            PersistenceManager pm = PMF.get().getPersistenceManager();
            try {
                // Store the image in App Engine's datastore
                pm.makePersistent(movie);
            } finally {
                pm.close();
            }
        }
    }

web.xml

<servlet>
    <servlet-name>StoreMovie</servlet-name>
    <servlet-class>com.google.appengine.demo.web.StoreMovieServlet</servlet-class>
</servlet>
<servlet>
    <servlet-name>GetImage</servlet-name>
    <servlet-class>com.google.appengine.demo.web.GetImageServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>StoreMovie</servlet-name>
    <url-pattern>/addMovie</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>GetImage</servlet-name>
    <url-pattern>/image</url-pattern>
</servlet-mapping>
  • 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-31T09:19:13+00:00Added an answer on May 31, 2026 at 9:19 am

    The sample code you posted is to fetch an image from a URL , whereas your code is trying to upload an image from your filesystem.

    The <input type="file"> means you are selecting this from your local system. This does not translate into a URL. You’re going to have upload this image to the App Engine and then do whatever you want with it.

    Here is a nice walkthrough of how to do that with the Apache Commons File Upload jars.

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

Sidebar

Related Questions

I have one audio file captured from my iphone. I want to upload this
I want to upload a list of users from my work's LDAP server to
I want to upload and store video files to my server using PHP. Could
I want to upload picture from my_form in jQuery, I tried submit() function it
I want to upload files directly to IIS7 (in this case I am using
i am using CI 2.1.0 and mysql database for one of my projects. i
hi thanks a lot i have one code below in this code ill upload
Okay, I have a question guys. I want to remote upload (copy an image
Here is the code I'm using to upload Multiple Photos with the HTML5 tag.
I want to receive multi file post from image uploader.( i use this )

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.