| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.io.*; |
| | | import java.net.URLEncoder; |
| | | import java.util.UUID; |
| | | |
| | |
| | | } |
| | | return null; |
| | | } |
| | | /** |
| | | * 文件上传 |
| | | * |
| | | * @param file |
| | | * 要上传的文件 |
| | | * @return |
| | | */ |
| | | public String uploadFile(File file, String name) { |
| | | if (null != file) { |
| | | try { |
| | | UUID uuid = UUID.randomUUID(); |
| | | StringBuilder s = new StringBuilder(); |
| | | s.append(uuid.toString().replace("-", "")).append("/"); |
| | | MinioClient minioClient = new MinioClient(minioProperties.getHost(), minioProperties.getAccessKey(), |
| | | minioProperties.getSecretKey()); |
| | | // bucket 不存在,创建 |
| | | if (!minioClient.bucketExists(minioProperties.getBucket())) { |
| | | minioClient.makeBucket(minioProperties.getBucket()); |
| | | } |
| | | // 得到文件流 |
| | | InputStream input = new FileInputStream(file); |
| | | // 文件名 |
| | | // String fileName = uuid + "/images." + FilenameUtils.getExtension(file.getOriginalFilename()); |
| | | String fileName = s.append(name).toString(); |
| | | minioClient.putObject(minioProperties.getBucket(), fileName, input, ""); |
| | | StringBuilder fileUrl = new StringBuilder(minioProperties.getUrl()); |
| | | String url = fileUrl.append(fileName).toString(); |
| | | return url; |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 文件下载 |