【问题标题】:Upload zip file or image to Amazon s3 using java and html input form使用 java 和 html 输入表单将 zip 文件或图像上传到 Amazon s3
【发布时间】:2013-07-15 19:39:55
【问题描述】:

希望你能帮助我。我找不到任何问题的答案

我有这部分 JSP 代码:

  <html:form action="/restricted/gallery/GalleryUpload" method="post" enctype="multipart/form-data">  
  <table width="98%" border="0" cellspacing="0" cellpadding="2">
    <tr>
      <td width="21%" align="right" class="input_head">Upload Image: &nbsp;</td>
      <td class="size12_text_red"><html:file property="file" styleId="File"/>&nbsp;<br/>(Contents of zip files will be extracted and uploaded individually)</td>
    </tr>
    <tr align="center">
      <td colspan="4"><input name="upload2" type="submit" id="upload" class="butt_style" value="Submit"/></td>
    </tr>
  </table>

然后我将数据传递给 Java 类:

            else if (req.getParameter("upload2") != null) {

            String existingBucketName = "BUCKETNAME";           
            String filePath = cform.getFile().toString(); 
            String keyName = filePath.substring(filePath.lastIndexOf("/")+1); 

            String amazonFileUploadLocationOriginal=existingBucketName+"/gallery/"+user.getOwnerId()+"/album_name"; 
            AmazonS3 s3Client = new AmazonS3Client(new PropertiesCredentials(SouthdownsServicesAction.class.getResourceAsStream("AwsCredentials.properties"))); 
            FileInputStream stream = new FileInputStream(filePath); 
            ObjectMetadata objectMetadata = new ObjectMetadata(); 
            PutObjectRequest putObjectRequest = new PutObjectRequest(amazonFileUploadLocationOriginal, keyName, stream, objectMetadata); 
            PutObjectResult result = s3Client.putObject(putObjectRequest); 

所以我想要做的是,在 filePath 部分中,我试图将完整路径从您选择的输入表单文件传递到那里,但它根本不起作用。如果我将该 filePath 更改为位于我的本地计算机上的简单路径,它可以正常工作。像下面这样:

String filePath = "C://Pics//logo.jpg"; 

如果我上传的是 ZIP 文件而不是图片,它将如何提取 ZIP 内容并单独上传所有文件?

提前致谢

【问题讨论】:

    标签: java html forms file upload


    【解决方案1】:

    请使用下面的 put() 方法。 S3 不会解压缩您的文件 - 如果您希望它们单独出现在 S3 中,则必须逐个上传。

        //---------------------------------------------------------------------     
        // Amazon S3
        //---------------------------------------------------------------------     
            class S3 extends AmazonS3Client
             {final String bucket;
              S3(String u, String p, String Bucket)
               {super(new BasicAWSCredentials(u, p));
                bucket = Bucket;
               }
              boolean put(String k, String v)      
               {try 
                 {final ByteArrayInputStream b = new ByteArrayInputStream(v.toString().getBytes());
                  putObject(bucket, k, b, new ObjectMetadata());
                  setObjectAcl(bucket, k, CannedAccessControlList.PublicRead);  // Has to be here to allow change to reduced redundancy
                  changeObjectStorageClass(bucket, k, StorageClass.ReducedRedundancy);
                  setObjectAcl(bucket, k, CannedAccessControlList.PublicRead);  // Has to be repeated because it is now a new object again
                  return true; 
                 }
                catch(Exception e) {log("Cannot put "+bucket+"/"+k+" to S3 because "+e);}
                return false; 
               }
              String get(String k) 
               {try 
                 {final S3Object f = getObject(bucket, k);
                  final BufferedInputStream i = new BufferedInputStream(f.getObjectContent());  
                  final StringBuilder s = new StringBuilder(); 
                  final byte[]b = new byte[1024];
                  for(int n = i.read(b); n != -1; n = i.read(b)) {s.append(new String(b, 0, n));}
                  return s.toString(); 
                 }
                catch(Exception e) {log("Cannot get "+bucket+"/"+k+" from S3 because "+e);}
                return null; 
               }
              String[]list(String d) 
               {try 
                 {final ObjectListing l = listObjects(bucket, d);
                  final List<S3ObjectSummary> L = l.getObjectSummaries(); 
                  final int n = L.size();
                  final String[]s = new String[n];  
                  for(int i = 0; i < n; ++i)
                   {final S3ObjectSummary k = L.get(i);
                    s[i] = k.getKey();
                   } 
                  return s; 
                 }
                catch(Exception e) {log("Cannot list "+bucket+"/"+d+" on S3 because "+e);}
                return new String[]{}; 
               }
             }
           }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-24
      • 2015-09-05
      • 2020-10-02
      • 2013-10-03
      • 2020-08-16
      • 1970-01-01
      • 2017-04-28
      • 2021-06-10
      相关资源
      最近更新 更多