fwq
2024-04-11 f31ae0176ba124a2a39121584f5c39c3de932157
提交 | 用户 | age
7c680b 1 package com.hz.his.util;
C 2
3 import feign.Response;
4
5 import java.io.File;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.InputStream;
9
10 /**
11  * 文件处理器
12  */
13 public class FileDownUtil {
14
15     /**
16      * 获取文件转化工具
17      * @param response 微服务返回
18      * @param fileUrl 文件路径
19      * @return
20      */
21     public static File downToFile(Response response,String fileUrl){
22         Response.Body body = response.body();
23
24         InputStream inputStream = null;
25         String filePath ="";
26         FileOutputStream fos = null;
27         File downloadFile = null;
28
29         try {
30             //获取response中的文件流
31             inputStream = body.asInputStream();
32
33             //临时文件
34             String suffix = fileUrl.substring(fileUrl.lastIndexOf("."));
35             downloadFile = File.createTempFile("temp/", suffix);
36             //写入文件
37             fos= new FileOutputStream(downloadFile);
38             byte[] c = new byte[1024];
39             int length;
40             while((length= inputStream.read(c))>0){
41                 fos.write(c,0,length);
42             }
43         } catch (IOException e1) {
44             e1.printStackTrace();
45         }finally{
46             try {
47                 inputStream.close();
48             } catch (IOException e) {
49                 e.printStackTrace();
50             }
51             try {
52                 fos.close();
53             } catch (IOException e) {
54                 e.printStackTrace();
55             }
56             if(downloadFile != null){
57                 downloadFile.deleteOnExit();
58             }
59         }
60         return downloadFile;
61     }
62
63 }