网络图片的下载以及上传到fastDFS

简介: 最近做了一次下载网络图片然后上传到fastDFS的任务。碰到了个别小问题现在记录一下

最近做了一次下载网络图片然后上传到fastDFS的任务。碰到了个别小问题现在记录一下。


主要思路

下载图片,然后,生成临时文件

得到临时文件生成的文件流

上传该文件流到fastDFS。

系统分析

网络图片下载

public static File downloadFromUrl(String urlStr){
        //获取URL对象
        URL url = null;
        HttpURLConnection conn = null;
        File file = null;
        try {
            url = new URL(urlStr);
            //获取连接
            conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(6000);
            //设置超时时间是3秒
            conn.setReadTimeout(6000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.19 Safari/537.36");
             //得到临时文件
            InputStream is = conn.getInputStream();
            if (is == null || is.available() <= 0) {
                throw new DownloadException("图片为空");
            }
            file = getTemplateFile(is);
        } catch (MalformedURLException e) {
            log.error("图片下载出现异常={}", e.getMessage());
            throw new DownloadException("图片下载失败");
        } catch (Exception e) {
            log.error("图片下载出现异常={}", e.getMessage());
            throw new DownloadException("图片下载失败");
        } finally {
            //关闭连接
            if (conn != null) {
                conn.disconnect();
            }
        }
        return file;
    }


此处得到一个输入流之后生成了一个临时文件,至于为啥要生成临时文件,主要的原因是如果直接将下载得到的输入流上传到fastDFS 上会导致上传之后的图片显示不全。故,做了如下如下处理。

生成临时文件:

/**
     * 获取模板文件--获取到的文件为临时文件,用完后需要手动删除
     *
     * @param inputStream
     * @return 模板文件
     * @throws Exception 异常抛出
     */
    public static File getTemplateFile(InputStream inputStream) throws Exception {
        File file = File.createTempFile("temp_image", null);
        inputStreamToFile(inputStream, file);
         if (file.exists() && file.length() <= 0) {
            throw new DownloadException("临时文件为空");
        }
        return file;
    }
/**
     * InputStream 转file
     *
     * @param ins  输入流
     * @param file 目标文件
     */
    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            try {
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                os.flush();
            } finally {
                if (os != null) {
                    os.close();
                }
                if (ins != null) {
                    ins.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


图片上传

@Override
    public String download2UploadImg(String oldUrl) throws Exception {
        //1.下载图片
        File tempFile = null;
        InputStream is = null;
        String newPic;
        try {
            tempFile = ImageUtil.downloadFromUrl(oldUrl);
            String fileName;
            if (oldUrl.contains(SPECIAL_SIGN)) {
                fileName = oldUrl.substring(oldUrl.lastIndexOf("/") + 1, oldUrl.indexOf(SPECIAL_SIGN));
            } else {
                fileName = oldUrl.substring(oldUrl.lastIndexOf("/") + 1);
            }
            //校验格式
            String suffix = fileName.substring(fileName.indexOf("."));
            if (!ImageContentType.toMap().containsKey(suffix)) {
                log.info("文件格式不对,该格式为={}", suffix);
                throw new ImageException("图片格式不对");
            }
            is = new FileInputStream(tempFile);
            if (tempFile == null || is == null || is.available() <= 0) {
                log.info("图片为空");
                throw new ImageException("图片为空");
            }
            //2.上传图片
            newPic = FastDfsUtil.uploadFile(is, fileName);
        } finally {
            //手动删除临时文件
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new IOException("文件流关闭失败");
                }
            }
            if (tempFile != null) {
                tempFile.delete();
            }
        }
        log.info("上传完之后新图片地址="+(fastdfs_url + newPic));
        return fastdfs_url + newPic;
    }@Override
    public String download2UploadImg(String oldUrl) throws Exception {
        //1.下载图片
        File tempFile = null;
        InputStream is = null;
        String newPic;
        try {
            tempFile = ImageUtil.downloadFromUrl(oldUrl);
            String fileName;
            if (oldUrl.contains(SPECIAL_SIGN)) {
                fileName = oldUrl.substring(oldUrl.lastIndexOf("/") + 1, oldUrl.indexOf(SPECIAL_SIGN));
            } else {
                fileName = oldUrl.substring(oldUrl.lastIndexOf("/") + 1);
            }
            //校验格式
            String suffix = fileName.substring(fileName.indexOf("."));
            if (!ImageContentType.toMap().containsKey(suffix)) {
                log.info("文件格式不对,该格式为={}", suffix);
                throw new ImageException("图片格式不对");
            }
            is = new FileInputStream(tempFile);
            if (tempFile == null || is == null || is.available() <= 0) {
                log.info("图片为空");
                throw new ImageException("图片为空");
            }
            //2.上传图片
            newPic = FastDfsUtil.uploadFile(is, fileName);
        } finally {
            //手动删除临时文件
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new IOException("文件流关闭失败");
                }
            }
            if (tempFile != null) {
                tempFile.delete();
            }
        }
        log.info("上传完之后新图片地址="+(fastdfs_url + newPic));
        return fastdfs_url + newPic;
    }


具体的上传方式参见:上传图片到fastDFS

3. 测试方法

