JavaFX: File upload to REST service / servlet fails because of missing boundary

Posted by spa on Stack Overflow See other posts from Stack Overflow or by spa
Published on 2010-04-21T19:29:20Z Indexed on 2010/04/21 19:33 UTC
Read the original article Hit count: 265

Filed under:
|
|

I'm trying to upload a file using JavaFX using the HttpRequest. For this purpose I have written the following function.

function uploadFile(inputFile : File) : Void {
    // check file
    if (inputFile == null or not(inputFile.exists()) or inputFile.isDirectory()) {
        return;
    }    
    def httpRequest : HttpRequest = HttpRequest {
        location: urlConverter.encodeURL("{serverUrl}");
        source: new FileInputStream(inputFile)
        method: HttpRequest.POST
        headers: [
            HttpHeader {
                name: HttpHeader.CONTENT_TYPE
                value: "multipart/form-data"
            }
        ]
    }
    httpRequest.start();
}

On the server side, I am trying to handle the incoming data using the Apache Commons FileUpload API using a Jersey REST service. The code used to do this is a simple copy of the FileUpload tutorial on the Apache homepage.

@Path("Upload")
public class UploadService {

  public static final String RC_OK = "OK";
  public static final String RC_ERROR = "ERROR";

  @POST
  @Produces("text/plain")
  public String handleFileUpload(@Context HttpServletRequest request) {
    if (!ServletFileUpload.isMultipartContent(request)) {
      return RC_ERROR;
    }
    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = null;
    try {
      items = upload.parseRequest(request);
    } 
    catch (FileUploadException e) {
      e.printStackTrace();
      return RC_ERROR;
    }
    ...
  }
}   

However, I get a exception at items = upload.parseRequest(request);: org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

I guess I have to add a manual boundary info to the InputStream. Is there any easy solution to do this? Or are there even other solutions?

© Stack Overflow or respective owner

Related posts about javafx

Related posts about multipart