lxw
2023-07-10 87505470b077b97b4a6be1db751ecb07ba9156c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.whyc.service;
 
import com.whyc.util.MacUtil;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
 
@Service
public class MacService {
 
    //获取mac
    public void getMac(HttpServletRequest req, HttpServletResponse resp, int macNum) {
        List<String> list = MacUtil.getMac(MacUtil.macStart, macNum);
        //导出csv
        String filename = "mac地址.csv";
        try {
            // 转码防止乱码
            resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "utf-8"));
            OutputStream out = resp.getOutputStream();
            if (list != null) {
                for (String mac : list) {
                    byte[] buffer = mac.getBytes();
                    out.write(buffer, 0, buffer.length);
                    out.write("\r\n".getBytes());
                }
            }
            out.close();
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}