Java Code Examples for com.aliyun.oss.model.ObjectMetadata#setHeader()
The following examples show how to use
com.aliyun.oss.model.ObjectMetadata#setHeader() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AliyunOssClient.java From markdown-image-kit with MIT License | 6 votes |
/** * Upload string. * * @param ossClient the ossClient client * @param instream the instream * @param fileName the file name * @return the string */ public String upload(@NotNull OSS ossClient, @NotNull InputStream instream, @NotNull String fileName) { try { // 创建上传 Object 的 Metadata ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentLength(instream.available()); objectMetadata.setCacheControl("no-cache"); objectMetadata.setHeader("Pragma", "no-cache"); objectMetadata.setContentType(ImageUtils.getImageType(fileName)); objectMetadata.setContentDisposition("inline;filename=" + fileName); ossClient.putObject(bucketName, filedir + fileName, instream, objectMetadata); return getUrl(ossClient, filedir, fileName); } catch (IOException | OSSException | ClientException e) { log.trace("", e); } return ""; }
Example 2
Source File: ALiYunOSSUploadFileTemplateServiceImpl.java From plumemo with Apache License 2.0 | 6 votes |
@Override public String doSaveFileStore(final MultipartFile file) { final OSS ossClient = new OSSClientBuilder() .build(ConfigCache.getConfig(Constants.ALIYUN_OSS_ENDPOINT), ConfigCache.getConfig(Constants.ALIYUN_OSS_ACCESS_KEY), ConfigCache.getConfig(Constants.ALIYUN_OSS_SECRET_KEY)); try { final String fileName = FileUtil.createSingleFilePath(ConfigCache.getConfig(Constants.ALIYUN_OSS_PATH), file.getOriginalFilename()); final PutObjectRequest putObjectRequest = new PutObjectRequest(ConfigCache.getConfig(Constants.ALIYUN_OSS_BUCKET), fileName, file.getInputStream()); final ObjectMetadata metadata = new ObjectMetadata(); metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString()); putObjectRequest.setMetadata(metadata); ossClient.putObject(putObjectRequest); return ConfigCache.getConfig(Constants.ALIYUN_OSS_IMAGE_DOMAIN) + fileName; } catch (final IOException e) { return ""; } finally { if (ossClient != null) { ossClient.shutdown(); } } }
Example 3
Source File: OSSImageupload.java From Tbed with GNU Affero General Public License v3.0 | 5 votes |
/** * 客户端接口 * */ public Map<ReturnImage, Integer> clientuploadOSS(Map<String, MultipartFile> fileMap, String username, UploadConfig uploadConfig) throws Exception { File file = null; Map<ReturnImage, Integer> ImgUrl = new HashMap<>(); ObjectMetadata meta = new ObjectMetadata(); meta.setHeader("Content-Disposition", "inline"); for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) { String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位 java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss"); String times = format1.format(new Date()); file = SetFiles.changeFile(entry.getValue()); String head = ""; if(entry.getKey().equals("jpg")||entry.getKey().equals("jpeg")){ head = "image/jpeg"; }else if(entry.getKey().equals("png")){ head = "image/png"; }else if(entry.getKey().equals("bmp")){ head = "image/bmp"; }else if(entry.getKey().equals("gif")){ head = "image/gif"; }else{ //System.err.println("位置格式文件,无法定义header头。"); head = "image/"+entry.getKey(); } meta.setHeader("Content-Type", head);//image/jpeg ReturnImage returnImage = new ReturnImage(); if(entry.getValue().getSize()/1024<=uploadConfig.getFilesizeuser()*1024){ ossClient.putObject(key.getBucketname(), username + "/" + uuid+times + "." + entry.getKey(),file,meta); returnImage.setImgname(entry.getValue().getOriginalFilename()); returnImage.setImgurl(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey()); ImgUrl.put(returnImage, (int) (entry.getValue().getSize())); }else{ returnImage.setImgname(entry.getValue().getOriginalFilename()); returnImage.setImgurl("文件超出系统设定大小,不得超过"); ImgUrl.put(returnImage, -1); } } return ImgUrl; }
Example 4
Source File: USSImageupload.java From Tbed with GNU Affero General Public License v3.0 | 5 votes |
/** * 客户端接口 * */ public Map<ReturnImage, Integer> clientuploadUSS(Map<String, MultipartFile> fileMap, String username, UploadConfig uploadConfig) throws Exception { File file = null; Map<ReturnImage, Integer> ImgUrl = new HashMap<>(); //设置Header ObjectMetadata meta = new ObjectMetadata(); meta.setHeader("Content-Disposition", "inline"); for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) { String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位 java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss"); String times = format1.format(new Date()); file = SetFiles.changeFile(entry.getValue()); // 上传文件流。 System.out.println("客户端:待上传的图片:"+username + "/" + uuid+times + "." + entry.getKey()); ReturnImage returnImage = new ReturnImage(); if(entry.getValue().getSize()/1024<=uploadConfig.getFilesizeuser()*1024){ // 例2:采用数据流模式上传文件(节省内存),自动创建父级目录 upyun.setContentMD5(UpYun.md5(file)); boolean result = upyun.writeFile(username + "/" + uuid+times + "." + entry.getKey(), file, true); if(result){ returnImage.setImgname(entry.getValue().getOriginalFilename()); returnImage.setImgurl(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey()); ImgUrl.put(returnImage, (int) (entry.getValue().getSize())); //ImgUrl.put(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey(), (int) (entry.getValue().getSize())); }else{ System.err.println("上传失败"); } }else{ returnImage.setImgname(entry.getValue().getOriginalFilename()); returnImage.setImgurl("文件超出系统设定大小,不得超过"); ImgUrl.put(returnImage, -1); } } return ImgUrl; }
Example 5
Source File: OssUploadServiceImpl.java From mysiteforme with Apache License 2.0 | 4 votes |
@Override public String upload(MultipartFile file) throws IOException, NoSuchAlgorithmException { String fileName =null,realNames = ""; StringBuffer returnUrl = new StringBuffer(getUploadInfo().getOssBasePath()); String ossDir = getUploadInfo().getOssDir(); try { fileName = file.getOriginalFilename(); //上传文件 StringBuffer realName = new StringBuffer(UUID.randomUUID().toString()); String fileExtension = fileName.substring(fileName.lastIndexOf(".")); realName.append(fileExtension); QETag tag = new QETag(); String hash = tag.calcETag(file); Rescource rescource = new Rescource(); EntityWrapper<RestResponse> wrapper = new EntityWrapper<>(); wrapper.eq("hash",hash); wrapper.eq("source","oss"); rescource = rescource.selectOne(wrapper); if( rescource!= null){ return rescource.getWebUrl(); } InputStream is = file.getInputStream(); Long fileSize = file.getSize(); //创建上传Object的Metadata ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(is.available()); metadata.setCacheControl("no-cache"); metadata.setHeader("Pragma", "no-cache"); metadata.setContentEncoding("utf-8"); metadata.setContentType(ToolUtil.getContentType(fileName)); StringBuffer description = new StringBuffer("filename/filesize="); description.append(realNames).append("/").append(fileSize).append("Byte."); metadata.setContentDisposition(description.toString()); StringBuffer key = new StringBuffer(); if(ossDir != null && !"".equals(ossDir)){ key.append(ossDir).append("/"); returnUrl.append(ossDir).append("/"); } key.append(realName); returnUrl.append(realName); PutObjectResult putResult = getOSSClient().putObject(getUploadInfo().getOssBucketName(), key.toString(), is, metadata); //解析结果 System.out.println("md5码为"+putResult.getETag()); rescource = new Rescource(); rescource.setFileName(realName.toString()); rescource.setFileSize(new java.text.DecimalFormat("#.##").format(file.getSize()/1024)+"kb"); rescource.setHash(hash); rescource.setFileType(StringUtils.isBlank(fileExtension)?"unknown":fileExtension); rescource.setWebUrl(returnUrl.toString()); rescource.setSource("oss"); rescource.insert(); getOSSClient().shutdown(); is.close(); } catch (Exception e) { throw new MyException("上传阿里云OSS服务器异常." + e.getMessage()); } return returnUrl.toString(); }