0

Bluemix Object Storage サービスと Java を使用して、コーディングによってファイルを Object Storage にアップロードしています。以下は、ファイルを 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
    }
}

ただし、0kb ファイルは Object Storage にアップロードされます。

オブジェクトストレージのスクリーンショット

4

2 に答える 2

0

InputStream からバイナリを取得するには、getData() を使用してください。以下は openstack4j getData ドキュメントです。

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

于 2016-08-05T21:11:32.130 に答える
0

私がそれを機能させる唯一の方法は、サーブレットに MultiPart アノテーションを配置することでした

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

そして、inputStream 全体ではなく一部を取得します。

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

そして、html フォームで enctype を multipart/formdata に設定します。

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

私のコード全体はそのようでした:

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 コード:

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

    }
于 2016-08-23T11:43:04.240 に答える