Java导入导出图片

Java导入图片压缩包到服务器&导出服务器图片为压缩包实现步骤:1.导入图片压缩包 - 获取上传压缩包解压遍历为单个文件 - 定义图片存储目录 (Linux和win目录不同) - 遍历图片File存储到指定目录//图片存储目录 Linux路径:/opt/student_enlistprivate String saveRootDirectory = "D:\\student_enlist"
摘要由CSDN通过智能技术生成

Java导入&导出图片

实现步骤:

导入图片压缩包
- 1.获取上传压缩包解压遍历为单个文件
- 2.定义图片存储目录 (Linux和win目录不同)
- 3.遍历图片File存储到指定目录
- 4.后台异步任务上传图片到服务器,生成UUID存储到数据库 (根据自己项目情况)

//图片存储目录  Linux路径:/opt/student_enlist
private String saveRootDirectory = "D:\\student_enlist";
//创建文件夹
File file1 = new File(saveRootDirectory);
if (!file1.exists() && !file1.isDirectory()) {
    file1.mkdir();
}

/**
     * 获取压缩包里面的文件
     *
     * @param file 上传的压缩文件
     * @return
     * @throws Exception
     */
private Map<String, Object> readZipFile(File file) throws Exception {
        ZipFile zf = new ZipFile(file);
        Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zf.entries();
        while (entries.hasMoreElements()) {
            //获取一个文件对象
            ZipEntry zipEntry = (ZipEntry) entries.nextElement();
            InputStream read = zf.getInputStream(zipEntry);
            String fileUrl = zipEntry.getName();
            // 判断是否为文件类型
            if (fileUrl != null && fileUrl.indexOf(".") != -1) {
                // 如果只读取图片,自行判断就OK.
                String fileName = zipEntry.getName();
                //创建图片到存储目录
                File file = new File(saveRootDirectory +File.separatorChar+ fileName);
                //判断图片是否已经存在
                if (!file.exists()) { 
                    //创建图片文件
                    file.createNewFile();
                    // 写入文件
                    BufferedOutputStream write = new BufferedOutputStream(
                            
  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值