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();
|
}
|
|
}
|
}
|