package io.j99.app.measure.storage; import com.qiniu.common.QiniuException; import com.qiniu.common.Zone; import com.qiniu.http.Response; import com.qiniu.storage.Configuration; import com.qiniu.storage.UploadManager; import com.qiniu.util.Auth; import io.j99.app.measure.properties.storage.QiniuStorageProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.nio.file.Path; import java.util.UUID; import java.util.stream.Stream; @Service public class QiniuStorageService implements StorageService { private final String qiniuSecret; private final String qiniuKey; private final String baseUrl; private Auth auth; private final String bucket; private Configuration configuration; @Autowired public QiniuStorageService(QiniuStorageProperties properties) { this.qiniuKey = properties.getKey(); this.qiniuSecret = properties.getSecret(); this.bucket = properties.getBucket(); this.baseUrl = properties.getBaseUrl(); } @Override public void init() { auth = Auth.create(qiniuKey, qiniuSecret); configuration = new Configuration(Zone.autoZone()); } @Override public String store(MultipartFile file) { String filename = file.getOriginalFilename(); return store(file, UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf("."))); } public String getUploadToken() { String token = auth.uploadToken(bucket); return token; } @Override public String store(MultipartFile file, String saveName) { InputStream is = null; try { is = file.getInputStream(); return this.store(is, saveName, file.getContentType()); } catch (IOException e) { e.printStackTrace(); } finally { // if (is != null) { // try { //// is.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } } return null; } @Override public String store(InputStream in, String saveName, String contentType) { try { UploadManager uploadManager = new UploadManager(configuration); String token = getUploadToken(); Response res = uploadManager.put(in, saveName, token, null, contentType); return saveName; } catch (QiniuException e) { Response r = e.response; // 請求失敗時打印的異常的信息 System.out.println(r.toString()); try { //響應的文本信息 System.out.println(r.bodyString()); } catch (QiniuException e1) { e1.printStackTrace(); } } return null; } @Override public String store(File file, String contentType) throws IOException { return this.store(file, file.getName(), contentType); } @Override public String store(File file, String saveName, String contentType) throws IOException { FileInputStream fis = null; try { fis = new FileInputStream(file); return this.store(fis, saveName, contentType); } catch (FileNotFoundException e) { throw e; } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { throw e; } } } } @Override public Stream<Path> loadAll() { throw new RuntimeException(); } @Override public Path load(String filename) { return null; } @SuppressWarnings("Duplicates") @Override public Resource loadAsResource(String filename) { return loadAsResource(filename, 0, 0); } @Override public Resource loadAsResource(String filename, int width, int height) { String url; if (width > 0 && height > 0) { url = auth.privateDownloadUrl(baseUrl + filename + "?imageMogr2/thumbnail/" + width + "x" + height, 600 * 30); } else { url = auth.privateDownloadUrl(baseUrl + filename, 600 * 30); } return new NetResource(url, filename); } @Override public void deleteAll() { // throw new RuntimeException(); } }
File dirFile = new File(newsaveurl);//刪除文件 if (dirFile.exists()&&dirFile.isFile()) { try { storageService.store(dirFile,newFileName, "application/x-jpg-compressed"); storageService.store(dirFile,newFileName, "application/x-zip-compressed"); } catch (IOException e) { e.printStackTrace(); throw new StorageException("上傳失敗", e); }
}java