Package jmri.util

Class MultipartMessage

java.lang.Object
jmri.util.MultipartMessage

public class MultipartMessage
extends Object
Sends multi-part HTTP POST requests to a web server

Based on http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically


This file is part of JMRI.

JMRI is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. See the "COPYING" file for a copy of this license.

JMRI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

  • Constructor Details

    • MultipartMessage

      public MultipartMessage​(String requestURL, String charSet) throws IOException
      Constructor initialises a new HTTP POST request with content type set to 'multipart/form-data'.

      This allows for additional binary data to be uploaded.

      Parameters:
      requestURL - URL to which this request should be sent
      charSet - character set encoding of this message
      Throws:
      IOException - if OutputStream cannot be created
  • Method Details

    • addFormField

      public void addFormField​(String name, String value)
      Adds form field data to the request
      Parameters:
      name - field name
      value - field value
    • addFilePart

      public void addFilePart​(String fieldName, File uploadFile) throws IOException
      Adds an upload file section to the request. MIME type of the file is determined based on the file extension.
      Parameters:
      fieldName - name attribute in form <input name="{fieldName}" type="file" />
      uploadFile - file to be uploaded
      Throws:
      IOException - if problem adding file to request
    • addFilePart

      public void addFilePart​(String fieldName, File uploadFile, String fileType) throws IOException
      Adds an upload file section to the request. MIME type of the file is explicitly set.
      Parameters:
      fieldName - name attribute in form <input name="{fieldName}" type="file" />
      uploadFile - file to be uploaded
      fileType - MIME type of file
      Throws:
      IOException - if problem adding file to request
    • addHeaderField

      public void addHeaderField​(String name, String value)
      Adds a header field to the request
      Parameters:
      name - name of header field
      value - value of header field
    • finish

      public List<String> finish() throws IOException
      Finalise and send MultipartMessage to end-point.
      Returns:
      Responses from end-point as a List of Strings
      Throws:
      IOException - if problem sending MultipartMessage to end-point