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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:04:51+00:00 2026-06-16T18:04:51+00:00

Am trying to Store a file with Apache File Upload. My JSP looks like

  • 0

Am trying to Store a file with Apache File Upload. My JSP looks like below,

<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit"value="upload" />
</form>

In my Servlet i can get the uploaded fie as below,

FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mime,fileName);
boolean lock = true;
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1 = is.read(b1, 0, BUFFER_SIZE);
while (readBytes1 != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, BUFFER_SIZE));}
writeChannel.closeFinally();

Now am trying to store the file as blob values using the below code,

 String blobKey = fileService.getBlobKey(file).getKeyString();
 Entity Input = new Entity("Input");
 Input.setProperty("Input File", blobKey);
 datastore.put(Input);

When i try this i can store the file name blob key but the file is not storing. It shows me “0” Bytes in the Blob viewer & Blob list of Google App engine.

Kindly Suggest me An idea to solve this problem,

Your help is appreciated.

My Servlet

public class UploadServlet extends HttpServlet{
  private static final long serialVersionUID = 1L;
  private static int BUFFER_SIZE =1024 * 1024* 10;
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
   ServletFileUpload upload = new ServletFileUpload();
   FileItemIterator iter; 
try {
iter = upload.getItemIterator(req);
 while (iter.hasNext()) {
   FileItemStream item = iter.next();
   String fileName = item.getName();
   String mime = item.getContentType();

   InputStream is = new BufferedInputStream(item.openStream());
    try {
         boolean isMultipart = ServletFileUpload.isMultipartContent(req);
         if( !isMultipart ) {
             resp.getWriter().println("File cannot be uploaded !");}
         else {
            FileService fileService = FileServiceFactory.getFileService();
            AppEngineFile file = fileService.createNewBlobFile(mime,fileName);
            boolean lock = true;
            FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
            byte[] b1 = new byte[BUFFER_SIZE];
            int readBytes1;
             while ((readBytes1 = is.read(b1)) != -1) {
               writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));}
               writeChannel.closeFinally();
            String blobKey = fileService.getBlobKey(file).getKeyString();
            Entity Input = new Entity("Input");
           Input.setProperty("Input File", blobKey);
           datastore.put(Input);}}
catch (Exception e) {
       e.printStackTrace(resp.getWriter());}
  }
}
  • 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-06-16T18:04:53+00:00Added an answer on June 16, 2026 at 6:04 pm

    You are reading the data from input stream in the wrong way. It should be:

    byte[] b1 = new byte[BUFFER_SIZE];
    int readBytes1;
    while ((readBytes1 = is.read(b1)) != -1) {
            writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes));
    }
    writeChannel.closeFinally();
    

    Update: you are not handling multipart correctly – it has multiple parts, you need to make sure you read the correct part (part with name “file”):

    public class UploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static int BUFFER_SIZE = 1024 * 1024 * 10;
    
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ServletFileUpload upload = new ServletFileUpload();
    
        boolean isMultipart = ServletFileUpload.isMultipartContent(req);
        if (!isMultipart) {
            resp.getWriter().println("File cannot be uploaded !");
            return;
        }
    
        FileItemIterator iter;
        try {
            iter = upload.getItemIterator(req);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                String fileName = item.getName();
                String fieldName = item.getFieldName();
                String mime = item.getContentType();
    
                if (fieldName.equals("file")) {  // the name of input field in html
                    InputStream is = item.openStream();
                    try {
                        FileService fileService = FileServiceFactory.getFileService();
                        AppEngineFile file = fileService.createNewBlobFile(mime, fileName);
                        boolean lock = true;
                        FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
                        byte[] b1 = new byte[BUFFER_SIZE];
                        int readBytes1;
                        while ((readBytes1 = is.read(b1)) != -1) {
                            writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));
                        }
                        writeChannel.closeFinally();
                        String blobKey = fileService.getBlobKey(file).getKeyString();
                        Entity input = new Entity("Input");
                        input.setProperty("Input File", blobKey);
                        datastore.put(input);
                    } catch (Exception e) {
                        e.printStackTrace(resp.getWriter());
                    }
                }
            }
        } catch (FileUploadException e) {
            // log error here
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to upload large file (>100Mb) using Apache commons-net FTP library to Secure
I am trying to store a file into a ftp server using apache commons
This the file I am trying to store in the database. I want to
I am trying to store a byteArrayInputStream as File on a FTP Server .
I am trying to store some integers in a file and I am storing
I'm trying to store data from a xml-file in a associative Array. Creating the
I'm trying to store some metadata in a .txt file using xattr on a
I am trying to store a multiple line e-mail in an ini file using
I am trying to store my Google Maps API Key in my application.ini file,
I am trying to read a file and store the information in unsigned char

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.