IT-KIMI_SHI\SINOIT.KIMI
2018-06-04 b12dfac6e495d6cf38df6b7e5222191f59762900
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
package report;
 
import java.io.File;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpStatus;
 
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
 
import foundation.callable.Callable;
import foundation.config.Configer;
import foundation.data.Entity;
import foundation.file.download.ClientAction;
import foundation.file.download.HttpResponseWriter;
import foundation.persist.DataHandler;
import foundation.persist.sql.SQLRunner;
import foundation.util.Util;
 
public class Console extends Callable {
 
    protected void publishMethod() {
        addMethod("getNews");
        addMethod("export");
        addMethod("uplodeFile");
        addMethod("text");
    }
    
    protected void text() throws Exception {
        String sql = "{call Pro_DistributorHierarchy_Import}";
        SQLRunner.execSQL(sql);
    }
    
    protected void getNews() throws Exception {
        String id = dataPool.getParameter("id").getStringValue();
        
        Entity entity = DataHandler.getLine("announcement", id);
        
        resultPool.addValue("title", entity.getString("title"));
        resultPool.addValue("contents", URLEncoder.encode(entity.getString("contents"), "UTF-8"));
    }
    
    protected void export() throws Exception {
        String params = request.getQueryString();
        
        HttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet("http://localhost:3456/api/FileCenter/Download?" + params);
        
        HttpResponse httpResponse = httpclient.execute(httpget);
        StatusLine statusLine = httpResponse.getStatusLine();
        
         if (statusLine.getStatusCode() == 200) {
              String strResult = EntityUtils.toString(httpResponse.getEntity());  
              JsonObject returnData = new JsonParser().parse(strResult).getAsJsonObject();
              JsonElement jsonElement = returnData.get("FilePath");
              String path = jsonElement.getAsString();
 
              File file = new File(path);
              
              if (!file.exists()) {
                  resultPool.error("file not exists: " + file);
                  return;
              }
              
              HttpResponseWriter writer = new HttpResponseWriter(response);
              writer.write(file, ClientAction.AsExcel);
        }        
    }
    
    protected void uplodeFile() throws FileUploadException {
        DiskFileItemFactory factory = new DiskFileItemFactory(); 
        ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
        servletFileUpload.setHeaderEncoding("UTF-8");   
        List<FileItem> items;
        items = servletFileUpload.parseRequest(request);
        
        if (items.isEmpty()) {
            resultPool.error("请选择上传文件");
            return;
        }
        
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
        String now = format.format(new Date());
        String filesPath = Configer.getParam("uploadFilePath") + "\\" + now;
        
        for (FileItem fileItem : items) {
            String fileName = fileItem.getName().substring(fileItem.getName().lastIndexOf("\\") + 2);;
            
            byte[] bs = fileItem.get();
            Util.getFile(bs, filesPath, fileName);
        }
        
        if (!dataPool.getParameter("toDB").isEmpty()) {
            upload2DB(filesPath);
        }
        
        resultPool.addValue("path", filesPath);
    }
    protected void upload2DB(String filesPath) {
        try {
            HttpClient httpclient = HttpClients.createDefault();
            String url = "http://localhost:3456/api/FileCenter/Upload?filespath=" + filesPath;
            String replace = url.replace("\\", "/");
            HttpGet httpget = new HttpGet(replace);
            
            HttpResponse httpResponse = httpclient.execute(httpget);
            StatusLine statusLine = httpResponse.getStatusLine();
            
             if (statusLine.getStatusCode() == HttpStatus.OK.value()) {
                  String strResult = EntityUtils.toString(httpResponse.getEntity());  
                  JsonObject returnData = new JsonParser().parse(strResult).getAsJsonObject();
                  Boolean success = returnData.get("Success").getAsBoolean();
                  
                /*  JsonArray array=returnData.get("Files").getAsJsonArray();  
                  String filepath = "";
                  JsonArray asJsonArray = returnData.getAsJsonArray("Files");
                    for(int i=0;i<array.size();i++){
                        System.out.println("---------------");
                        JsonObject subObject=array.get(i).getAsJsonObject();
                         filepath =  subObject.get("FilePath").getAsString();
                    }
                  File file = new File(filepath);
                  
                  if (!file.exists()) {
                      resultPool.error("file not exists: " + file);
                      return;
                  }*/
                  
                  /*HttpResponseWriter writer = new HttpResponseWriter(response);
                  writer.write(file, ClientAction.AsExcel);*/
            }
            
        } catch (Exception e) {
 
            e.printStackTrace();
        } 
    }
 
 
}