@ResponseBody @RequestMapping(value = "/admin/upload.ajax", method = RequestMethod.POST) public ResponseEntity<byte[]> upload( @RequestParam("restaurantId") String restaurantId, @RequestParam("file") CommonsMultipartFile file) throws Exception { Map<String, Object> model = new HashMap<String, Object>(); try { S3Object object = new S3Object(basePath + "/" + restaurantId); object.setDataInputStream(file.getInputStream()); object.setContentLength(file.getSize()); object.setContentType(file.getContentType()); S3Bucket bucket = s3Service.getBucket(bucketName); s3Service.putObject(bucket, object); Restaurant restaurant = restaurantRepository.findByRestaurantId(restaurantId); restaurant.setHasUploadedImage(true); restaurantRepository.saveRestaurant(restaurant); model.put("success", true); } catch (Exception ex) { LOGGER.error("", ex); model.put("success", false); model.put("message", ex.getMessage()); } String json = jsonUtils.serializeAndEscape(model); final HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_HTML); headers.setCacheControl("no-cache"); return new ResponseEntity<byte[]>(json.getBytes("utf-8"), headers, HttpStatus.OK); }
@Override protected boolean putInputStream(String bucket, String key, InputStream data, String contentType) throws Exception { org.jets3t.service.model.S3Object object = new org.jets3t.service.model.S3Object(key); object.setContentType(contentType); object.setDataInputStream(data); object.setContentLength(data.available()); return jetClient.putObject(bucket, object) != null; }
/** * Creates a directory flagged file with the key and folder suffix. * * @param key the key to create a folder * @return true if the operation was successful, false otherwise */ private boolean mkdirsInternal(String key) { try { String keyAsFolder = convertToFolderName(stripPrefixIfPresent(key)); S3Object obj = new S3Object(keyAsFolder); obj.setDataInputStream(new ByteArrayInputStream(new byte[0])); obj.setContentLength(0); obj.setContentType(Mimetypes.MIMETYPE_BINARY_OCTET_STREAM); mClient.putObject(mBucketName, obj); return true; } catch (ServiceException se) { LOG.error("Failed to create directory: " + key, se); return false; } }