严智鑫
2025-11-13 945bc394f40d8af1072a53da9a94f24207124e6d
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
package com.northglass.web.common;
 
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Iterator;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
 
import com.northglass.entity.FileMeta;
import com.northglass.entity.RawPackageTxt;
import com.northglass.repository.RawPackageTxtDao;
import com.northglass.service.manage.ManageService;
import com.northglass.service.optpattern.OptPatternService;
 
@Controller
@RequestMapping(value="/upload")
public class UploadController {
 
    private static String WEB_ROOT_PATH;
    private static String TEMP_PATH;
    
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
    
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);
    
    @Autowired
    private OptPatternService optPatternService;
    
    @Autowired
    private ManageService manageService;
    
    @Autowired
    private RawPackageTxtDao rawPackageTxtDao;
    
    @RequestMapping(value = "/optpattern")
    @ResponseBody
    public FileMeta upload(Model model,HttpServletRequest request, HttpServletResponse response) {
        //初始化根目录
        initializeTempDir(request);
          
        //将当前上下文初始化给  CommonsMutipartResolver (多部分解析器) 
        MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
 
        //获取multiRequest 中所有的文件名
        @SuppressWarnings("rawtypes")
        Iterator iter = multiRequest.getFileNames();
        FileMeta fileMeta = null;
        while(iter.hasNext()){
             //一次遍历所有文件
             MultipartFile file=multiRequest.getFile(iter.next().toString());
             
             if(file!=null){ 
                 //上传
                fileMeta = new FileMeta();
                fileMeta.setFileName(file.getOriginalFilename());
                fileMeta.setFileSize(file.getSize() / 1024 + " Kb");
                fileMeta.setFileType(file.getContentType());
                try {
                    fileMeta.setBytes(file.getBytes());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                      File newFile = new File(TEMP_PATH,file.getOriginalFilename());
                    try {
                        file.transferTo(newFile);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
             } 
         }        
         //将封装的文件数据返回到前端
         return fileMeta;
    }
    
    @RequestMapping(method=RequestMethod.GET,value="/cancelUpload")
    public String manage(Model model,HttpServletRequest request) {
        //初始化根目录
        initializeTempDir(request);
        try {
            delFolder(TEMP_PATH);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "redirect:/shelfmanager/taskimport/";
    }
    
    @RequestMapping(method=RequestMethod.POST, value="/uploadSuccess")
    public String uploadOptPattern1(Model model,HttpServletRequest request, HttpServletResponse response) throws IOException {
            initializeTempDir(request);
            //string url="";
            double OrdersWidth=0;          //原片长
            double OrdersHeight=0;            //原片宽
            double OrdersThiness=0;        //原片厚
            double Width=0;                  //小片长
            double Height=0;                //小片宽
            double Thiness=0;                //小片厚
            String Groups="2";                //生产线
            int Count=1;                //小片数量
            int Number=0;                //一张大版开几张小片
            try {
                RawPackageTxt rawPackageTxt=new RawPackageTxt();
                Long MaxrawPackageTxt=rawPackageTxtDao.maxTxtNameid();
                rawPackageTxt.setTxt_name((Integer.valueOf(MaxrawPackageTxt+"")+1)+"");
                rawPackageTxt.setGroups(Groups);
                rawPackageTxt.setStatus("已下发");
                rawPackageTxt.setOperator("邓锦龙");
                rawPackageTxt.setOrdersWidth(OrdersWidth);
                rawPackageTxt.setOrdersHeight(OrdersHeight);
                rawPackageTxt.setOrdersThiness(OrdersThiness);
                rawPackageTxt.setWidth(Width);
                rawPackageTxt.setHeight(Height);
                rawPackageTxt.setThiness(Thiness);
                rawPackageTxt.setCount(Count);
                rawPackageTxt.setNumber(Number);
              rawPackageTxtDao.save(rawPackageTxt);
              
              //rawPackageTxtDao
              //singalGlassDao
              //patternDao
            return "redirect:/shelfmanager/taskimport";
        } catch (Exception e) {
            // TODO: handle exception
                System.out.println("产生任务出错请检查");
              return "redirect:/shelfmanager/taskimport";
        }    
 }  
/*  @RequestMapping(method=RequestMethod.POST, value="/uploadSuccess")
  public String uploadOptPattern1(Model model,HttpServletRequest request, HttpServletResponse response) throws IOException {
      initializeTempDir(request);
      
      File file1=new File(TEMP_PATH);
      String[] fileName = file1.list();
      String uploadResult =null;
      for(String name :fileName){
          File newFile=new File(TEMP_PATH+File.separator+name);
          RawPackageTxt rawPackageTxt = rawPackageTxtDao.findByTxtName(name.substring(0,name.lastIndexOf(".")));
          //判断数据库中是否存在此OPT文件名
          if(rawPackageTxt!=null){
              continue;
              
          }
          //不存在则 解析OPT文件  得到返回值
          uploadResult = optPatternService.processOptFile(newFile.getAbsolutePath(),name.substring(0,name.lastIndexOf(".")));
              
      }
      
            //删除文件
          try {
            delFolder(TEMP_PATH);
        } catch (IOException e) {
            e.printStackTrace();
        }
          //数据库中存在此 OPT文件,或者未上传文件
          if(uploadResult == null||uploadResult == ""){
              model.addAttribute("uploadResult", "alsohave");
              //response.sendRedirect("");
              return "redirect:/shelfmanager/taskimport";
          }
          //解析成功 ,数据库添加数据
          else if ("success".equals(uploadResult)) {
              model.addAttribute("uploadResult", uploadResult);
              return "redirect:/shelfmanager/taskimport";
          }
          //解析失败,数据库不加数据
          else if ("failure".equals(uploadResult)) {
                 model.addAttribute("uploadResult", uploadResult);
              return "redirect:/shelfmanager/taskimport";
          }
          //不明白  不应该出现的路径
        return "redirect:/shelfmanager/taskimport";
        
  }*/
  
  
 
    /**
     * 初始化目录
     * @param request
     */
    private void initializeTempDir(HttpServletRequest request) {
        if (WEB_ROOT_PATH == null || WEB_ROOT_PATH.equals("")) {
            WEB_ROOT_PATH = request.getSession().getServletContext().getRealPath("/");
            LOGGER.debug("WEB_ROOT_PATH: " + WEB_ROOT_PATH);
            
            TEMP_PATH = WEB_ROOT_PATH + File.separator + "static" + File.separator + "temp";
            LOGGER.debug("TEMP_PATH: " + TEMP_PATH);
            
            File tempDir = new File(TEMP_PATH);
            if (!tempDir.exists()) {
                tempDir.mkdirs();
            }
        }
    }
    /**
     * 删除项目中opt文件夹下的所有旧的opt
     * @param folderPath
     * @throws IOException 
     */
    public void delFolder(String folderPath) throws IOException
    {   
          try {   
              File file = new File(folderPath);      
                  String[] filelist = file.list(); 
                  if(filelist!=null){
                      for(int i=0;i<filelist.length;i++){   
                          File delfile=new File(folderPath+File.separator+filelist[i]);   
                              delfile.delete();       
                      }
                  }
                     
          }   
          catch(Exception e)   
          {   
            e.printStackTrace();
          }
          
      }
}