chenjiahe
2022-07-13 7c680b3e59d8b46fa4797f283101476917fa44c6
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
package com.hz.his.util;
 
import feign.Response;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
/**
 * 文件处理器
 */
public class FileDownUtil {
 
    /**
     * 获取文件转化工具
     * @param response 微服务返回
     * @param fileUrl 文件路径
     * @return
     */
    public static File downToFile(Response response,String fileUrl){
        Response.Body body = response.body();
 
        InputStream inputStream = null;
        String filePath ="";
        FileOutputStream fos = null;
        File downloadFile = null;
 
        try {
            //获取response中的文件流
            inputStream = body.asInputStream();
 
            //临时文件
            String suffix = fileUrl.substring(fileUrl.lastIndexOf("."));
            downloadFile = File.createTempFile("temp/", suffix);
            //写入文件
            fos= new FileOutputStream(downloadFile);
            byte[] c = new byte[1024];
            int length;
            while((length= inputStream.read(c))>0){
                fos.write(c,0,length);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }finally{
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(downloadFile != null){
                downloadFile.deleteOnExit();
            }
        }
        return downloadFile;
    }
 
}