|
|
|
@ -3,9 +3,12 @@ package com.platform.controller;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.io.RandomAccessFile;
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
@ -17,6 +20,8 @@ import javax.servlet.ServletException;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
|
|
import net.sf.json.JSONArray;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
@ -26,6 +31,7 @@ import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.ui.ModelMap;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
@ -52,15 +58,15 @@ public class ExcelController extends BaseController{
|
|
|
|
|
@Resource(name = "preDataInfoService")
|
|
|
|
|
private IPreDataInfoService preDataInfoService;
|
|
|
|
|
|
|
|
|
|
// 文件上传处理函数
|
|
|
|
|
// 文件上传处理函数
|
|
|
|
|
@RequestMapping(value = "/file/upload", method = RequestMethod.POST)
|
|
|
|
|
public void upload(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
|
|
int resumableChunkNumber = HttpUtils.toInt(
|
|
|
|
|
request.getParameter("resumableChunkNumber"), -1);
|
|
|
|
|
ResumableInfo info = getResumableInfo(request);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String requestMethod = request.getMethod();
|
|
|
|
|
// 处理GET请求
|
|
|
|
|
if ("GET".equals(requestMethod)) {
|
|
|
|
@ -78,7 +84,7 @@ public class ExcelController extends BaseController{
|
|
|
|
|
// Seek to position
|
|
|
|
|
raf.seek((resumableChunkNumber - 1)
|
|
|
|
|
* (long) info.resumableChunkSize);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save to file
|
|
|
|
|
InputStream is = request.getInputStream();
|
|
|
|
|
long readed = 0;
|
|
|
|
@ -93,23 +99,26 @@ public class ExcelController extends BaseController{
|
|
|
|
|
readed += r;
|
|
|
|
|
}
|
|
|
|
|
raf.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mark as uploaded.
|
|
|
|
|
info.uploadedChunks.add(new ResumableInfo.ResumableChunkNumber(
|
|
|
|
|
resumableChunkNumber));
|
|
|
|
|
if (info.checkIfUploadFinished()) { // Check if all chunks uploaded,
|
|
|
|
|
// and change filename
|
|
|
|
|
log.info(info.resumableFilename);
|
|
|
|
|
ResumableInfoStorage.getInstance().remove(info);
|
|
|
|
|
response.getWriter().print("All finished.");
|
|
|
|
|
//开始导入excel
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
response.getWriter().print("Upload");
|
|
|
|
|
}
|
|
|
|
|
} else { // 不处理非GET/POST请求
|
|
|
|
|
throw new IllegalStateException("只接受 POST或GET请求");
|
|
|
|
|
}
|
|
|
|
|
log.info("----");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 文件下载处理函数
|
|
|
|
|
@RequestMapping(value = "/file/download")
|
|
|
|
|
public ResponseEntity<byte[]> download(HttpServletRequest request,
|
|
|
|
@ -134,7 +143,7 @@ public class ExcelController extends BaseController{
|
|
|
|
|
|
|
|
|
|
// 文件上传处理函数
|
|
|
|
|
private ResumableInfo getResumableInfo(HttpServletRequest request)
|
|
|
|
|
throws ServletException {
|
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
String base_dir = Configs.FILE_UPLOAD_PATH;
|
|
|
|
|
|
|
|
|
|
int resumableChunkSize = HttpUtils.toInt(
|
|
|
|
@ -143,7 +152,9 @@ public class ExcelController extends BaseController{
|
|
|
|
|
request.getParameter("resumableTotalSize"), -1);
|
|
|
|
|
String resumableIdentifier = request
|
|
|
|
|
.getParameter("resumableIdentifier");
|
|
|
|
|
String resumableFilename = request.getParameter("resumableFilename");
|
|
|
|
|
String fileName = new String(request.getParameter("resumableFilename").getBytes("iso-8859-1"),
|
|
|
|
|
"UTF-8");// 为了解决中文名称乱码问题
|
|
|
|
|
String resumableFilename = fileName /*request.getParameter("resumableFilename")*/;
|
|
|
|
|
String resumableRelativePath = request
|
|
|
|
|
.getParameter("resumableRelativePath");
|
|
|
|
|
// Here we add a ".temp" to every upload file to indicate NON-FINISHED
|
|
|
|
@ -204,14 +215,89 @@ public class ExcelController extends BaseController{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ResponseBody
|
|
|
|
|
@RequestMapping("/importExcel")
|
|
|
|
|
public ModelMap importExcel(String... paths) throws Exception {
|
|
|
|
|
@RequestMapping(produces = "application/json", value = "/importExcel", method = RequestMethod.POST)
|
|
|
|
|
public ModelMap importExcel(HttpServletRequest request,
|
|
|
|
|
HttpServletResponse response, @RequestBody String fileNameList) throws Exception {
|
|
|
|
|
log.info("---importExcel---" + fileNameList);
|
|
|
|
|
JSONArray jsonArray = JSONArray.fromObject(fileNameList);
|
|
|
|
|
List<String> jsonlist = (List<String>) JSONArray.toCollection(jsonArray, String.class);
|
|
|
|
|
|
|
|
|
|
ModelMap modelMap = new ModelMap();
|
|
|
|
|
if (null == jsonlist && jsonlist.size() > 0) {
|
|
|
|
|
System.err.println(request.getParameter("fileNameList"));
|
|
|
|
|
modelMap.addAttribute("returncode","3002");
|
|
|
|
|
return modelMap;
|
|
|
|
|
}
|
|
|
|
|
log.info(jsonlist.size());
|
|
|
|
|
List<String> listPath = new ArrayList<String>();
|
|
|
|
|
listPath.addAll(Arrays.asList(paths));
|
|
|
|
|
Map<String, String> result = preDataInfoService.importExcel(listPath);
|
|
|
|
|
|
|
|
|
|
modelMap.addAttribute("data", "success"+result);
|
|
|
|
|
for (String p : jsonlist) {
|
|
|
|
|
listPath.add(Configs.FILE_UPLOAD_PATH + p);
|
|
|
|
|
}
|
|
|
|
|
Map<String, List> result = new HashMap<String, List>();
|
|
|
|
|
if (listPath.size() > 0) {
|
|
|
|
|
result = preDataInfoService.importExcel(listPath);
|
|
|
|
|
if (result.containsKey("fileUnExist") || result.containsKey("areaUnImport")) {
|
|
|
|
|
modelMap.addAttribute("data", result);
|
|
|
|
|
modelMap.addAttribute("returncode","3001");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
List<PreDataInfo> datalist = preDataInfoService.findAll();
|
|
|
|
|
modelMap.addAttribute("data", datalist);
|
|
|
|
|
modelMap.addAttribute("length", datalist.size());
|
|
|
|
|
modelMap.addAttribute("returncode","1001");
|
|
|
|
|
}
|
|
|
|
|
//删除文件
|
|
|
|
|
for (String filePath : listPath) {
|
|
|
|
|
//读取完后删除源文件(不管是否成功导入)
|
|
|
|
|
File tmpf = new File(filePath + ".temp");
|
|
|
|
|
File f = new File(filePath);
|
|
|
|
|
if (tmpf.exists()) {
|
|
|
|
|
tmpf.delete();
|
|
|
|
|
}
|
|
|
|
|
if (f.exists()) {
|
|
|
|
|
f.delete();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return modelMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void uploadSpring(HttpServletRequest request, HttpServletResponse respone){
|
|
|
|
|
System.out.println(request.getAttribute("file"));
|
|
|
|
|
System.out.println(request.getParameter("file"));
|
|
|
|
|
//解析器解析request的上下文
|
|
|
|
|
CommonsMultipartResolver multipartResolver =
|
|
|
|
|
new CommonsMultipartResolver(request.getSession().getServletContext());
|
|
|
|
|
//先判断request中是否包涵multipart类型的数据,
|
|
|
|
|
if(multipartResolver.isMultipart(request)){
|
|
|
|
|
//再将request中的数据转化成multipart类型的数据
|
|
|
|
|
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
|
|
|
|
|
Iterator iter = multiRequest.getFileNames();
|
|
|
|
|
while(iter.hasNext()){
|
|
|
|
|
//这里的name为fileItem的alias属性值,相当于form表单中name
|
|
|
|
|
String name=(String)iter.next();
|
|
|
|
|
System.out.println("name:"+name);
|
|
|
|
|
//根据name值拿取文件
|
|
|
|
|
MultipartFile file = multiRequest.getFile(name);
|
|
|
|
|
if(file != null){
|
|
|
|
|
String fileName = file.getOriginalFilename();
|
|
|
|
|
String path = "D:/test/" + fileName;
|
|
|
|
|
File localFile = new File(path);
|
|
|
|
|
if(!localFile.getParentFile().exists()) {
|
|
|
|
|
//如果目标文件所在的目录不存在,则创建父目录
|
|
|
|
|
localFile.getParentFile().mkdirs();
|
|
|
|
|
System.out.println("parent:"+localFile.getParentFile().getPath());
|
|
|
|
|
}
|
|
|
|
|
//写文件到本地
|
|
|
|
|
try {
|
|
|
|
|
file.transferTo(localFile);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|