|
|
|
@ -0,0 +1,166 @@
|
|
|
|
|
|
|
|
package com.nmgs.controller;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.poi.excel.ExcelReader;
|
|
|
|
|
|
|
|
import cn.hutool.poi.excel.ExcelUtil;
|
|
|
|
|
|
|
|
import com.nmgs.config.ResultData;
|
|
|
|
|
|
|
|
import com.nmgs.util.LogUtil;
|
|
|
|
|
|
|
|
import com.nmgs.util.PubTools;
|
|
|
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
|
|
|
import io.swagger.annotations.ApiImplicitParam;
|
|
|
|
|
|
|
|
import io.swagger.annotations.ApiImplicitParams;
|
|
|
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
|
|
|
@RequestMapping("/whiteBatchImport")
|
|
|
|
|
|
|
|
@Api(tags = "白名单批量导入")
|
|
|
|
|
|
|
|
public class WhiteBatchImportController {
|
|
|
|
|
|
|
|
//实现Spring Boot 的文件下载功能,映射网址为/download
|
|
|
|
|
|
|
|
@ApiOperation(value = "模板下载",httpMethod = "GET",ignoreJsonView = false )
|
|
|
|
|
|
|
|
@ApiImplicitParams(
|
|
|
|
|
|
|
|
@ApiImplicitParam(value = "fileName",defaultValue = "gasstation")
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
@RequestMapping(value = "/download", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
|
|
|
|
|
|
|
|
@ResponseBody
|
|
|
|
|
|
|
|
public String downloadFile(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
|
|
|
|
|
|
|
|
String fileName = request.getParameter("fileName");
|
|
|
|
|
|
|
|
if(PubTools.isNull(fileName)){
|
|
|
|
|
|
|
|
fileName="白名单模板空白";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName =fileName+".xlsx";
|
|
|
|
|
|
|
|
String fileP=com.nmgs.util.PathUtil.webappsPath+"//"+ fileName;
|
|
|
|
|
|
|
|
LogUtil.WriteLog_Info("模板下载地址====="+fileP,"WhiteBatchImportController");
|
|
|
|
|
|
|
|
File file = new File(fileP);
|
|
|
|
|
|
|
|
// 如果文件名存在,则进行下载
|
|
|
|
|
|
|
|
if (file.exists()) {
|
|
|
|
|
|
|
|
// 配置文件下载
|
|
|
|
|
|
|
|
response.setHeader("content-type", "application/octet-stream");
|
|
|
|
|
|
|
|
response.setContentType("application/octet-stream");
|
|
|
|
|
|
|
|
// 下载文件能正常显示中文
|
|
|
|
|
|
|
|
response.addHeader("Content-Length", String.valueOf(file.length()));
|
|
|
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 实现文件下载
|
|
|
|
|
|
|
|
byte[] buffer = new byte[1024*1024*100];
|
|
|
|
|
|
|
|
FileInputStream fis = null;
|
|
|
|
|
|
|
|
BufferedInputStream bis = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
fis = new FileInputStream(file);
|
|
|
|
|
|
|
|
bis = new BufferedInputStream(fis);
|
|
|
|
|
|
|
|
OutputStream os = response.getOutputStream();
|
|
|
|
|
|
|
|
int i = bis.read(buffer);
|
|
|
|
|
|
|
|
while (i != -1) {
|
|
|
|
|
|
|
|
os.write(buffer, 0, i);
|
|
|
|
|
|
|
|
i = bis.read(buffer);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("Download the song successfully!");
|
|
|
|
|
|
|
|
return "下载成功!";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
|
|
|
System.out.println("Download the song failed!");
|
|
|
|
|
|
|
|
return "下载失败!";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
finally {
|
|
|
|
|
|
|
|
if (bis != null) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
bis.close();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fis != null) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
fis.close();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//return "Download the song successfully!";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
return file.getPath()+"文件不存在!";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* excel 导入
|
|
|
|
|
|
|
|
* @param file
|
|
|
|
|
|
|
|
* @throws Exception
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@PostMapping("/import")
|
|
|
|
|
|
|
|
public ResultData<String> importExcel(MultipartFile file) throws Exception {
|
|
|
|
|
|
|
|
InputStream inputStream = file.getInputStream();
|
|
|
|
|
|
|
|
ExcelReader reader = ExcelUtil.getReader(inputStream);
|
|
|
|
|
|
|
|
// 方式1:(推荐) 通过 javabean的方式读取Excel内的对象,但是要求表头必须是英文,跟javabean的属性要对应起来
|
|
|
|
|
|
|
|
// List<User> list = reader.readAll(User.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 方式2:忽略表头的中文,直接读取表的内容
|
|
|
|
|
|
|
|
List<List<Object>> read = reader.read(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* List<User> users = CollUtil.newArrayList();
|
|
|
|
|
|
|
|
for (List<Object> row : list) {
|
|
|
|
|
|
|
|
User user = new User();
|
|
|
|
|
|
|
|
user.setUsername(row.get(0).toString());
|
|
|
|
|
|
|
|
user.setPassword(row.get(1).toString());
|
|
|
|
|
|
|
|
user.setNickname(row.get(2).toString());
|
|
|
|
|
|
|
|
user.setEmail(row.get(3).toString());
|
|
|
|
|
|
|
|
user.setPhone(row.get(4).toString());
|
|
|
|
|
|
|
|
user.setAddress(row.get(5).toString());
|
|
|
|
|
|
|
|
user.setAvatarUrl(row.get(6).toString());
|
|
|
|
|
|
|
|
users.add(user);
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ResultData.success("","",1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/uploadFile")
|
|
|
|
|
|
|
|
public ResultData<String> uploadFile(HttpServletRequest request, HttpServletResponse response,@RequestParam("file") MultipartFile file) throws UnsupportedEncodingException {
|
|
|
|
|
|
|
|
System.out.println("文件上传开始");
|
|
|
|
|
|
|
|
response.setHeader("content-type", "application/octet-stream");
|
|
|
|
|
|
|
|
response.setContentType("application/octet-stream");
|
|
|
|
|
|
|
|
// 下载文件能正常显示中文
|
|
|
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
|
|
|
|
|
|
|
|
//当前项目的目录
|
|
|
|
|
|
|
|
String path = com.nmgs.util.PathUtil.webappsPath + "\\CarNoFile\\";
|
|
|
|
|
|
|
|
//每天创建一个文件目录
|
|
|
|
|
|
|
|
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
|
|
|
|
|
|
|
|
String fileDirName = sdf.format(new Date());
|
|
|
|
|
|
|
|
String folderPath = path + fileDirName;
|
|
|
|
|
|
|
|
System.out.println("文件路径=====>"+folderPath);
|
|
|
|
|
|
|
|
File fileTemp=new File(folderPath);
|
|
|
|
|
|
|
|
if (!fileTemp.exists())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fileTemp.mkdirs();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
String fileName="";
|
|
|
|
|
|
|
|
if (file != null){
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fileName = file.getOriginalFilename();
|
|
|
|
|
|
|
|
System.out.println("上传的文件名称======>"+fileName);
|
|
|
|
|
|
|
|
File fileDocument=new File(folderPath,fileName);
|
|
|
|
|
|
|
|
File fileExists=new File(folderPath+"//"+fileName);
|
|
|
|
|
|
|
|
if(fileExists.exists()){
|
|
|
|
|
|
|
|
fileExists.delete();
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
fileDocument.mkdir();
|
|
|
|
|
|
|
|
file.transferTo(fileDocument);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
System.out.println("文件异常====="+ ex.getMessage());
|
|
|
|
|
|
|
|
return ResultData.fail("文件异常====="+ ex.getMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResultData.success(1,fileName);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|