0
String contentType = request.getContentType();
  if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
        DataInputStream in = new DataInputStream(request.getInputStream());
        int formDataLength = request.getContentLength();
        byte dataBytes[] = new byte[formDataLength];
        int byteRead = 0;
        int totalBytesRead = 0;
        while (totalBytesRead < formDataLength) {
              byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
              totalBytesRead += byteRead;
        }
        String file = new String(dataBytes);
        saveFile = file.substring(file.indexOf("filename=\"") + 10);
        saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
        saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1, saveFile.indexOf("\""));
        int lastIndex = contentType.lastIndexOf("=");
        String boundary = contentType.substring(lastIndex + 1, contentType.length());
        int pos;
        pos = file.indexOf("filename=\"");
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        pos = file.indexOf("\n", pos) + 1;
        int boundaryLocation = file.indexOf(boundary, pos) - 4;
        int startPos = ((file.substring(0, pos)).getBytes()).length;
        int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;            

        String bucket = "k.wong";
        String fileName = saveFile;            
        try {
            AmazonS3 client = new AmazonS3Client(new BasicAWSCredentials(awsAccessKey, awsSecretKey));
            ByteArrayInputStream stream = new ByteArrayInputStream(dataBytes);
            ObjectMetadata meta = new ObjectMetadata();

            client.putObject(bucket, fileName, stream, meta);
            client.setObjectAcl(bucket, fileName, CannedAccessControlList.PublicRead);                
            GeneratePresignedUrlRequest imgURL = new GeneratePresignedUrlRequest(bucket, fileName);
            destFile += client.generatePresignedUrl(imgURL);
        } catch (Exception ex) {
            System.out.println(ex);
        }

I was trying to upload a photo/image to the Amazon-S3 storage, and I am successful with the uploading. In my Amazon-S3 storage does show the file I uploaded. But the problem is the photo I upload is corrupted/crashed, whenever I am trying to open and view the photo from my Amazon-S3. I keep try and error of my coding, but I couldn't find where is the mistake. Thanks.

1
Contributor
1
Reply
8
Views
1 Year
Discussion Span
Last Post by gahhon
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.