@Test
    public void testDownload2Upload() throws Exception {
        //1.图片正常下载上传
        String oldUrl = "http://img.okwuyou.com/shop/store/goods/1/2017/05/16/1_05482671770179845.jpg";
        Assert.assertNotNull(okwuyouSynItemService.download2UploadImg(oldUrl));
        //2.图片为空
        String oldUrl1 = "http://img.okwuyou.com/shop/store/goods/1/2018/01/25/48_05701903155753607.jpg";
        try {
            okwuyouSynItemService.download2UploadImg(oldUrl1);
        } catch (Exception e) {
            Assert.assertTrue(e instanceof DownloadException);
        }
        //3.图片路径失效
        String oldUrl2 = "http://img.okwuyou.com/shop/store/goods/12017/12/15/25_05666488135210549.png";
        try {
            okwuyouSynItemService.download2UploadImg(oldUrl2);
        } catch (Exception e) {
            Assert.assertTrue(e instanceof DownloadException);
        }
        //4.图片格式不对
        String oldUrl3 = "https://github.com/XWxiaowei/JavaWeb/blob/master/jackson-demo/pom.xml";
        try {
            okwuyouSynItemService.download2UploadImg(oldUrl3);
        } catch (Exception e) {
            Assert.assertTrue(e instanceof ImageException);
        }
    }
     @Test
     public void testDeleteFile() {
         //5.临时文件是否删除
//        1.获取临时文件的地址
         String tempPath = System.getProperty("java.io.tmpdir") + File.separator;
         System.out.println("临时文件的目录是=" + tempPath);
//      2.匹配临时文件
         List<String> fileNameList = getAllFile(tempPath, false);
         if (!CollectionUtils.isEmpty(fileNameList)) {
             int i = 0;
             for (String s : fileNameList) {
                 if (s.contains("temp_image")) {
                     i++;
                     System.out.println("临时文件未删除" + i);
                 }
             }
         }
     }
    /**
     * 获取路径下的所有文件/文件夹
     * @param directoryPath 需要遍历的文件夹路径
     * @param isAddDirectory 是否将子文件夹的路径也添加到list集合中
     * @return
     */
    public static List<String> getAllFile(String directoryPath,boolean isAddDirectory) {
        List<String> list = new ArrayList<String>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                if(isAddDirectory){
                    list.add(file.getAbsolutePath());
                }
                list.addAll(getAllFile(file.getAbsolutePath(),isAddDirectory));
            } else {
                list.add(file.getAbsolutePath());
            }
        }
        return list;
    }
相关文章
|
网络协议 Python
149 python网络编程 - TFTP下载案例
149 python网络编程 - TFTP下载案例
111 0
|
存储 监控 数据可视化
Java网络编程:下载进度监控实现详解
文件下载是许多应用程序的重要功能,而下载进度监控是提高用户体验的关键。在本文中,我们将详细介绍如何使用Java实现文件下载进度监控,以便用户可以实时了解文件下载的进度。
297 0
|
XML Java Android开发
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
Android App开发网络通信中使用okhttp下载和上传图片、文件讲解及实战(超详细实现用户注册信息上传 附源码)
1227 0
|
7月前
|
网络协议 物联网
VB6网络通信软件上位机开发,TCP网络通信,读写数据并处理,完整源码下载
本文介绍使用VB6开发网络通信上位机客户端程序,涵盖Winsock控件的引入与使用,包括连接服务端、发送数据(如通过`Winsock1.SendData`方法)及接收数据(利用`Winsock1_DataArrival`事件)。代码实现TCP网络通信,可读写并处理16进制数据,适用于自动化和工业控制领域。提供完整源码下载,适合学习VB6网络程序开发。 下载链接:[完整源码](http://xzios.cn:86/WJGL/DownLoadDetial?Id=20)
237 12
|
8月前
|
运维 安全 网络安全
VMware NSX 4.2.1.3 下载 - 网络安全虚拟化平台
VMware NSX 4.2.1.3 下载 - 网络安全虚拟化平台
240 0
VMware NSX 4.2.1.3 下载 - 网络安全虚拟化平台
|
存储 网络架构
网络速率与下载速率
【8月更文挑战第8天】
2047 1
网络速率与下载速率
java代码实现使用网络地址下载文件
java代码实现使用网络地址下载文件
435 0
【干货】Python下载网络小说
【干货】Python下载网络小说
|
数据采集 存储 数据安全/隐私保护
拓展网络技能:利用lua-http库下载www.linkedin.com信息的方法
本文介绍如何使用Lua和lua-http库抓取LinkedIn信息,强调了Lua在爬虫开发中的应用。通过配置亿牛云爬虫代理解决IP封锁问题,实现步骤包括安装库、配置代理、发送HTTP请求、解析响应及提取信息。提供的Lua代码示例展示了下载和存储LinkedIn信息的过程。实验成功展示了Lua爬虫的可行性,但也指出需考虑反爬虫策略以应对实际挑战。
150 1
拓展网络技能:利用lua-http库下载www.linkedin.com信息的方法
|
数据采集 存储 Scala
挖掘网络宝藏:利用Scala和Fetch库下载Facebook网页内容
本文介绍了如何使用Scala和Fetch库下载Facebook网页内容,同时通过爬虫代理服务(以亿牛云为例)绕过网络限制。代码示例展示了配置代理服务器、多线程爬取及内容存储的过程。注意实际应用时需替换代理服务器配置和目标URL,并考虑应对复杂的反爬虫机制。此方法兼顾匿名性和效率。
238 3
挖掘网络宝藏:利用Scala和Fetch库下载Facebook网页内容