简体   繁体   中英

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

Please use getData() in order to pull the binary from the InputStream. Below are openstack4j getData docs.

http://www.openstack4j.com/javadoc/org/openstack4j/api/sahara/JobBinaryInternalService.html#getData-java.lang.String-

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);
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM