excel文件上传 java_java上传excel文件及解析

1 package controller;2

3 import java.io.File;4 import java.io.IOException;5 import java.io.PrintWriter;6 import java.util.Calendar;7 import java.util.Iterator;8 import java.util.List;9 import javax.servlet.ServletException;10 import javax.servlet.annotation.WebServlet;11 import javax.servlet.http.HttpServlet;12 import javax.servlet.http.HttpServletRequest;13 import javax.servlet.http.HttpServletResponse;14 import org.apache.commons.fileupload.FileItem;15 import org.apache.commons.fileupload.FileItemFactory;16 import org.apache.commons.fileupload.disk.DiskFileItemFactory;17 import org.apache.commons.fileupload.servlet.ServletFileUpload;18

19 /**20 * Servlet implementation class UploadExcel21 */

22 @WebServlet("/uploadExcel.do")23 public class UploadExcelController extends HttpServlet {24 private static final long serialVersionUID =1L;25

26 protected voiddoGet(HttpServletRequest request, HttpServletResponse response)27 throws ServletException, IOException {28 this.doPost(request, response);29 }30

31 protected voiddoPost(HttpServletRequest request, HttpServletResponse response)32 throws ServletException, IOException {33 //指定保存路径

34 String fileSavePath = "/upload";35 String rootPath = this.getServletContext().getRealPath("");36 fileSavePath = rootPath +fileSavePath;37 //获取前台传参

38 String param = request.getParameter("test");39 System.out.println("获取前台参数:" +param);40

41 //上传操作

42 FileItemFactory factory = newDiskFileItemFactory();43 ServletFileUpload upload = newServletFileUpload(factory);44 upload.setHeaderEncoding("UTF-8");45 String saveFileName = "";46 String oldFileName = "";47 try{48 List items =upload.parseRequest(request);49 if (null !=items) {50 Iterator itr =items.iterator();51 while(itr.hasNext()) {52 FileItem item =(FileItem) itr.next();53 if (!item.isFormField()) {//文件格式

54 //以当前精确到秒的日期为上传的文件的文件名

55 saveFileName = this.getServerSysDateAndTimeAsCode();56 oldFileName =item.getName();57 String fileType = oldFileName.substring(oldFileName.lastIndexOf("."));58 saveFileName +=fileType;59 //空文件对象路径+文件名

60 File savedFile = newFile(fileSavePath, saveFileName);61 //写入

62 item.write(savedFile);63 }64 }65 }66

67 StringBuffer sb = newStringBuffer();68 //key和value两边都必须带""

69 sb.append("{").append("\"oldFileName\"").append(":").append("\"").append(oldFileName).append("\"")70 .append(",").append("\"saveFileName\"").append(":").append("\"").append(saveFileName).append("\"").append("}");71 //json字符串:文件名称及文件路径

72 String returnMsg =sb.toString();73 System.out.println(returnMsg);74 //返回信息

75 response.setContentType("text/html; charset=UTF-8");76 PrintWriter out =response.getWriter();77

78 //返回页面

79 out.print(returnMsg);80

81 } catch(Exception e) {82 e.printStackTrace();83 }84

85 }86

87 /**88 * 获得当前日期【long型】作为文件名称89 * @return90 */

91 public String getServerSysDateAndTimeAsCode() {92 String result = null;93 long currentTimeInMilis =Calendar.getInstance().getTimeInMillis();94 result =String.valueOf(currentTimeInMilis);95 returnresult;96 }97 }98 解析excel99

100 package controller;101

102 import java.io.File;103 import java.io.IOException;104 import java.io.PrintWriter;105 import javax.servlet.ServletException;106 import javax.servlet.annotation.WebServlet;107 import javax.servlet.http.HttpServlet;108 import javax.servlet.http.HttpServletRequest;109 import javax.servlet.http.HttpServletResponse;110 import bo.BoExcelImpl;111 import bo.IBoExcel;112

113 /**114 * Servlet implementation class AnalyzeExcelController115 */

116 @WebServlet("/readExcel.do")117 public class AnalyzeExcelController extends HttpServlet {118 private static final long serialVersionUID =1L;119

120 protected voiddoGet(HttpServletRequest request, HttpServletResponse response)121 throws ServletException, IOException {122 this.doPost(request, response);123 }124

125 protected voiddoPost(HttpServletRequest request, HttpServletResponse response)126 throws ServletException, IOException {127 String FILENAME = request.getParameter("FILENAME");128 String msg = "";129 String expMsg = "";130 IBoExcel boExcel = newBoExcelImpl();131 try{132 String path = this.getServletContext().getRealPath("/upload");133 //上传excel的绝对路径

134 path += File.separator +FILENAME;135 //解析excel数据

136 boolean isSuccess =boExcel.readExcel(path);137

138 if(isSuccess) {139 msg = "数据导入成功!";140 } else{141 msg = "数据导入失败!";142 }143

144 } catch(Exception e) {145 expMsg =e.getMessage();146 } finally{147 StringBuffer sb = newStringBuffer();148 sb.append("{").append("\"msg\"").append(":").append("\"").append(msg).append("\"").append(",")149 .append("\"expMsg\"").append(":").append("\"").append(expMsg).append("\"").append("}");150 //json字符串:文件名称及文件路径

151 String returnMsg =sb.toString();152 System.out.println(returnMsg);153

154 //返回信息

155 response.setContentType("text/html; charset=UTF-8");156 PrintWriter out =response.getWriter();157

158 //返回页面

159 out.print(returnMsg);160 }161

162 }163

164 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值