文件压缩并提取成流上传到Ambry,并且定时删除压缩包

这个是最近写的小模块,主要功能是:前端点击“上传”按钮,将文件压缩到指定目录,然后再把压缩后的zip文件转成流上传到Ambry(Lindedin)使用的分布式文件存储服务。压缩后的zip文件存储在指定目录中,每天会定时清空该文件夹。

定时删除方面用到了cron4j,quartz有点重,这里的定时功能用cron4j就完全可以实现。在maven中添加如下依赖:

   <dependency>
      <groupId>it.sauronsoftware.cron4j</groupId> 
      <artifactId>cron4j</artifactId>
      <cron4j_version>2.2.5</cron4j_version>
   </dependency>

接下来是各个功能的实现代码:

1 删除文件夹下的所有文件和子文件夹

package com.wj.ambry;

import java.io.File;

public class DeleteTask implements Runnable {

    private String tempFilePath;

    public DeleteTask(String tempFilePath){
        this.tempFilePath = tempFilePath;
    }

    public static void DeleteAll(File file){
        for(File fi:file.listFiles()){
            if(fi.isDirectory()){
                DeleteAll(fi);
            }
            else{
                fi.delete();
            }
        }
        file.delete();
    }

    @Override
    public void run(){
        File file = new File(tempFilePath);
        DeleteAll(file);
    }
}

2 将文件压缩,再转换成流以供上传

package com.wj.ambry;

import com.wj.common.Constants;
import com.wj.common.idgen.IDGenerator;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class StreamGenerator {

    private static File file;
    private static String tempFilePath;

    public StreamGenerator(File file,String tempFilePath){
        this.file = file;
        this.tempFilePath = tempFilePath;
    }

    /**
     * 压缩传入的文件,返回流
     * @return inputstream
     */
    public static InputStream getInputStream() {

        String zipName =  IDGenerator.getID(Constants.PUBLIC_COMPONENT_SYSTEM);

        //判断是否有文件夹,如果没有就创建
        File tempF = new File(tempFilePath);

        if(!tempF.exists()){
            tempF.mkdirs();
        }

        /**
         * 压缩并在指定目录生成临时zip文件
         */
        byte[] buffer = new byte[4096];
        ZipOutputStream out = null;
        try {
            out = new ZipOutputStream(new FileOutputStream(tempFilePath + zipName + ".zip"));
            FileInputStream fileInput = new FileInputStream(file);
            out.putNextEntry(new ZipEntry(file.getName()));
            int temp = 0;
            //读取文件并打包
            while ((temp = fileInput.read(buffer)) > 0) {
                out.write(buffer, 0, temp);
            }
            out.closeEntry();
            fileInput.close();
            out.close();
        } catch(Exception ex){
            ex.printStackTrace();
        }

        /**
         * 把压缩包读取为输出流
         */
        File filezip = new File(tempFilePath + zipName + ".zip");
        ByteArrayOutputStream byteOut = null;
        try {
            byteOut = new ByteArrayOutputStream();
            FileInputStream FileIn = new FileInputStream(filezip);
            BufferedInputStream bufferInput = new BufferedInputStream(FileIn);
            int k = bufferInput.read();
            while (k != -1) {
                byteOut.write(k);
                k = bufferInput.read();
            }
            bufferInput.close();
            FileIn.close();
        } catch(Exception ex) {
            ex.printStackTrace();
        }

        /**
         * 输出流转为uploadFile方法的输入流
         */
        ByteArrayInputStream inStream = new ByteArrayInputStream(byteOut.toByteArray());
        return inStream;
    }
}

3 调用上面的两个类并上传到Ambry

package com.wj.ambry;

import com.wj.common.utils.ExceptionUtils;
import com.wj.common.utils.StrUtils;
import it.sauronsoftware.cron4j.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;

public class HTTPUpload {
    private static final Logger log = LoggerFactory.getLogger(HTTPUpload.class);
    private final String ambryUrl;
    private final String tmpFilePath;

    public HTTPUpload(final String ambryUrl) {
        this.ambryUrl = ambryUrl;
        this.tmpFilePath = StrUtils.makeString(HTTPUpload.class.getResource("").getFile().toString(),"tmp");
        File file1 = new File(tmpFilePath);
        if(!file1.exists()){
            file1.mkdirs();
        }
        Scheduler scheduler = new Scheduler();
        scheduler.schedule("50 13 * * *", new DeleteTask(tmpFilePath));
    }

    public String uploadFile(MultipartFile file1, long size, String ServiceId, String owner, String fileFormat,
            String description) {
        HttpURLConnection connection = null;
        OutputStream os = null;
        DataInputStream is = null;
        InputStream inputStream = null;
        int count = 0;
        while (count <= 3) {
            try {
                File file = new File(StrUtils.makeString(tmpFilePath,"/",file1.getOriginalFilename()));
                file1.transferTo(file);
                if (!fileFormat.equals("image")) {
                    inputStream = new StreamGenerator(file, tmpFilePath).getInputStream();
                } else {
                    inputStream = new FileInputStream(file);
                }
                int length = inputStream.available();
                connection = (HttpURLConnection) new URL(ambryUrl).openConnection();
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/octet-stream");
                //设置是否从httpUrlConnection读入,默认情况下是true;
                connection.setDoOutput(true);
                //设置是否向httpUrlConnection输出,默认为false
                connection.setDoInput(true);
                connection.setRequestProperty("x-ambry-blob-size", length + "");
                connection.addRequestProperty("x-ambry-service-id", ServiceId);
                connection.addRequestProperty("x-ambry-owner-id", owner);
                connection.addRequestProperty("x-ambry-content-type", fileFormat);
                connection.addRequestProperty("x-ambry-um-description", description);
                connection.connect();

                os = new BufferedOutputStream(connection.getOutputStream());

                byte[] buffer = new byte[4096];
                int bytes_read;
                //只要可以读取到数据,就输出写到buffer中
                while ((bytes_read = inputStream.read(buffer)) != -1) {
                    os.write(buffer, 0, bytes_read);
                }
                //数据读取完关闭inputStream
                os.close();
                inputStream.close();
                String location = connection.getHeaderField("Location");
                return StrUtils.makeString(ambryUrl, location);
            } catch (Exception e) {
                count++;
                try {
                    TimeUnit.SECONDS.sleep(1L);
                } catch (InterruptedException e1) {
                }
                log.warn(ExceptionUtils.getStackTrace(e));
            } finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                } catch (IOException e) {
                    log.warn(ExceptionUtils.getStackTrace(e));
                }
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    log.warn(ExceptionUtils.getStackTrace(e));
                }
                if (connection != null) {
                    connection.disconnect();
                }
            }
        }
        return null;
    }

    public static void main(String[] args) throws FileNotFoundException {
        String rootPath=HTTPUpload.class.getResource("").getFile().toString();
        System.out.println(rootPath);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值