地质所 沉降监测网建设项目
zmk
2024-05-15 9e3afc6d0fa514f986d3fea40fa23124e6fb5070
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.javaweb.cms.util;
 
import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import com.javaweb.common.config.Global;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.common.utils.file.FileUploadUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class DownloadImageUtil {
    static Logger logger = LoggerFactory.getLogger(DownloadImageUtil.class);
    static Cache<String, String> downloadCache = CacheUtil.newLRUCache(1000);
 
    public static void main(String[] args) {
        System.out.println(downloadImageToLocal("http://old.duoguyu.com/uploads/201809/09/180909053041227.jpg","/test/test2"));
    }
    /**
     * 检测一个网络路径文件是否存在
     * @param url
     * @return
     */
    public static boolean checkNetUrlExists(String url){
        try{
            URL u = new URL(url);
            HttpURLConnection urlcon = (HttpURLConnection) u.openConnection();
            if(urlcon.getResponseCode()==200){
                return true;
            }
        }catch (Exception ex){
        }
        return false;
    }
 
    /**
     * 本地化网络路径图片
     * @param urlPath 网络图片
     * @param appendPath 追加一层目录如/book或/article
     * @return 返回保存到本地的相对路径
     */
    public static String downloadImageToLocal(String urlPath,String appendPath){
        try{
            if(!checkNetUrlExists(urlPath)){
                return "";
            }
            String saveBathPath=Global.getUploadPath();
            if(StringUtils.isNotEmpty(appendPath)){
                appendPath=appendPath.startsWith("/")?appendPath:"/"+appendPath;
                saveBathPath+=appendPath;
            }
            CloseableHttpClient httpclient = HttpClients.createDefault();
            //这里通过httpclient下载之前抓取到的图片网址,并放在对应的文件中
            //String urlPath="http://v3cdn.duoguyu.com/oss/uploads/201803/08/180308100445561.jpg";
            HttpGet httpget = new HttpGet(urlPath);
            String extension=com.google.common.io.Files.getFileExtension(urlPath);
            String name=com.google.common.io.Files.getNameWithoutExtension(urlPath);
            String fileName = DateUtils.datePath() + "/" + FileUploadUtils.encodingFilename(name) + "." + extension;
            File file = FileUploadUtils.getAbsoluteFile(saveBathPath,fileName);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            InputStream in = entity.getContent();
            try {
                FileOutputStream fout = new FileOutputStream(file);
                int l = -1;
                byte[] tmp = new byte[1024];
                while ((l = in.read(tmp)) != -1) {
                    fout.write(tmp,0,l);
                }
                fout.flush();
                fout.close();
            } finally {
                in.close();
            }
            return FileUploadUtils.getPathFileName(saveBathPath,fileName);
        }catch (Exception ex){
            ex.printStackTrace();
            return "error";
        }
    }
 
}