在java中将文件上传到amazon s3时出现空指针异常

时间:2013-03-16 19:43:30

标签: java amazon-s3

当我从类FileXfer(从主要或来自其他地方)调用函数fileUpload时,它工作正常。但是当我从外部调用这个函数时,它会在upload = tx.upload(request)和函数被调用时给出一个空指针异常。 请建议我解决这个问题的方法。我尝试在此函数中使用tx定义,但它会产生一些其他错误。

import java.awt.BorderLayout;


import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
  import com.amazonaws.auth.AWSCredentials;
 import com.amazonaws.auth.PropertiesCredentials;
 import com.amazonaws.services.s3.model.GetObjectRequest;
   import com.amazonaws.services.s3.model.ProgressEvent;
     import com.amazonaws.services.s3.model.ProgressListener;
   import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
 import com.amazonaws.services.s3.transfer.Download;


public class FileXfer {


private static AWSCredentials credentials;
private static TransferManager tx;
private static String bucket;
private static String key;


private JProgressBar pb;
private JFrame frame;
private Upload upload;
private Download download;

public static void main(String[] args) throws Exception {
credentials = new PropertiesCredentials(FileXfer.class.getResourceAsStream("AwsCredentials.properties"));

    //transfer manager
 tx = new TransferManager(credentials);

 FileXfer upld = new FileXfer();

 // for testing
    //upld.createAmazonS3Bucket("aabrak12ujjia");
 upld.FileUpload("riti","D:/DM1/ec2trp.pem");
// upld.FileDownload("riti","ec2trp.pem","D:/DM1/"); 

  }

//for file download  
public void FileDownload( String Bname,String getkey, String destination) throws Exception {


    try{
        bucket=Bname; // bucket name

    key=getkey;  // file name to be downloaded

    frame = new JFrame("Saving a File");
    ProgressBar();
    ProgressTrackDownload progress = new ProgressTrackDownload();

    File fileToSave = new File(destination+key);

    GetObjectRequest request1 = new GetObjectRequest(bucket,key)
    .withProgressListener(progress.progressListener);
    download = tx.download(request1,fileToSave);

    System.out.println("Save as file: " + fileToSave.getAbsolutePath());

}catch (AmazonServiceException ase) {
    System.out.println("Caught an AmazonServiceException, which means your request made it "
            + "to Amazon S3, but was rejected with an error response for some reason.");
    System.out.println("Error Message:    " + ase.getMessage());
    System.out.println("HTTP Status Code: " + ase.getStatusCode());
    System.out.println("AWS Error Code:   " + ase.getErrorCode());
    System.out.println("Error Type:       " + ase.getErrorType());
    System.out.println("Request ID:       " + ase.getRequestId());
} catch (AmazonClientException ace) {
    System.out.println("Caught an AmazonClientException, which means the client encountered "
            + "a serious internal problem while trying to communicate with S3, "
            + "such as not being able to access the network.");
    System.out.println("Error Message: " + ace.getMessage());
 }
}


 // for file upload
public void FileUpload(String bucket, String source) throws Exception {


    try{
         frame = new JFrame("Amazon S3 File Upload");

    File fileToUpload= new File(source);

    ProgressBar();
    ProgressTrackUpload pro = new ProgressTrackUpload(); 

    PutObjectRequest request = new PutObjectRequest(
            bucket, fileToUpload.getName(), fileToUpload).withProgressListener(pro.progressListener);

    upload = tx.upload(request);



}catch (AmazonServiceException ase) {
    System.out.println("Caught an AmazonServiceException, which means your request made it "
            + "to Amazon S3, but was rejected with an error response for some reason.");
    System.out.println("Error Message:    " + ase.getMessage());
    System.out.println("HTTP Status Code: " + ase.getStatusCode());
    System.out.println("AWS Error Code:   " + ase.getErrorCode());
    System.out.println("Error Type:       " + ase.getErrorType());
    System.out.println("Request ID:       " + ase.getRequestId());
} catch (AmazonClientException ace) {
    System.out.println("Caught an AmazonClientException, which means the client encountered "
            + "a serious internal problem while trying to communicate with S3, "
            + "such as not being able to access the network.");
    System.out.println("Error Message: " + ace.getMessage());
  }
    }}

1 个答案:

答案 0 :(得分:0)

如果我不得不猜测,(您可能应该通过调试器运行代码),但是在您获得NullPointerException的情况下,tx为空。那是因为tx是一个在main中初始化的私有静态变量,但它似乎没有在其他地方设置。 (这个班级以外的任何地方都可以......它是私人的)。

我建议的是将tx设为私有final(实例)变量,该变量在构造时提供给FileXfer对象。 (你可以对你的其他一些变量做同样的事情......遵循更多的Dependency Injection pattern而不是使用静态字段传递。你会发现你的代码更容易推理这种方式)。

缩写代码:

// or elsewhere of course...
public static void main(String... args) {
  TransferManager manager = new TransferManager(credentials);
  FileXfer upld = new FileXfer(manager);
  upld.fileUpload....
}

public class FileXfer {
  private final TransferManager tx;
  ...

  public FileXfer(TransferManager tx) {
    //ensure that tx is not null.
    if(tx == null) { throw new NullPointerException("tx"); }
    this.tx = tx;
  }

  ...
  public void FileUpload(...) {
    ...
    upload=tx.upload(request);
    ...
  }
}