liuxiaolong
2019-05-06 71af9c46c24b562acc00a71ec6c97c04eb048d9c
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.cloud.common.utils;
 
 
import com.cloud.common.model.FileInfos;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.UUID;
 
@Configuration
public class FastDFSUtil {
 
    @Autowired
    private FastFileStorageClient fastFileStorageClient;
 
    public FileInfos upload(MultipartFile file) {    //上传文件
 
        try {
 
            String ext_Name = file.getOriginalFilename().split("\\.")[1];
 
            byte[] bytes = null;
            try {
                bytes = file.getBytes();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //获取文件大小
            long fileSize = file.getSize();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
 
            StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, ext_Name, null);
//            String fileIds[] = storageClient.upload_file(bytes, ext_Name, null);
            //获取文件信息,并保存至数据库
            FileInfos fileInfo = new FileInfos();
            fileInfo.setId(UUID.randomUUID().toString());
            fileInfo.setName(file.getOriginalFilename());
            fileInfo.setExtName(ext_Name);
            fileInfo.setPath(storePath.getFullPath());
            fileInfo.setSize(file.getSize());
            fileInfo.setCreateTime(new Date());
 
            return fileInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
 
    }
 
 
    public FileInfos uploadByByte(byte[] bytes, String extName) {    //上传文件
        try {
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, bytes.length, extName, null);
//            String fileIds[] = storageClient.upload_file(bytes, extName, null);
            //获取文件信息,并保存至数据库
            FileInfos fileInfo = new FileInfos();
 
            fileInfo.setExtName(extName);
            fileInfo.setPath(storePath.getFullPath());
            fileInfo.setCreateTime(new Date());
 
            return fileInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
 
    }
 
    /**
     * 根据图片的FastDFS地址获得数据的字节流
     * @param strUrl 网络连接地址
     * @return
     */
    public  byte[] getImageFromFDFS(String strUrl){
        try {
            URL url = new URL(strUrl);
            System.out.println("url是:"+strUrl);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            InputStream inStream = conn.getInputStream();//获取从网络的输入流
            byte[] btImg = new byte[inStream.available()];
            inStream.read(btImg);         //从输入流中得到图片的二进制数据
            return btImg;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public byte[] downloadFDFS(String fileUrl) throws Exception {
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        DownloadByteArray downloadByteArray = new DownloadByteArray();
        return fastFileStorageClient.downloadFile(group, path, downloadByteArray);
    }
}