admin管理员组文章数量:1660065
1,先写一个公共类
public static String courseFile;
public static String uploadImagePath = "/Upload/vbsqyw/image/";
public static String uploadFilePath ="/Upload/vbsqyw/file/";
public static String templatePath="/Template/vbsqyw/";
/**
* 导入excle文件,导入后先进行文件上传,再解析文件内容,并返回
*
* @param file
* @return
*/
public static HashMap<String, List> importFile(MultipartFile file) {
HashMap<String, List> result = new HashMap<>();
List headers = new ArrayList(); //表头
List bodys = new ArrayList(); //表格行
HashMap fileInfo = uploadFile(file); //先进行文件上传,再解析文件内容
String fileName = fileInfo.get("fileName").toString();
if (!fileName.equals("")) {
Workbook workbook = null;
try {
workbook = getWorkbook(fileName);
} catch (IOException e) {
e.printStackTrace();
}
if (workbook != null || !workbook.equals("")) {
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i); //获取excle中的每一个sheet,再解析每个sheet中的内容
if (sheet.getLastRowNum() != 0) {
for (int r = 0; r <= sheet.getLastRowNum(); r++) {
Row row = sheet.getRow(r); //获取sheet的每一行
List body = new ArrayList();
for (int n = 0; n < row.getLastCellNum(); n++) {
HashMap bodyVal = new HashMap();
HashMap headerVal = new HashMap();
if (r == 0) {
//如果行号为0,则是表头
headerVal.put("value", row.getCell(n).getStringCellValue());
headers.add(headerVal);
} else {
Cell cell = row.getCell(n);
String dateString = checkDate(cell);
if (dateString.equals("")) {
cell.setCellType(CellType.STRING);
} else {
cell.setCellValue(dateString);
}
bodyVal.put("value", cell.getStringCellValue());
body.add(bodyVal);
}
}
if (r > 0) {
//行号为0时不添加内容到bodys中
bodys.add(body);
}
}
}
}
result.put("body", bodys); //返回的内容
result.put("header", headers); //返回的表头
}
}
return result;
}
/**
* 上传文件,包括图片,表格等文件,不解析文件内容
*
* @param file 选择的文件
* @return
*/
public static HashMap<String, String> uploadFile(MultipartFile file) {
HashMap<String, String> result = new HashMap<>();
String saveFilePath = "";
if (!file.isEmpty
版权声明:本文标题:后端文件上传,返回地址,前端删除文件或图片时,后台同时删除 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1729844242a1214945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论