日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用SpringBoot進行文件壓縮

1 簡介

你知道在Java應(yīng)用程序中優(yōu)化文件服務(wù)器的磁盤空間是非常重要的非功能性要求之一嗎?如果管理得當,可以節(jié)省文件存儲服務(wù)器上60%至70%的成本。因此,對于由Java Spring Boot API生成的數(shù)據(jù)文件進行壓縮顯得尤為非常重要。

在通化等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作定制網(wǎng)站設(shè)計,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,營銷型網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站建設(shè)公司,通化網(wǎng)站建設(shè)費用合理。

Java提供了多個庫來幫助我們壓縮內(nèi)容,同時也提供了本地文件操作來壓縮數(shù)據(jù)文件。在本文中,我們使用本地方法來實現(xiàn)壓縮。

2 使用Java實現(xiàn)文件壓縮和刪除功能

我們創(chuàng)建一個實用程序,它可以打包在Java代碼的任何位置,可以在你的微服務(wù)中的任何地方使用。你還可以將其放在共享庫中,作為組織中所有微服務(wù)的實用程序公開。我們的實用程序?qū)⒕哂袃蓚€功能:

  • 壓縮給定的源文件
  • 在壓縮成功后刪除原始文件

先決條件:

JDK ≥ 1.8 + Maven > 3.X + Spring Boot 2.X + Lombok + Common-IO

我們創(chuàng)建一個對象,該對象將具有基本參數(shù)用于壓縮文件。在下面的代碼中,我們創(chuàng)建了一個名為FileZipper類,它通過Lombok實現(xiàn)Builder和Getter功能。

@Builder(setterPrefix = "set")
@Getter
public class FileZipper {

 @NonNull
 private String sourceFilePath;
 @NonNull
 private String sourceFileName;
 @NonNull
 private String sourceFileExtension;
 @NonNull
 private String zippedFilePath;
 @NonNull
 private String zippedFileName;
 private boolean deleteSourceAfterZipped;
}

現(xiàn)在,我們創(chuàng)建一個名為FileZippingService的Java服務(wù)類,它將根據(jù)用戶輸入壓縮源文件。此服務(wù)類還具有文件刪除功能,可以在文件壓縮成功后刪除源文件。

public class FileZippingService {

 private static final String DOT = ".";
 private static final String ZIP = ".zip";

 private FileZippingService() {
 }

 static Logger logger = LoggerFactory.getLogger(FileZippingService.class);

 /**
  * Method to Zip a file
  * 
  * @param fileZipper
  */
 public static void zipExportFile(FileZipper fileZipper) {

  String sourceFileWithPath = fileZipper.getSourceFilePath().concat(fileZipper.getSourceFileName()).concat(DOT)
    .concat(fileZipper.getSourceFileExtension());
  String zippedFileWithPath = fileZipper.getZippedFilePath().concat(fileZipper.getZippedFileName()).concat(ZIP);
  logger.info("Source File : {} will be zipped into {}", sourceFileWithPath, zippedFileWithPath);
  try {
   File fileToZip = new File(sourceFileWithPath);
   if (fileToZip.exists()) {
    FileOutputStream fos = new FileOutputStream(zippedFileWithPath);
    ZipOutputStream zipOut = new ZipOutputStream(fos);
    FileInputStream fis = new FileInputStream(fileToZip);
    ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
    zipOut.putNextEntry(zipEntry);
    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
     zipOut.write(bytes, 0, length);
    }
    zipOut.close();
    fis.close();
    fos.close();
    logger.info("{} zipped successfully at {}", sourceFileWithPath, zippedFileWithPath);
    if (fileZipper.isDeleteSourceAfterZipped())
     deleteFile(sourceFileWithPath);
   }
  } catch (IOException e) {
   logger.error("Error while Zipping the file", e);
   deleteFile(zippedFileWithPath);
  }

 }

 /**
  * Method to delete a file at given path
  * 
  * @param fileToDelete
  */
 private static void deleteFile(String fileToDelete) {
  File filetoDelete = new File(fileToDelete);
  if (filetoDelete.exists()) {
   if (FileUtils.deleteQuietly(filetoDelete)) {
    logger.info("{} deleted successfully.", fileToDelete);
   } else {
    logger.info("{} deletion unsuccessfull.", fileToDelete);
   }
  } else {
   logger.info("{} was not found for deletion.", fileToDelete);
  }

 }

}

接下來我們通過運行Spring Boot應(yīng)用程序來測試代碼。在下面的Java文件中,你可以看到提供的源文件路徑、名稱和擴展名,從中需要讀取文件;還提供了壓縮文件的文件名以及壓縮后需要存儲的路徑。此外,這里還設(shè)置了一個標志,用于決定是否在壓縮后刪除源文件。

@SpringBootApplication
public class FileZippingApplication {

 public static void main(String[] args) {
  SpringApplication.run(FileZippingApplication.class, args);

  FileZipper fileZipper = FileZipper.builder()
    .setSourceFilePath("C:/Data/")
    .setSourceFileName("UserData")
    .setSourceFileExtension("xlsx")
    .setZippedFileName("ZippedUserData")
    .setZippedFilePath("C:/Data/")
    .setDeleteSourceAfterZipped(true)
    .build();

  FileZippingService.zipExportFile(fileZipper);
 }

}

3 結(jié)果

上述代碼的輸出結(jié)果令人非常驚訝。在我們的一個業(yè)務(wù)應(yīng)用程序中,有大量的CSV文件,大小一般在600MB到1 GB之間,占用消耗大量的磁盤空間,因為我們在一個小時內(nèi)收到了1000多個請求。

但是,在使用上述代碼壓縮后,磁盤空間減少了85%,我們甚至可以通過電子郵件向客戶發(fā)送文件。


分享名稱:使用SpringBoot進行文件壓縮
文章出自:http://www.5511xx.com/article/dppcjge.html