当我从 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());
}
}}