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

  • Home
  • SEARCH
  • 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 6834839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:09:38+00:00 2026-05-26T23:09:38+00:00

My web service hosted on Play! framework. I have few image files uploaded from

  • 0

My web service hosted on Play! framework. I have few image files uploaded from a non-play! framework based client using the standard HTTP client request with content-type of multipart/form-data.

On the web service side, I tried using Play! ApacheMultipartParser to parse the Http.request.body, but failed with the Java IO Bad File Descriptor exception.

The problem seems come from Java MultipartStream, by looking at the following callstack

    at java.io.FileInputStream.readBytes(Native Method)
    at java.io.FileInputStream.read(FileInputStream.java:208)
    at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:976)
    at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:886)
    at java.io.InputStream.read(InputStream.java:85)

I also tried directly reading the http.request.body into a big buffer for experiment, got the same exception. What could be wrong?

The http data sent out from client side is something like the following. On web service side, I could using IO.write to save it to a file w/o any problem.

Content-Type: multipart/form-data; boundary=--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f

--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f
Content-Disposition: form-data; name="foo1.jpg"; filename="foo1.jpg"
Content-Length: 5578
Content-Type: image/jpeg

<image data 1 omitted>
--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f
Content-Disposition: form-data; name="foo2.jpg"; filename="foo2.jpg"
Content-Length: 327
Content-Type: image/jpeg

<image data 2 omitted>
--3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f--
  • 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-26T23:09:39+00:00Added an answer on May 26, 2026 at 11:09 pm

    I had the exact same issue. The problem lies in the way that Play! handles multipart uploads. Usually you can add a FileUpload to your upload method and get your files there. This helps a lot as you can get the filenames and sizes and all this stuff directly from Play:

    public static void uploadFile(File fileUpload) {
      String name = fileUpload.getName()  // etc.
    }
    

    However using this logic prevents you from using the HTTPRequest. So if you use a non-Play way of uploading files (e.g with XMLHTTPRequest) where the automatic mapping to the fileUpload won’t work the following thing happens:

    • Play tries to bind the request to your arguments
    • Play encounters your File argument and parses the request.
    • Play finds nothing of use (as it doesn’t understand XMLHttpRequest) and maps your File argument to null.

    Now the request input stream has already been consumed by Play and you get your “Bad File Descriptor” message.

    The solution to this is, to not use any Play! magic, if you want to use the same method for uploading via Form and XMLHttpRequest (XHR). I wanted to use Valum’s file uploader script (http://github.com/valums/file-uploader) in addition to my own form based upload method. One uses XHR, the other uses plain multipart form uploads. I created the following method in my controller, that takes the uploaded file from the “qqfile” parameter and works with form based and XHR-Uploads:

    @SuppressWarnings({"UnusedDeclaration"})
    public static void uploadFile() {
        FileUpload qqfile = null;
        DataParser parser = DataParser.parsers.get(request.contentType);
        if (parser != null) {
            // normal upload. I have to manually parse this because
            // play kills the body input stream for XHR-requests when I put the file upload as a method
            // argument to {@link #uploadFile)
            parser.parse(request.body);
            @SuppressWarnings({"unchecked"})
            ArrayList<FileUpload> uploads = (ArrayList<FileUpload>) request.args.get("__UPLOADS");
            for (FileUpload upload : uploads) {
                if ("qqfile".equals(upload.getFieldName())) {
                    qqfile = upload;
                    break;
                }
            }
        } else {
            //  XHR upload
            qqfile = new FileUpload(new XHRFileItem("qqfile"));
        }
    
        if (qqfile == null) {
            badRequest();
            return;
        }
        // and now do something with your Fileupload object here (e.g. write it to db or something else) 
    }
    

    You probably can skip the IF-part of the if, if you split this method into two, so you can use the normal Play! magic for default uploads and use a separate method for your XHR uploads.

    I also had to create the XHRFileItem class which just wraps around a file item that is posted via an XMLHttpRequest. You might have to modify it a bit to work with multiple files and your particular file uploader, but nevertheless here it is:

    package application.util;
    
    import org.apache.commons.fileupload.FileItem;
    import org.jetbrains.annotations.Nullable;
    
    import java.io.*;
    
    import static play.mvc.Http.Request.current;
    
    /**
     * An implementation of FileItem to deal with XmlHttpRequest file uploads.
     */
    public class XHRFileItem implements FileItem {
    
        private String fieldName;
    
        public XHRFileItem(String fieldName) {
            this.fieldName = fieldName;
        }
    
        public InputStream getInputStream() throws IOException {
            return current().body;
        }
    
        public String getContentType() {
            return current().contentType;
        }
    
        public String getName() {
            String fileName = current().params.get(fieldName);
            if (fileName == null) {
                fileName = current().headers.get("x-file-name").value();
            }
            return fileName;
        }
    
        public boolean isInMemory() {
            return false;
        }
    
        public long getSize() {
            return 0;
        }
    
        public byte[] get() {
            return new byte[0];
        }
    
        public String getString(String s) throws UnsupportedEncodingException {
            return s;
        }
    
        public String getString() {
            return "";
        }
    
        public void write(File file) throws Exception {
            FileOutputStream fos = new FileOutputStream(file);
            InputStream is = getInputStream();
            byte[] buf = new byte[64000];
    
            int read;
            while ((read = is.read(buf)) != -1) {
                fos.write(buf, 0, read);
            }
            fos.close();
        }
    
        public void delete() {
    
        }
    
        public String getFieldName() {
            return fieldName;
        }
    
        public void setFieldName(String fieldName) {
            this.fieldName = fieldName;
        }
    
        public boolean isFormField() {
            return false;
        }
    
        public void setFormField(boolean b) {
    
        }
    
        @Nullable
        public OutputStream getOutputStream() throws IOException {
            return null;
        }
    }
    

    Hope this helps, it took me about a day to make this work on my end.

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

Sidebar

Related Questions

I have a WCF based web service hosted in windows sever 2003 machine. The
I have a self-hosted WCF web service running, and an Android client application. I
I have a .net web-service hosted in IIS 6.0 that periodically fails with an
Hi We developed web service using WCF service and hosted on Windows 2003 server
I have a web service that serves widgets. It is hosted on a server
I have a simple WCF Web service. It's hosted on IIS under the default
I have a web service (not a WCF service) hosted under IIS 7, the
When trying to make a web service (hosted on a different server) call from
I have a RESTful Web Service hosted in IIS 6.0, I am able to
A little up front info: I have a SOAP service (hosted using JAX-WS (Endpoint

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.