亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

基于Java寫minio客戶端實現上傳下載文件的方法

發布時間:2020-07-23 15:58:45 來源:億速云 閱讀:867 作者:小豬 欄目:編程語言

這篇文章主要講解了基于Java寫minio客戶端實現上傳下載文件的方法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

前言:

  確保已經安裝了minio的服務端

代碼:

pom.xml

<dependency>
  <groupId>io.minio</groupId>
  <artifactId>minio</artifactId>
  <version>7.0.2</version>
</dependency>

application.yml

server:
 port:90
minio:
 url: http://10.69.94.140:9000
 accessKey: 賬號
 secretKey: 密碼
 defaultFolder: /

MinioProperties.java

@ConfigurationProperties("minio")
@Data
public class MinioProperties {
  private String url;
  private String accessKey;
  private String secretKey;
  private String defaultFolder;
}

SpringConfig.java

@Configuration
@EnableConfigurationProperties(MinioProperties.class)
@Slf4j
public class SpringConfig {
  @Autowired
  private MinioProperties minioProperties;

  @Bean
  public MinioClient minioClient() {
    try {
      return new MinioClient(minioProperties.getUrl(), minioProperties.getAccessKey(), minioProperties.getSecretKey());
    } catch (Exception e) {
      log.error(e.toString());
    }
    return null;
  }

}

ImagesController.java

@RestController
@RequestMapping("/image")
@Slf4j
@CrossOrigin(origins = "*")
public class ImageController {

  @Autowired
  private FileService fileService;

  /*******
   * Get image file, this method return an image type file which can be displayed in browser.
   * @param bucketName, system, each system should belong a special bucket.
   * @param category, a system may contain multiple category
   * @param fileName
   */
  @GetMapping(value = "/get/{bucketName}/{category}/{objectName}/{fileName}", produces = MediaType.IMAGE_JPEG_VALUE)
  public byte[] get(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category,
           @PathVariable("objectName") String objectName,
           @PathVariable("fileName") String fileName) throws Exception {
    return fileService.getFile(bucketName, category, objectName);
  }

  @GetMapping("/download/{bucketName}/{category}/{objectName}/{fileName}")
  public void download(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category,
             @PathVariable("objectName") String objectName,
             @PathVariable("fileName") String fileName, HttpServletResponse response) throws Exception {
    byte[] buffer = fileService.getFile(bucketName, category, objectName);
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
    response.getOutputStream().write(buffer);
    response.flushBuffer();
    response.getOutputStream().close();
  }

  @PostMapping("/upload/{bucketName}/{category}")
  public String upload(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category,
             @RequestParam("file") MultipartFile file) throws Exception {
    String objectName = UUID.randomUUID().toString();
    fileService.storeFile(bucketName, category, objectName, file.getBytes());
    return String.format("image/get/%s/%s/%s/%s", bucketName, category, objectName, file.getOriginalFilename());
  }
}

FilesController.java

@RestController
@RequestMapping("/files")
@Slf4j
@CrossOrigin(origins = "*")
public class FilesController {

  @Autowired
  private FileService fileService;

  @GetMapping("/download/{bucketName}/{category}/{objectName}/{fileName}")
  public void download(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category,
             @PathVariable("objectName") String objectName, @PathVariable("fileName") String fileName, HttpServletResponse response) throws Exception {
    byte[] buffer = fileService.getFile(bucketName, category, objectName);
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
    response.getOutputStream().write(buffer);
    response.flushBuffer();
    response.getOutputStream().close();
  }

  @PostMapping("/upload/{bucketName}/{category}")
  public String upload(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category,
             @RequestParam("file") MultipartFile file) throws Exception {
    String objectName = UUID.randomUUID().toString();
    fileService.storeFile(bucketName, category, objectName, file.getBytes());
    return String.format("files/download/%s/%s/%s/%s", bucketName, category, objectName, file.getOriginalFilename());
  }
}

upload.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Upload file test</title>
</head>
<body>
  <form action="http://localhost:90/image/upload/zeng/test" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="Submit">
  </form>
</body>
</html>

看完上述內容,是不是對基于Java寫minio客戶端實現上傳下載文件的方法有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

垫江县| 库尔勒市| 崇左市| 抚顺市| 武义县| 昌乐县| 泾川县| 梓潼县| 乌兰察布市| 三原县| 济源市| 黔西| 舞阳县| 湟源县| 德州市| 道孚县| 安徽省| 宝丰县| 永春县| 九寨沟县| 红桥区| 友谊县| 吉林市| 衢州市| 太仆寺旗| 全南县| 延吉市| 汤原县| 乌兰察布市| 西丰县| 台湾省| 石嘴山市| 敦煌市| 乐昌市| 白城市| 同江市| 措勤县| 武陟县| 麟游县| 德安县| 景泰县|