void uploadSomeType(@RequestParam("uploadFile") MultipartFile multipartFile) {
//进行文件读取配置
CsvReadConfig csvReadConfig = new CsvReadConfig();
csvReadConfig.setSkipEmptyRows(true);
csvReadConfig.setContainsHeader(true);
//构建 CsvReader 对象
CsvReader csvReader = CsvUtil.getReader(csvReadConfig);
//读取文件,封装成 Bean
if (null == multipartFile) {
log.info("设备类型文件不能为空!");
return new BaseResponse<>(ApiResponseStatus.COMMON_FAIL);
}
ArrayList<SomeType> deviceTypeList = new ArrayList<SomeType>();
try {
File file = uploadFile(multipartFile);
deviceTypeList = (ArrayList<SomeType>) csvReader.read(new FileReader(file), SomeType.class);
file.delete();
} catch (Exception e) {
log.info("设备类型文件上传失败!!!");
}
//判重后,添加设备类型入库
for(SomeType deviceType:deviceTypeList){
}
}
postman文件上传:
//导出到csv文件
public static <T> void downloadsToCsvFile(HttpServletResponse response, List<T> list, String uniPath, String filenam, String cment, Locale locale) throws IOException {
File file;
try {
//响应类型
response.setContentType("multipart/form-data");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
String fileNameStr = URLEncoder.encode( filename, "UTF-8").replace("+", "%20");
//响应的是 .csv 文件的后缀
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileNameStr + ".csv");
String filePath=staticFilePath+ uniquePath +filename+".csv";
log.info("-----filePath {}", filePath);
file=new File(filePath);
File dir = file.getParentFile();
// 创建文件夹
if (!dir.exists()) {
dir.mkdirs();
}
// 创建文件
if(!file.exists()){
file.createNewFile();
}
// 第一行需要写入bom(三个字节,提示office为utf-8格式),否则Excel打开会多语言乱码
FileOutputStream out = null;
try {
out=new FileOutputStream(file);
out.write(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.flush();
out.close();
}
}
CsvWriter csvWriter = null;
try {
//追加的方式,将数据,写入到 文件里面。
csvWriter = CsvUtil.getWriter(file, Charset.forName("UTF-8"), true);
if (!StringUtils.isEmpty(comment)) {
csvWriter.writeComment(comment);
}
if (CollUtil.isNotEmpty(list)) {
boolean isFirst = true;
Map<String, Object> map;
for (Object bean : list) {
map = BeanUtil.beanToMap(bean);
if (isFirst) {
String titlesStr = InternationalizeUtil.get("MSG.Report.Export.Headers", locale);
String[] titles = Arrays.asList(titlesStr.split(",")).toArray(new String[0]);
csvWriter.writeHeaderLine(titles);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (csvWriter != null) {
try {
csvWriter.flush();
csvWriter.close();
} catch (IORuntimeException e) {
e.printStackTrace();
}
}
}
//下载文件
// try {
// downloadFileNew(response, file, fileNameStr + ".csv");
// } catch (Exception e) {
// throw new RuntimeException(e);
// } finally {
// //将该文件删除
//// file.delete();
// }
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
// 下载文件流
public static boolean downloadFile(HttpServletResponse response, File file, String filename) {
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
OutputStream os = null;
try {
// 设置响应头,控制浏览器下载该文件, 传文件名
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
os = response.getOutputStream();
//MS产本头部需要插入BOM
//如果不写入这几个字节,会导致用Excel打开时,中文显示乱码,windows下实践发现写两次,记事本才能识别到bom
os.write(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
byte[] buffer = new byte[1024];
int i = bufferedInputStream.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bufferedInputStream.read(buffer);
}
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}