Java 从AmazonS3下载对象时,我无法将其下载到Different文件夹,而不是文件上载路径
从amazon s3下载对象时,我无法将其下载到Different文件夹,而无法下载文件上载路径…为什么会出现这种情况可能是因为元数据问题…请提前发布您的宝贵意见,谢谢..下面我将发布上载和下载代码Java 从AmazonS3下载对象时,我无法将其下载到Different文件夹,而不是文件上载路径,java,amazon-s3,Java,Amazon S3,从amazon s3下载对象时,我无法将其下载到Different文件夹,而无法下载文件上载路径…为什么会出现这种情况可能是因为元数据问题…请提前发布您的宝贵意见,谢谢..下面我将发布上载和下载代码 public void AmazonUpload(String fileObj) throws IOException { try { this.key = fileObj; try { if (thi
public void AmazonUpload(String fileObj) throws IOException {
try {
this.key = fileObj;
try {
if (this.key == null) {
} else {
if (readFile(this.key) != null) {
// this.key="1";
this.putObjResult = this.amzObj.putObject(new PutObjectRequest(this.bucketName, this.key, readFile(this.key)));
}
}
} catch (AmazonServiceException ae) {
System.out.println(ae.getMessage());
}
} catch (AmazonServiceException ex) {
Logger.getLogger(AthinioCloudMigration.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void AmazonDownload(String dirName, String xmlFilename, String amazonid) throws ParserConfigurationException, SAXException, TransformerException, IOException {
String cloudid;
cloudid = amazonid;
this.comm = new CommonResources(xmlFilename);
this.RequestFiles=new ArrayList();
try {
this.RequestFiles = this.comm.getXML(cloudid);
if (this.RequestFiles != null) {
int len = this.RequestFiles.size();
System.out.println(len);
for (int index = 0; index < len; index++) {
this.CRobj = (CommonResources) this.RequestFiles.get(index);
if (cloudid.equals(this.CRobj.getCloudID())) {
this.newFile = new File(dirName + this.CRobj.getFileName().concat(".rec"));
System.out.println(newFile);
newFile.createNewFile();
this.metaData = this.amzObj.getObject(new GetObjectRequest(this.bucketName, (dirName + this.CRobj.getFileName())), this.newFile);
System.out.println(metaData);
java.io.File tmp = new java.io.File(dirName + this.CRobj.getFileName());
System.out.println(tmp);
tmp.delete();
76,23 87%
public void amazonpload(字符串fileObj)引发IOException{
试一试{
this.key=fileObj;
试一试{
if(this.key==null){
}否则{
if(readFile(this.key)!=null){
//此参数为.key=“1”;
this.putObjResult=this.amzObj.putObject(新PutObjectRequest(this.bucketName、this.key、readFile(this.key));
}
}
}捕获(AmazonServiceException ae){
System.out.println(ae.getMessage());
}
}捕获(AmazonServiceException ex){
Logger.getLogger(AthinioCloudMigration.class.getName()).log(Level.SEVERE,null,ex);
}
}
public void AmazonDownload(String dirName、String xmlFilename、String amazonid)抛出ParserConfigurationException、SAXException、TransformerException、IOException{
弦云状;
cloudid=amazonid;
this.comm=新的公共资源(xmlFilename);
this.RequestFiles=new ArrayList();
试一试{
this.RequestFiles=this.comm.getXML(cloudid);
如果(this.RequestFiles!=null){
int len=this.RequestFiles.size();
系统输出打印项次(len);
对于(int index=0;index
因为在AmazonS3中没有文件夹结构,所以您可以将所有内容作为对象接收
例如:在bucket中,您将文件存储在类似文件夹的结构中,但当您从S3请求对象时,您将收到类似于folder1/folder2/demo.txt的文件
因此,试试这个,为S3文件获取InputStream
,比如amazonS3.getObject(bucket,“folder1/folder2/demo.txt”).getObjectContent()获取InputStream
后,将文件名传递给下面的方法,下载位置和InputStream
。如果使用Java7
则使用FileSystems.getDefault().getPath(fullPathWithFileName).getFileName().toString()
从对象名获取文件名
public void saveFile(String uploadFileName, String path, InputStream inputStream) throws Exception {
DataOutputStream dos = null;
OutputStream out = null;
try {
File newDirectory = new File(path);
if (!newDirectory.exists()) {
newDirectory.mkdirs();
}
File uploadedFile = new File(path, uploadFileName);
out = new FileOutputStream(uploadedFile);
byte[] fileAsBytes = new byte[inputStream.available()];
inputStream.read(fileAsBytes);
dos = new DataOutputStream(out);
dos.write(fileAsBytes);
} catch (IOException io) {
io.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (dos != null) {
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}