hooyantsing's Blog

实现将本地文件上传至服务器,自动地将文件中的数据导入数据库

字数统计: 955阅读时长: 4 min
2019/11/17

实现将本地文件上传至服务器,自动地将文件中的数据导入数据库

2019.11.05

项目案例:双色球(DoubleBall)

DoubleBall.zip

思想:首先通过upload将文件上传至服务器,服务器自动的通过io读取该文件,读到的数据放入一个临时字符串中。再对这个长字符串进行分割,后数组元素对应表列存入数据库。

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<%@page import="task.biz.LotteryBiz"%>
<%@page import="task.bean.Lottery"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.util.Date"%>
<%@page  import="java.text.SimpleDateFormat"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.jspsmart.upload.Files"%>
<%@page  import="com.jspsmart.upload.SmartUpload"%>
<%@ page language="java"  contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%
    //创建文件上传对象
    SmartUpload su = new SmartUpload();
    //初始化,传入页面上下文对象
    su.initialize(pageContext);
    //设置上传配置信息
    //限定文件名后缀
    su.setAllowedFilesList("txt");
    //限定大小
    su.setMaxFileSize(1024 * 1024 * 10);
    //上传准备就绪
    su.upload();
    Files files = su.getFiles();
    List<String> fileList = new  ArrayList<String>();
    String filename = null; //文件名
    String diskPath = null; //目录绝对路径
    String realName = null; //文件绝对路径
    for (int i = 0; i < files.getCount();  i++) {
       //防止上传的文件名相同,将文件名修改为唯一
       //获取系统当前时间
       SimpleDateFormat df = new  SimpleDateFormat("yyyyMMddHHmmssSSS");
       Date date = new Date();
       filename = df.format(date);
       //使用application 将相对路径转换成绝对路径
       diskPath =  application.getRealPath("/upload");
       realName = diskPath + "/" +  filename;
       files.getFile(i).saveAs(realName);
       fileList.add(realName);
    }
    String result = ""; //读取txt文件的数据并保存到该字符串中
    File file = null;
    BufferedReader br = null;
    InputStreamReader reader = null;
    String s = null; //Reader每次读取一行的容器
    List<String[]> listS = new  ArrayList<String[]>(); //每一个元素将是已经被分割好的一期双色球数据
    String[] params = null; //每一个元素将是数据库中对应的一列
    String[] sql = null; //每一个元素将是一期双色球数据
    for (String f : fileList) {
       try {
           //f是上传文件的绝对路径
           file = new File(f);
           reader = new  InputStreamReader(new FileInputStream(file),  "utf-8");
           br = new BufferedReader(reader);
           while ((s = br.readLine()) !=  null) {
               result = result + s;
           }
           //从文件中读取数据并存入数据库中
           //通过"/"分割每一期数据
           sql = result.split("/");
           if (sql.length != 0) {
               for (String number : sql) {
                  //通过":"分割数据库中对应的每一列
                  params =  number.split(":");
                  listS.add(params);
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           //储存完后将result置为空
           result = "";
           br.close();
           reader.close();
       }
    }
    //listS中存储所有文件的所有数据
    String msg = null; //提示信息
    Lottery lottery = null;
    List<Lottery> list = new  ArrayList<Lottery>();
    //遍历集合将数据从数组中取出存入Lottery实体对象中,并存入集合中
    for (String[] string : listS) {
       //如果数组长度不等于12 则数据有误,丢弃
       if (string.length == 12) {
           lottery = new Lottery();
           lottery.setOpendate(string[0]);
           lottery.setNum(string[1]);
           lottery.setRedone(string[2]);
           lottery.setRedtwo(string[3]);
           lottery.setRedthree(string[4]);
           lottery.setRedfour(string[5]);
           lottery.setRedfive(string[6]);
           lottery.setRedsix(string[7]);
           lottery.setBlue(string[8]);
           lottery.setSale(string[9]);
           lottery.setFirstprize(string[10]);
           lottery.setSecondeprize(string[11]);
       }
       list.add(lottery);
    }
    //遍历集合将数据存入数据库
    LotteryBiz biz = null;
    long num = 0;
    if (list.size() != 0) {
       for (int i = 0; i < list.size();  i++) {
           biz = new LotteryBiz();
           //返回的是有多少条数据发生了更改
           num = biz.add(list.get(i));
           if (num < 0) {
               msg = "文件上传失败!";
               pageContext.setAttribute("msg", msg);
               return;
           }
       }
       msg = "文件上传成功!";
       pageContext.setAttribute("msg",  msg);
    }
%>
<b>${msg}</b>

<input type="button" value="返回"
    onclick="window.location.href =  'index.jsp'" />
CATALOG