SpringMVC上传文件

1.引入依赖

<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->

<!-- 文件上传  -->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.2</version>

</dependency>

2.配置文件

    <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->  
  <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
   <!-- 指定所上传文件的总大小不能超过200KB。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->  
        <property name="maxUploadSize" value="200000"/>  

    </bean> 

3.代码实现

/**
* 读取导入文件数据并入库

* @return
* @throws Exception
*/
@RequestMapping("upload")
public String upload(MultipartFile file) throws Exception {

//multipartfile 转 file
CommonsMultipartFile cf= (CommonsMultipartFile)file; 
        DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 
        File f = fi.getStoreLocation();
        
        String fileFileName = file.getOriginalFilename();
List<SysLanIpMap> ipMapList = new ArrayList<SysLanIpMap>();
String endString = fileFileName.substring(fileFileName.lastIndexOf(".") + 1);
if ("xls".equals(endString)) {
ipMapList = this.getIpMapByXls(f);
} else if ("xlsx".equals(endString)) {
ipMapList = this.getIpMapByXlsx(f);
}
if (ipMapList == null || ipMapList.size() == 0) {
return message;
}
List<Long> startList = new ArrayList<Long>();
for (int i = 0; i < ipMapList.size(); i++) {
long ip = ipMapList.get(i).getIpStartNumber();
if (startList.contains(ip)) {
message = "导入文件中存在相同的IP " + IpSwitchUtil.getLongIpToString(ip);
return message;
}
startList.add(ip);
}
for (SysLanIpMap sysLanIpMap : ipMapList) {
sysLanIpMapService.add(sysLanIpMap);
}
return "success";

}

/**
* 在xls中获取ip映射数据

* @param file
* @return
* @throws IOException
*/
@SuppressWarnings("resource")
private List<SysLanIpMap> getIpMapByXls(File file) {
List<SysLanIpMap> ipMapLists = null;
try {
ipMapLists = new ArrayList<SysLanIpMap>();
HSSFWorkbook rwb = new HSSFWorkbook(new FileInputStream(file));
// 获取第一张Sheet表
HSSFSheet sheet = rwb.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
HSSFRow row = sheet.getRow(i);
if (row == null) {
continue;
}
// 起始IP
String startIp = getCellValue(row, 0);
// 结束IP
String endIp = getCellValue(row, 1);

if (!IpSwitchUtil.ipCheck(startIp)) {
message = "起始IP地址" + startIp + ":格式错误";
return null;
} else if (!IpSwitchUtil.ipCheck(endIp)) {
message = "结束IP地址" + endIp + ":格式错误";
return null;
}
Long ipStartNumber = IpSwitchUtil.getStringIpToLong(startIp);
Long ipEndNumber = IpSwitchUtil.getStringIpToLong(endIp);
if (ipStartNumber > ipEndNumber) {
message = "起始IP地址大于结束IP地址";
return null;
}
//姓名
String name = getCellValue(row, 2);
//城市
String city = getCellValue(row, 3);
//单位
String area = getCellValue(row, 4);
//部门
String department = getCellValue(row, 5);

SysLanIpMap sysLanIpMap = new SysLanIpMap();
sysLanIpMap.setIpStartNumber(ipStartNumber);
sysLanIpMap.setIpEndNumber(ipEndNumber);
sysLanIpMap.setTerminalName(name);
sysLanIpMap.setCity(city);
sysLanIpMap.setArea(area);
sysLanIpMap.setDepartment(department);
ipMapLists.add(sysLanIpMap);
}
} catch (FileNotFoundException e) {
message = "文件找不到";
return null;
} catch (Exception e) {
e.printStackTrace();
message = "载入文件出错";
return null;
}
return ipMapLists;
}
/**
* 在xlsx中获取ip映射数据

* @param file
* @return
* @throws IOException
*/
@SuppressWarnings("resource")
private List<SysLanIpMap> getIpMapByXlsx(File file) {
List<SysLanIpMap> ipMapLists = null;
try {
ipMapLists = new ArrayList<SysLanIpMap>();
XSSFWorkbook rwb = new XSSFWorkbook(new FileInputStream(file));
// 获取第一张Sheet表
XSSFSheet sheet = rwb.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
XSSFRow row = sheet.getRow(i);
if (row == null) {
continue;
}
try {
// 起始IP
String startIp = getCellValue(row, 0);
// 结束IP
String endIp = getCellValue(row, 1);
if (!IpSwitchUtil.ipCheck(startIp)) {
message = "起始IP地址" + startIp + ":格式错误";
return null;
} else if (!IpSwitchUtil.ipCheck(endIp)) {
message = "结束IP地址" + endIp + ":格式错误";
return null;
}
Long ipStartNumber = IpSwitchUtil.getStringIpToLong(startIp);
Long ipEndNumber = IpSwitchUtil.getStringIpToLong(endIp);
if (ipStartNumber > ipEndNumber) {
message = "起始IP地址大于结束IP地址";
return null;
}
//姓名
String name = getCellValue(row, 2);
//城市
String city = getCellValue(row, 3);
//单位
String area = getCellValue(row, 4);
//部门

String department = getCellValue(row, 5);

SysLanIpMap sysLanIpMap = new SysLanIpMap();
sysLanIpMap.setIpStartNumber(ipStartNumber);
sysLanIpMap.setIpEndNumber(ipEndNumber);
sysLanIpMap.setTerminalName(name);
sysLanIpMap.setCity(city);
sysLanIpMap.setArea(area);
sysLanIpMap.setDepartment(department);
ipMapLists.add(sysLanIpMap);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
message = "文件找不到";
return null;
} catch (Exception e) {
e.printStackTrace();
message = "载入文件出错";
return null;
}
return ipMapLists;
}

@SuppressWarnings("deprecation")
public String getCellValue(Row row, int index) {
String value = null;
Cell cell = row.getCell(index);
if (cell == null) {
return "";
}
if (cell.getCellType() == 0) {
value = String.valueOf(cell.getNumericCellValue());
} else {
value = cell.getStringCellValue().trim();
}
return value;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值