LiJun
2018-11-17 7ce7005333154eb9a0da1461604bcc0e06f1b4cc
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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.util.ArrayList;
import java.util.List;
 
import javax.servlet.http.HttpServletResponse;
 
import com.fgkj.dto.BattInf;
import com.fgkj.dto.MyFile;
import com.fgkj.dto.ServiceModel;
import com.google.gson.Gson;
 
public class MyFileAction extends ActionUtil{
    
    private String result;
    private String json;
    private String battname;
    private File[] file;
    // 提交过来的file的名字
    private String[] fileFileName;
    // 提交过来的file的MIME类型
    private String[] fileContentType;
    
    private String filestr;                //文件内容
    private String fname;                //文件内容
    
    //上传文件
    public String UploadTestFile(){
        HttpServletResponse res = ActionUtil.getResponse();
        res.setHeader("Access-Control-Allow-Origin", "*");                //允许跨域访问
        res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type,token");
        res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
 
        
        boolean f = createFile();
        ServiceModel model = new ServiceModel();
        if(f){
            model.setCode(1);
            model.setMsg("上传成功");
        }else{
            model.setCode(0);
            model.setMsg("上传失败");
        }
        result = ActionUtil.tojson(model);
        return SUCCESS;
    }
    
    public boolean createFile(){
        String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
        String str = new File(loadpath).getParentFile().getAbsolutePath();
        boolean flag = false;
        String root = str+"/uploadfiles/";        // 上传路径
        //System.out.println(battname+"***");
        for(int i=0;i<file.length;i++){
            String savepath=root+battname+"/"+fileFileName[i];
            //System.out.println(savepath);
            FileInputStream fis=null;
            FileOutputStream fos=null;
            try {                      
                File f = new File(savepath);
                if(!f.exists()){
                    if(!f.getParentFile().exists()){
                        f.getParentFile().mkdirs();
                    }
                    f.createNewFile();
                }
                /** 创建输入流 */      
                fis = new FileInputStream(file[i]);       
                /** 输出流 根据文件的路径+文件名称创建文件对象 */      
                fos = new FileOutputStream(f);       
                /** 读取字节   */      
                byte b[] = new byte[1024];       
                int n = 0;       
                /** 读取操作   */      
                while ((n = fis.read(b)) != -1) {       
                    /** 写操作   */      
                    fos.write(b, 0, n);       
                }    
                fos.flush();
                flag = true;
            } catch (Exception e) {       
                e.printStackTrace();
            } finally{
                 /** 关闭操作  */      
                try {
                    if (fis != null) {       
                        fis.close();       
                    }       
                    if (fos != null) {       
                        fos.close();       
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag; 
    }
    
    public String UploadIphoneFile(){
        HttpServletResponse res = ActionUtil.getResponse();
        res.setHeader("Access-Control-Allow-Origin", "*");                //允许跨域访问
        res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type,token");
        res.setHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
        Gson gson = new Gson();
        int[] arr = gson.fromJson(filestr, int[].class);
        
        System.out.println("文件名"+fname);
        System.out.println("文件内容:"+filestr);
        System.out.println("电池组名称"+battname);
        boolean f = createFileByarr(arr,fname,battname);
        
        ServiceModel model = new ServiceModel();
        if(f){
            model.setCode(1);
            model.setMsg("上传成功");
        }else{
            model.setCode(0);
            model.setMsg("上传失败");
        }
        result = ActionUtil.tojson(model);
        return SUCCESS;
    }
    
    /**
     * 
     * @param arr            文件的字节数组
     * @param fname            文件名
     * @param bname            电池组名称
     * @return                是否生成文件成功
     */
    public boolean createFileByarr(int[] arr,String fname,String bname){
        String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
        String str = new File(loadpath).getParentFile().getAbsolutePath();
        boolean flag = false;
        String root = str+"/uploadfiles/";        // 上传路径
        //System.out.println(battname+"***");
        String savepath=root+bname+"/"+fname;
        //System.out.println(savepath);
        FileOutputStream fos=null;
        byte[] bytes = new byte[arr.length];
        for(int i=0;i<arr.length;i++){
            bytes[i] = (byte)(arr[i] & 0xFF);
        }        
        
        try {                      
            File f = new File(savepath);
            if(!f.exists()){
                if(!f.getParentFile().exists()){
                    f.getParentFile().mkdirs();
                }
                f.createNewFile();
            }    
            fos = new FileOutputStream(savepath);
            fos.write(bytes);
            fos.flush();
            flag = true;
        } catch (Exception e) {       
            e.printStackTrace();
        } finally{
             /** 关闭操作  */      
            try {
                if (fos != null) {       
                    fos.close();       
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return flag; 
    }
    
    //上传机房视频接口
    public String uploadStationFile(){
        BattInf binf = getGson().fromJson(json, BattInf.class);
        String fileRoot = "stationsrc";
        ServiceModel model = new ServiceModel();    
        boolean isSuccess = false;
        String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
        String str = new File(loadpath).getParentFile().getAbsolutePath();                                                    //获取服务器所在的绝对路径
        String root = str+"/"+fileRoot+"/"+binf.getStationId()+"/video/";                                                    // 上传路径
        for(int i=0;i<file.length && binf != null;i++){
            String filePath = root+fileFileName[i];
            createFilefolderIFNotExist(filePath);
            copyFile(file[i], filePath);
            isSuccess = true;
        }
        if(isSuccess){
            model.setCode(1);
            model.setMsg("上传成功");
        }else{
            model.setCode(0);
            model.setMsg("上传失败");
        }
        result = tojson(model);
        return SUCCESS;
    }
    
    /**
     * 
     *     获取指定机房得资源
     */
    public String searchStationSource(){
        String fileRoot = "stationsrc";                                                            //存放机房资源文件的文件夹
        BattInf binf = getGson().fromJson(json, BattInf.class);
        ServiceModel model = new ServiceModel();
        String loadpath = ActionUtil.getSession().getServletContext().getRealPath("/");
        String str = new File(loadpath).getParentFile().getAbsolutePath();    
        if(binf != null && binf.getStationId() != null && binf.getStationName() != null){
            String filesource = str+File.separator+fileRoot+File.separator+binf.getStationId()+File.separator+binf.getStationName()+File.separator;                //当前请求的机房所在的资源文件夹
            System.out.println(filesource);
            File source = new File(filesource);
            File[] files = source.listFiles();
            model.setMsg("暂无视频文件");
            if(files.length>0){
                List myfiles = new ArrayList<MyFile>();
                for(int i=0;i<files.length;i++){
                    MyFile myfile = new MyFile();
                    if(files[i].isFile()){
                        myfile.setFile(files[i]);
                        myfiles.add(myfile);
                    }
                }
                if(myfiles.size() > 0){
                    model.setCode(1);
                    model.setData(myfiles);
                    model.setMsg("读取成功");
                }
            }
            
        }
        result = tojson(model);
        return SUCCESS;
    }
    
    //将文件复制到指定的目录
    public static void copyFile(File oldFile, String newPath){
        File file = new File(newPath);
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(oldFile);
            out = new FileOutputStream(file);;
 
            byte[] buffer=new byte[2097152];
            int readByte = 0;
            while((readByte = in.read(buffer)) != -1){
                out.write(buffer, 0, readByte);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
 
 
    public void setFilestr(String filestr) {
        this.filestr = filestr;
    }
 
    public void setFname(String fname) {
        this.fname = fname;
    }
 
    public void setFile(File[] file) {
        this.file = file;
    }
 
    public void setFileFileName(String[] fileFileName) {
        this.fileFileName = fileFileName;
    }
 
 
    public void setFileContentType(String[] fileContentType) {
        this.fileContentType = fileContentType;
    }
 
    public String getResult() {
        return result;
    }
 
    public void setBattname(String battname) {
        this.battname = battname;
    }    
    
    
    
    public void setJson(String json) {
        this.json = json;
    }
 
    public static void main(String[] args) {
        String filePath = "D:/test/a/a.txt";
        //ActionUtil.createFileRootIFNotExist(filePath);
        
        File file = new File(filePath);
        System.out.println(ActionUtil.tojson(file));
        
    }
}