亚马逊s3 putObject Excel流不工作(java)

时间:2016-10-20 18:26:07

标签: java excel amazon-web-services amazon-s3

您好我正在使用amazon s3 sdk for java而我正在尝试上传动态创建的excel。会发生什么情况我可以使用以下方法在本地计算机上保存文件:

//creating local file
    try {
        FileOutputStream fileOut = new FileOutputStream(new File("C:\\test.xls"));
        excelWorkBook.write(fileOut);
        fileOut.flush();
        fileOut.close();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

但是当我尝试通过流上传亚马逊s3时没有任何事情发生,我得到的错误字符串如

com.amazonaws.services.s3.model.AmazonS3Exception: Status Code: 400, AWS Service: Amazon S3, AWS Request ID: BC86F8AED980C7A3, AWS Error Code: RequestTimeout, AWS Error Message: Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed., S3 Extended Request ID: P1yiux9HeVH92ifelvhMfTyQ27Hgu3sC4xx8DXstO+HLizMera2OkYv3BSVCEJtxtFz10/SRbMk=
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:767)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:414)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:228)
    at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3316)
    at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1336)
    at com.sterling.smartdata.service.upload.impl.UploadReportImpl.uploadToCloud(UploadReportImpl.java:96)
    at com.sterling.smartdata.service.upload.impl.UploadCloudLocalImplTest.testUpload(UploadCloudLocalImplTest.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:85)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:86)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:241)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我在最近2天遇到困难而且我已经尝试过搜索解决方案,但我还没找到。

代码:

public void uploadToCloud() {
        AWSCredentials credentials = new BasicAWSCredentials(
                accessKey, 
                secretKey);

        AmazonS3 s3client = new AmazonS3Client(credentials);

        HSSFWorkbook excelWorkBook=new HSSFWorkbook();
        missingAbbreviationReport.createMissingChargeAbbreviationSheet(excelWorkBook);
        missingAbbreviationReport.createMissingDispositionAbbreviationSheet(excelWorkBook);

        //creating local file
        try {
            FileOutputStream fileOut = new FileOutputStream(new File("C:\\test.xls"));
            excelWorkBook.write(fileOut);
            fileOut.flush();
            fileOut.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        //upload to amazon s3
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            excelWorkBook.write(byteArrayOutputStream);
            byteArrayOutputStream.close();
        } catch (IOException e) {

            LOGGER.error(e.getMessage());
        }

        byte[] bf=new byte[byteArrayOutputStream.toByteArray().length];
        ByteArrayInputStream bi=new ByteArrayInputStream(bf);
        try {
            bi.read(byteArrayOutputStream.toByteArray());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        ObjectMetadata objectMetaData =new ObjectMetadata();
        objectMetaData.setContentType("application/vnd.ms-excel");
        objectMetaData.setContentLength(byteArrayOutputStream.toByteArray().length);

        s3client.putObject(new PutObjectRequest("smartdata-missingslg-reports", "test.xls",bi,objectMetaData) );

    }

1 个答案:

答案 0 :(得分:2)

Change the following 2 lines:-

byte[] bf=new byte[byteArrayOutputStream.toByteArray().length];
ByteArrayInputStream bi=new ByteArrayInputStream(bf);

to

ByteArrayInputStream bi= ByteArrayInputStream(byteArrayOutputStream.toByteArray());