whycrzg
2021-02-23 351b9a53cb9ecebdf8f79db0117f540d9c42c2a4
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.fgkj.controller;
 
import com.fgkj.util.*;
 
import com.fgkj.dto.User_Chart;
import com.fgkj.services.User_ChartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Decoder;
 
import java.io.*;
import java.util.*;
import javax.annotation.Resource;
 
@RequestMapping("chart")
@RestController
@Api(tags = "chart接口")
public class ChartController{
 
    private static Map<String,String> fileUtil = new HashMap<String, String>(){
        {
            put("logo","logo.png");
            put("normal","home_normal.gif");
            put("behind","home_behind.gif");
            put("timeout","home_timeout.gif");
            put("warn","home_warn.gif");
        }        
    };
 
    @Resource
    private User_ChartService service;
 
    // private List<File> files;
    // private List<String> filesFileName;
    //上传文件内容类型集合  
    // private String josn;
    
    //执行上传功能  
    private void uploadFile(File file,String webpath) {  
        InputStream in =null;
        OutputStream out =null;
        try {   
            File fileLocation = new File(webpath).getParentFile(); 
            //此处也可以在应用根目录手动建立目标上传目录 
            if(!fileLocation.exists()){ 
                boolean isCreated = fileLocation.mkdir(); 
                if(!isCreated) { 
                    //目标上传目录创建失败,可做其他处理,例如抛出自定义异常等,一般应该不会出现这种情况。 
                    System.out.println("创建失败!");
                } 
            } 
            in = new FileInputStream(file);
            File uploadFile = new File(webpath);
            out = new FileOutputStream(uploadFile);  
            byte[] buffer = new byte[1024 * 1024];  
            int length;  
            while ((length = in.read(buffer)) > 0) {  
                out.write(buffer, 0, length);  
            }
            out.flush();
          }catch (FileNotFoundException e) {
              e.printStackTrace();
          }catch (IOException e) {
              e.printStackTrace(); 
          }finally{
              try {
                  if (in!= null) {       
                      in.close();       
                  }       
                  if (out != null) {       
                      out.close();       
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }  
    }
 
    @PutMapping("form")
    @ApiOperation(notes = "",value="form")
    public boolean uploadform(@RequestBody User_Chart uchart,@RequestBody List<File> files,@RequestBody List<String> filesFileName){
        // User_Chart uchart=ActionUtil.getGson().fromJson(json, User_Chart.class);
        boolean res=false;
        String webpath=null;//上传路径
        //System.out.println(uchart);
        String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
        String root = new File(loadpath).getAbsolutePath();
        System.out.println("root:"+root);
        for (int i = 0; i < files.size(); i++) { 
            System.out.println("filename:"+filesFileName.get(i));            
            File file=files.get(i);
            //循环上传每个文件  
            webpath=root+"/images/"+uchart.getChart_file()+"/"+filesFileName.get(i);
            System.out.println(webpath);
            //uploadFile(file,webpath); 
        }
        System.out.println("完成");
        //model=service.add(uchart);
        res=true;
        return res;
    }
    
    //修改班组的logo
    @PutMapping("serverRootLogo")
    @ApiOperation(notes = "",value="修改班组的logo")
    public boolean updateServerRootLogo(HashMap<String,String> s){
        boolean res=false;
        boolean isSuccess = false;
        //获取服务器上文件存放的根路径
        String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
        String str = new File(loadpath).getParentFile().getAbsolutePath();
        String root = str+"/images/";
        // Map<String,String> s = getGson().fromJson(json, new TypeToken<HashMap<String,String>>(){}.getType());
        String dirname = s.get("chart_file");
        if(dirname != null && dirname.length()>0){
            root += dirname + "/";        
        }
        List<String> bases = new ArrayList<String>();
        List<String> filepath = new ArrayList<String>();
        Iterator iter = s.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            String fname = fileUtil.get(key);
            if(fname!=null && fname.length()>0){
                bases.add(entry.getValue().toString());
                filepath.add(root+fname);
                System.out.println(root+fname);
            }
            Object val = entry.getValue();
        }
        //System.out.println(bases.size());
        //System.out.println(filepath.size());
        for(int i=0;i<bases.size();i++){
            String pattern = "^data:image\\/\\w+;base64,";
            String basestr = bases.get(i).replaceFirst(pattern, "");
            ActionUtil.generateImage(basestr,filepath.get(i));
        }
        res=true;
 
        return res;
    }
    
    
    private void createFileByBase64(String path,String base64){
        base64 =base64.replaceAll("data:image/jpeg;base64,", "");      
        BASE64Decoder decoder = new BASE64Decoder();      
        try {      
             // Base64解码      
             byte[] b = decoder.decodeBuffer(base64);      
             for (int i = 0; i < b.length; ++i) {      
                 if (b[i] < 0) {// 调整异常数据      
                     b[i] += 256;      
                 }      
             }      
             // 生成jpeg图片      
             
             File file = new File(path);    
             OutputStream out = new FileOutputStream(file);      
             out.write(b);      
             out.flush();      
             out.close();      
         } catch (Exception e) {      
             e.printStackTrace();      
         }
    }
 
}