1
81041
2019-10-18 cc4059f9f5a2cd6766f4d888bd35d19f3827a053
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
package com.fgkj.actions;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
 
import javax.servlet.http.HttpSession;
 
import org.apache.struts2.ServletActionContext;
 
import com.fgkj.dto.DLG_Progress;
import com.fgkj.dto.ServiceModel;
import com.fgkj.dto.User;
import com.fgkj.dto.User_inf;
import com.fgkj.fbo.FboData;
import com.fgkj.fbo.FboDataInf;
import com.fgkj.services.FboDataUploadService;
import com.google.gson.Gson;
import com.mysql.fabric.xmlrpc.base.Array;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Result;
import com.sun.xml.internal.ws.wsdl.writer.document.Service;
 
public class FboDataUploadAction extends ActionSupport {
    private String tableRowData;
    private int battgroupid;
    private File[] file;
    // 提交过来的file的名字
    private String[] fileFileName;
    private String result;
 
    // 文件上传
    public String uploadFile() {
        User_inf user = (User_inf) ActionUtil.getUser();
        if(user!=null){
            // System.out.println(tableRowData.length());
            HttpSession session = ActionUtil.getSession();
            Gson gson = new Gson();
            createFile();
            String[][] tableRowDatas = gson
                    .fromJson(tableRowData, String[][].class);
            //System.out.println(file.length + "---" + tableRowDatas.length);
            FboDataUploadService fduservice = new FboDataUploadService(user,
                    battgroupid, tableRowDatas, file, session);
            fduservice.start();
        }
        return SUCCESS;
    }
 
    // 停止上传
    public String stopUpload() {
        HttpSession session = ActionUtil.getSession();
        DLG_Progress progress = (DLG_Progress) session.getAttribute("progress");
        if (progress != null) {
            progress.setJudge(false);
            ServiceModel model=new ServiceModel();
            model.setCode(1);
            model.setData(model);
            result = ActionUtil.tojson(progress);
        }
        return SUCCESS;
    }
 
    // 查询进度
    public String selectprogress() {
        HttpSession session = ActionUtil.getSession();
        DLG_Progress progress = (DLG_Progress) session.getAttribute("progress");
//        for(int i=0;i<progress.getTableData().length;i++){
//            for(int j=0;j<progress.getTableData()[i].length;j++){
//                System.out.print(progress.getTableData()[i][j]+"\t");
//            }
//            System.out.println();
//        }
        if (progress != null) {
            ServiceModel model = new ServiceModel();
            model.setCode(1);
            model.setData(progress);
            result = ActionUtil.tojson(model);
        }
        return SUCCESS;
    }
 
    public void createFile(){        
        String root = ServletActionContext.getServletContext().getRealPath("");        // 上传路径
        //System.out.println(root);
        for(int i=0;i<file.length;i++){
            long times=new Date().getTime();
            String savepath=root+"/"+times+fileFileName[i];
            File f=new File(savepath);
            FileInputStream fis=null;
            FileOutputStream fos=null;
            try {       
                if (!f.exists()) {       
                    /** 创建此文件对象路径  */      
                    f.createNewFile();       
                }       
                /** 创建输入流 */      
                fis = new FileInputStream(file[i]);       
                /** 输出流 更据文件的路径+文件名称创建文件对象 */      
                fos = new FileOutputStream(savepath);       
                /** 读取字节   */      
                byte b[] = new byte[1024];       
                int n = 0;       
                /** 读取操作   */      
                while ((n = fis.read(b)) != -1) {       
                    /** 写操作   */      
                    fos.write(b, 0, n);       
                }    
                fos.flush();
                file[i]=f;      
            } catch (Exception e) {       
                e.printStackTrace();       
            } finally{
                 /** 关闭操作  */      
                try {
                    if (fis != null) {       
                        fis.close();       
                    }       
                    if (fos != null) {       
                        fos.close();       
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }        
        
    }
 
    public static void main(String[] args) {
        new FboDataUploadAction().uploadFile();
    }
 
    public String getTableRowData() {
        return tableRowData;
    }
 
    public void setTableRowData(String tableRowData) {
        this.tableRowData = tableRowData;
    }
 
    public int getBattgroupid() {
        return battgroupid;
    }
 
    public void setBattgroupid(int battgroupid) {
        this.battgroupid = battgroupid;
    }
 
    public File[] getFile() {
        return file;
    }
 
    public void setFile(File[] file) {
        this.file = file;
    }
 
    public String getResult() {
        return result;
    }
 
    public void setResult(String result) {
        this.result = result;
    }
 
    public String[] getFileFileName() {
        return fileFileName;
    }
 
    public void setFileFileName(String[] fileFileName) {
        this.fileFileName = fileFileName;
    }
 
}