| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.mapper.MacInitMapper; |
| | | import com.whyc.mapper.MacMapper; |
| | | import com.whyc.pojo.Mac; |
| | | import com.whyc.pojo.MacInit; |
| | | import com.whyc.util.ActionUtil; |
| | | import com.whyc.util.MacUtil; |
| | | import com.whyc.util.ThreadLocalUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.swing.plaf.TableHeaderUI; |
| | | import java.io.FileNotFoundException; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | |
| | | public class MacService { |
| | | @Autowired(required = false) |
| | | private MacMapper mapper; |
| | | |
| | | @Autowired(required = false) |
| | | private MacInitMapper initMapper; |
| | | |
| | | //查询mac历史记录 |
| | | public Response searMacHis() { |
| | |
| | | startStr = Integer.toHexString(10001); |
| | | } |
| | | List<String> list = MacUtil.getMac(startStr, macNum); |
| | | //获取初始值 |
| | | QueryWrapper initwrapper = Wrappers.query(); |
| | | initwrapper.last("limit 1"); |
| | | MacInit macInit = initMapper.selectOne(initwrapper); |
| | | //导出csv |
| | | Date date = new Date(); |
| | | String filename = "mac地址_" + ActionUtil.sdf.format(date).replace(" ", "_").replace(":", "_") + ".csv"; |
| | | //String filename = "mac_" + ActionUtil.sdf.format(date).replace(" ", "_").replace(":", "_") + ".csv"; |
| | | String filename = "mac_" + ThreadLocalUtil.format(date,1).replace(" ", "_").replace(":", "_") + ".csv"; |
| | | try { |
| | | // 转码防止乱码 |
| | | resp.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); |
| | |
| | | if (i == list.size() - 1) { |
| | | stopStr = list.get(i); |
| | | } |
| | | byte[] buffer = (MacUtil.macStart + ":" + list.get(i)).getBytes(); |
| | | byte[] buffer = (macInit.getMacStart() + ":" + list.get(i)).getBytes(); |
| | | out.write(buffer, 0, buffer.length); |
| | | out.write("\r\n".getBytes()); |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | } |
| | | //从历史导出CSV |
| | | public void getMacHis(HttpServletRequest req, HttpServletResponse resp, List<String> list) { |
| | | //导出csv |
| | | Date date = new Date(); |
| | | //String filename = "mac_" + ActionUtil.sdf.format(date).replace(" ", "_").replace(":", "_") + ".csv"; |
| | | String filename = "mac_" + ThreadLocalUtil.format(date,1).replace(" ", "_").replace(":", "_") + ".csv"; |
| | | try { |
| | | // 转码防止乱码 |
| | | resp.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); |
| | | resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "utf-8")); |
| | | OutputStream out = resp.getOutputStream(); |
| | | if (list != null) { |
| | | for (int i = 0; i < list.size(); i++) { |
| | | byte[] buffer = list.get(i).getBytes(); |
| | | out.write(buffer, 0, buffer.length); |
| | | out.write("\r\n".getBytes()); |
| | | } |
| | | } |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |