文件上传
@RequestMapping(path = "/add_drug", method = {RequestMethod.POST})
public String add_drug(String str, MultipartFile file) {
System.out.println("add_drug:" + str);
if (!file.isEmpty()) {
String upload = FileUploadUtil.fileUpload(file);
System.out.println(upload);
return "添加成功!";
}
return "文件为空!";
}
文件上传工具类
package com.dxy.demo_05_08.util;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
public class FileUploadUtil {
private static String FILEPATH = "E:\other\file\";
public static String fileUpload(MultipartFile file){
if (file.isEmpty()) {
System.out.println("文件为空!");
}
String fileName = file.getOriginalFilename(); // 文件名
String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后缀名
fileName = UUID.randomUUID() + suffixName; // 新文件名
File dest = new File(FILEPATH + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
return dest.getPath();
}
}
前端
导出excel
@RequestMapping(value = "export_consumeMonth", method = RequestMethod.GET)
public void export_consumeMonth(HttpServletResponse response, String month) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(month+"消费信息表");
// List all = consumeService.selectByMonth(month);
List lists = new ArrayList<>();
lists.add("你好");
//String fileName = month+ "consume你好.xls";//设置要导出的文件的名字
String fileName = URLEncoder.encode(month+ "consume你好.xlsx","UTF-8");
//新增数据行,并且设置单元格数据
int rowNum = 1;
//String[] headers = { "学号", "姓名", "身份类型", "登录密码"};
//headers表示excel表中第一行的表头
String[] headers = { "部门","用户名","早餐","午餐","晚餐","总计","时间",month+"日总和"};
XSSFRow row = sheet.createRow(0);
//在excel表中添加表头
for(int i=0;i
XSSFCell cell = row.createCell(i);
XSSFRichTextString text = new XSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
Double a=0.0;
//在表中存放查询到的数据放入对应的列
for (String str : lists) {
XSSFRow row1 = sheet.createRow(rowNum);
row1.createCell(0).setCellValue(str+0);
row1.createCell(1).setCellValue(str+1);
row1.createCell(2).setCellValue(str+1);
row1.createCell(3).setCellValue(str+1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
row1.createCell(4).setCellValue(sdf.format(new Date()));
rowNum++;
}
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.flushBuffer();
workbook.write(response.getOutputStream());
}