whyclj
2019-10-29 1c0469e45346d464e0c5672ee68f9ecd4fb6be7c
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
174
175
176
177
178
179
180
181
182
183
package com.fgkj.actions;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import sun.misc.BASE64Decoder;
 
import com.fgkj.dto.ServiceModel;
import com.fgkj.dto.User_Chart;
import com.fgkj.services.User_ChartService;
import com.google.gson.reflect.TypeToken;
import com.mysql.fabric.xmlrpc.base.Array;
 
public class ChartAction extends ActionUtil{
    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");
        }        
    };
    private User_ChartService service=new User_ChartService();
    private String result;
    private String json;
    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();
              }
          }  
    }
    public String uploadform(){
        //System.out.println(json+"*********************");
        ServiceModel model = new ServiceModel();
        User_Chart uchart=ActionUtil.getGson().fromJson(json, User_Chart.class);
        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()+"/"+this.filesFileName.get(i);
            System.out.println(webpath);
            //uploadFile(file,webpath); 
        }
        System.out.println("完成");
        //model=service.add(uchart);
        result = ActionUtil.tojson(model);
        return SUCCESS;
    }
    
    //修改班组的logo
    public String updateServerRootLogo(){
        ServiceModel model = new ServiceModel();
        boolean isSuccess = false;
        //获取服务器上文件存放的根路径
        String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
        String str = new File(loadpath).getParentFile().getAbsolutePath();
        String root = str+"/images/";
        //System.out.println(json);
        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));
        }
        result = tojson(model);
        return SUCCESS;
    }
    
    
    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();      
         }
    }
    
    public String getResult() {
        return result;
    }
    
    public void setJson(String json) {
        this.json = json;
    }
    
    public void setFiles(List<File> files) {
        this.files = files;
    }
    
    public void setFilesFileName(List<String> filesFileName) {
        this.filesFileName = filesFileName;
    }  
}