Search code examples
javaibm-cloudobject-storage

bluemix object storage file uploading successfully, but 0kb file is uploading on object storage using java


I am using the Bluemix Object Storage service and Java to upload a file to Object Storage through coding. Here is my code snipet for uploading the file to Object Storage:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ObjectStorageService objectStorage = authenticateAndGetObjectStorageService();

    System.out.println("Storing file in post ObjectStorage...0508");

    String containerName = request.getParameter("container");

    String fileName = request.getParameter("file");
    System.out.println("containerName in post: "+containerName +"And file name"+fileName);

    if(containerName == null || fileName == null){ //No file was specified to be found, or container name is missing
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        System.out.println("File not found.");
        return;
    }

    try {


    final InputStream fileStream = request.getInputStream();
    System.out.println("fileStream: "+fileStream);
    Payload<InputStream> payload = new PayloadClass(fileStream);

    objectStorage.objects().put(containerName, fileName, payload);

    System.out.println("Successfully stored file in ObjectStorage!");
    } catch (Exception e) {
        System.out.println("Exception in uploaidng +"+e.toString());

        // TODO: handle exception
    }
}

However, a 0kb file is uploaded to Object Storage.

Object Storage screenshot


Solution

  • The only way I could make it work was to put the MultiPart annotation on the Servlet

     @WebServlet("/objectStorage")
    @MultipartConfig
    public class SimpleServlet extends HttpServlet
    

    And getting the part instead of the whole inputStream:

     Part filePart = request.getPart("fileToUpload"); // Retrieves <input type="file" name="fileToUpload">  
     InputStream fileContent = filePart.getInputStream();
    

    And on the html form set the enctype to multipart/formdata

    <form action="objectStorage" method="post" enctype="multipart/form-data">
    

    My whole code was that way:

    HTML:

    <form action="objectStorage" method="post" enctype="multipart/form-data">
                Select image to upload:
                <input type="file" name="fileToUpload" id="fileToUpload">  
                <input type="text" name="nome" id="nome">
                <input type="submit" value="Upload Image" name="submit">
     </form>
    

    Java code:

    @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            ObjectStorageService objectStorage = authenticateAndGetObjectStorageService();
             System.out.println("Storing file in ObjectStorage...");
            String containerName = "abc";
            String fileName = request.getParameter("nome");
    
             if (containerName == null || fileName == null) { //No file was specified to be found, or container name is missing
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                System.out.println("File not found.");
                return;
            }
    
            try {
    
                Part filePart = request.getPart("fileToUpload"); // Retrieves <input type="file" name="file">  
                InputStream fileContent = filePart.getInputStream();
                Payload<InputStream> payload = new PayloadClass(fileContent);
    
                 objectStorage.objects().put(containerName, fileName,  payload);
    
                System.out.println("Upload Successful");
            } catch (Exception e) {
                System.out.println(e);
            }
    
        }