Package jmri.util

Class MultipartMessage


  • public class MultipartMessage
    extends java.lang.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 Summary

      Constructors 
      Constructor Description
      MultipartMessage​(java.lang.String requestURL, java.lang.String charSet)
      Constructor initialises a new HTTP POST request with content type set to 'multipart/form-data'.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFilePart​(java.lang.String fieldName, java.io.File uploadFile)
      Adds an upload file section to the request.
      void addFilePart​(java.lang.String fieldName, java.io.File uploadFile, java.lang.String fileType)
      Adds an upload file section to the request.
      void addFormField​(java.lang.String name, java.lang.String value)
      Adds form field data to the request
      void addHeaderField​(java.lang.String name, java.lang.String value)
      Adds a header field to the request
      java.util.List<java.lang.String> finish()
      Finalise and send MultipartMessage to end-point.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MultipartMessage

        public MultipartMessage​(java.lang.String requestURL,
                                java.lang.String charSet)
                         throws java.io.IOException,
                                java.net.URISyntaxException
        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:
        java.io.IOException - if OutputStream cannot be created
        java.net.URISyntaxException - if the requestURL has wrong syntax
    • Method Detail

      • addFormField

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

        public void addFilePart​(java.lang.String fieldName,
                                java.io.File uploadFile)
                         throws java.io.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:
        java.io.IOException - if problem adding file to request
      • addFilePart

        public void addFilePart​(java.lang.String fieldName,
                                java.io.File uploadFile,
                                java.lang.String fileType)
                         throws java.io.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:
        java.io.IOException - if problem adding file to request
      • addHeaderField

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

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