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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:39:55+00:00 2026-06-15T22:39:55+00:00

My project contains only simple file upload form and the redirect to view it.

  • 0

My project contains only simple file upload form and the redirect to view it. I can upload any files less than 50 KB but when I upload some files larger than 100 KB, it throws IO Exception from app engine. I am not sure where the problem is. Do anyone know how to increase the file size that I can upload to Google Cloud storage through the form posting in app engine? file size properly less than 10 MB is OK. Any code snippets would be vhighly appreciated. Thank you.

java.io.IOException
    at com.google.appengine.api.files.FileServiceImpl.translateException(FileServiceImpl.java:615)
    at com.google.appengine.api.files.FileServiceImpl.makeSyncCall(FileServiceImpl.java:588)
    at com.google.appengine.api.files.FileServiceImpl.append(FileServiceImpl.java:535)
    at com.google.appengine.api.files.FileServiceImpl.append(FileServiceImpl.java:289)
    at com.google.appengine.api.files.FileWriteChannelImpl.write(FileWriteChannelImpl.java:57)
    at com.google.appengine.api.files.FileWriteChannelImpl.write(FileWriteChannelImpl.java:46)
    at java.nio.channels.Channels.write(Channels.java:80)
    at java.nio.channels.Channels.access$000(Channels.java:64)
    at java.nio.channels.Channels$1.write(Channels.java:151)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
    at storefile.StorageService.storeFile(StorageService.java:46)
    at storefile.UploadServlet.doPost(UploadServlet.java:46)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Following is my code:
upload.html

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Hello App Engine</title>
</head>

<body>
    <h1>Hello App Engine!</h1>
    <form action="/upload" method="post" enctype="multipart/form-data">
        <p>File<input type="file" name="file" /> </p>
        <p> <input type="submit"value="upload" /> <input type="reset" value="reset"/> </p>
    </form>
  </body>
</html>

uploadServlet.java

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

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

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.google.appengine.api.blobstore.BlobKey;


public class UploadServlet extends HttpServlet{

   private static final long serialVersionUID = 1L;
  private StorageService storage = new StorageService();
  private static int BUFFER_SIZE =1024 * 1024* 10;
  @Override
   public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {

      resp.setContentType("text/plain");
      resp.getWriter().println("Now see here your file content, that you have uploaded on storage..");

      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();

            storage.init(fileName, mime);
            InputStream is = new BufferedInputStream(item.openStream());

             byte[] b = new byte[BUFFER_SIZE];
            int readBytes = is.read(b, 0, BUFFER_SIZE);

            while (readBytes != -1) {
                storage.storeFile(b, readBytes);
                readBytes = is.read(b, 0, readBytes);
            }

             is.close();
            storage.destroy();

       resp.getWriter().println("File uploading done");

            // resp.getWriter().println("READ:" + storage.readTextFileOnly(fileName));
            BlobKey key = storage.getBlobkey(fileName);
            if (key != null) {
                resp.sendRedirect("/serve?blob-key=" + key.getKeyString());
            } else {
                resp.sendRedirect("/");
            }


      }
   } catch (Exception e) {
    e.printStackTrace(resp.getWriter());
    System.out.println("Exception::"+e.getMessage());
    e.printStackTrace();
   }
 }

}

StorageService.java

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.nio.channels.Channels;
import java.util.logging.Logger;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileReadChannel;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;
import com.google.appengine.api.files.FileWriteChannel;
import com.google.appengine.api.files.GSFileOptions.GSFileOptionsBuilder;

public class StorageService {

public static final String BUCKET_NAME = "aaaa";  

private FileWriteChannel writeChannel = null;
FileService fileService = FileServiceFactory.getFileService();

private BufferedOutputStream bos = null;
private static final Logger log = Logger.getLogger(StorageService.class.getName());
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
public void init(String fileName, String mime) throws Exception {
 System.out.println("Storage service:init() method:  file name:"+fileName+" and mime:"+mime);
 log.info("Storage service:init() method:  file name:"+fileName+" and mime:"+mime);

    GSFileOptionsBuilder builder = new GSFileOptionsBuilder()
            .setAcl("public_read")
            .setBucket(BUCKET_NAME)
            .setKey(fileName)
            .setMimeType(mime);  
    AppEngineFile writableFile = fileService.createNewGSFile(builder.build());

    boolean lock = true;
    writeChannel = fileService.openWriteChannel(writableFile, lock);
    bos = new BufferedOutputStream(Channels.newOutputStream(writeChannel));
}

public void storeFile(byte[] b, int readSize) throws Exception { 
    bos.write(b,0,readSize);
    bos.flush();
}

    public void destroy() throws Exception {
     log.info("Storage service: destroy() method");
        bos.close();
        writeChannel.closeFinally();
    }
    public BlobKey getBlobkey (String filename) {
        BlobKey bk = blobstoreService.createGsBlobKey("/gs/aaaa/"+filename);
        return bk;
    }

}

fileserve.java

import java.io.IOException;

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

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

public class fileserve extends HttpServlet {
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException {
        BlobKey blobKey = new BlobKey(req.getParameter("blob-key"));
        blobstoreService.serve(blobKey, res);
    }
}

web.xml

<?xml version="1.0" encoding="utf-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>upload</servlet-name>
        <servlet-class>storefile.UploadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>upload</servlet-name>
        <url-pattern>/upload</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>serve</servlet-name>
        <servlet-class>storefile.fileserve</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>serve</servlet-name>
        <url-pattern>/serve</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
  • 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-15T22:39:56+00:00Added an answer on June 15, 2026 at 10:39 pm

    Try changing the second is.read from:

    readBytes = is.read(b, 0, readBytes);
    

    to:

    readBytes = is.read(b, 0, BUFFER_SIZE);
    

    If at some point you are reading faster then data is available, readBytes is set to 0 and it stays forever at this value, because it’s basically doing is.read(b, 0, 0).

    Even better – you should check if there is data to be written:

    while (readBytes != -1) {
        if(readBytes > 0)
            storage.storeFile(b, readBytes);
        readBytes = is.read(b, 0, BUFFER_SIZE);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sample project from the NetBeans tutorials set contains only one Swing form (empty JFrame),
my project contains navigation controller and segmented control (with separate view controller: segmentManagingViewController) programmatically
My project directory contains a CMakeLists.txt file and src and include directories at its
I have a project that contains xml files. I also have an Ant build
In my project, user will upload videos. So i have done simple code to
I have a view that contains a UIScrollView. The UIScrollView contains an UIImageView (only
A sample Spring project contains the following POM file: <?xml version=1.0 encoding=UTF-8 standalone=no?> <project
I am building an Xcode project from console over ssh (I can use only
i made a windows service & add project installer.in which only contain this code.
My project contains shared library and exe client. I found that my own exception

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.