@Override protected void doPostDoer(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub // StringBuffer html = new StringBuffer(); StringBuffer html = new StringBuffer("<div>"); String fileStr = getFile(req, resp); List<Fabric> items = new ArrayList<Fabric>(); boolean result = getList(fileStr, items); if (result) { try { Message msg = FabricIO.saveOrUpdateList(items); html.append(msg.getMsgType() + ": " + msg.getMsgHtml()); } catch (Exception e) { Message msg = new Message("E", "Upload is failed!" + e.getMessage()); html.append(msg.getMsgType() + ": " + msg.getMsgHtml()); e.printStackTrace(); } } else { Message msg = new Message("E", "Upload is failed! format of row " + items.size() + "is wrong!"); html.append(msg.getMsgType() + ": " + msg.getMsgHtml()); } html.append("</div>"); CustomLog.info(html.toString()); print(resp, html.toString()); }
private boolean getList(String fileStr, List<Fabric> items) { CustomLog.info(fileStr); boolean result = true; String[] linesStr = fileStr.split("\r\n"); outer: for (int i = 1; i < linesStr.length; i++) { String lineStr = linesStr[i]; CustomLog.info(lineStr); if (Tool.isNotEmpty(lineStr)) { CustomLog.info(lineStr); lineStr = lineStr + ",==mock=="; String[] column = lineStr.split(","); if (column.length != 13) { result = false; break outer; } Fabric fabric = new Fabric(); fabric.setHangerNo(column[0]); fabric.setCstructnWarp(column[1]); fabric.setCstructnWeft(column[2]); fabric.setYarnWarp(column[3]); fabric.setYarnWeft(column[4]); fabric.setContent(column[5]); fabric.setStatus(column[6]); fabric.setWeaving(column[7]); fabric.setFinishing(column[8]); fabric.setWidth(column[9]); fabric.setArticle(column[10]); fabric.setOriginalPrice(column[11]); fabric.setFinalPrice(column[12]); fabric.setInputDate(new Date()); CustomLog.info(fabric.toString()); items.add(fabric); } } return result; }
private String getFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 如果是文件上传类型 StringBuffer fileStr = new StringBuffer(); // PrintWriter out = resp.getWriter(); if (ServletFileUpload.isMultipartContent(req)) { // 得到文件上传工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(5000 * 1024); factory.setRepository(new File("c:/temp")); // 处理文件上传核心类 ServletFileUpload fileUpload = new ServletFileUpload(factory); fileUpload.setFileSizeMax(50000 * 1024); // 设置文件上传类的编码格式 fileUpload.setHeaderEncoding("UTF-8"); // 集合数据 : FileItem对象 注意: 每一个表单域 对应一个 FileItem对象(封装) try { List<FileItem> fileItemList = fileUpload.parseRequest(req); for (FileItem item : fileItemList) { // 如果这个文本域是文件类型的 if (!item.isFormField()) { CustomLog.info(item.getFieldName()); InputStream inputStream = item.getInputStream(); BufferedInputStream bis = new BufferedInputStream(inputStream); int tempbyte; while ((tempbyte = bis.read()) != -1) { char c = (char) tempbyte; System.out.write(tempbyte); fileStr.append(c); } } else { // CustomLog.info(item.getFieldName()); // CustomLog.info(item.getString()); } } } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return fileStr.toString(); }