package ; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.InterceptorRefs; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.googlecode.genericdao.search.Filter; import com.googlecode.genericdao.search.Search; /** * 上传客户并保持到数据库 * @author * */ @Namespace("/customerexcelupload") @Action("") @InterceptorRefs(value = { @InterceptorRef("fileUploadStack") }) @Results({ @Result(name = "success", location = "/jsp/customervisit/customer/importok.jsp"), @Result(name = "error", location = "/jsp/customervisit/customer/importfailed.jsp") }) public class CustomerExcelUpload extends GeoinfoAction { /** * 序列化 */ private static final long serialVersionUID = 572146812454l; /** * 缓存 */ private static final int BUFFER_SIZE = 16 * 1024; /** * 文件 */ private File upload; /** * 类型 */ private String contentType; /** * 文件名 */ private String fileName; /** * 实际文件名 */ private String storageFileName; /** * 处理结果信息 */ private String exceptionstr = ""; /** geoinfoService业务操作对象 */ @Resource(type = CustomerService.class) private CustomerService customerService; /** geoinfoService业务操作对象 */ @Resource(type = ClassifyService.class) private ClassifyService classifyService; /** * 分类ID */ private String classifyexcelId; /** * 地物分类:客户1 店面2 竞品店3 */ private String geoType; /** 负责人的ID **/ private String responseAccountId; /** 负责人的名字 **/ private String responseName; /** * 上传数据处理工具 */ @Resource(name = "importDataHandler") private ImportDataHandler importDataHandler; /** * 用户登录信息 */ @Resource(name = "accountAPI") private AccountAPI accountAPI; /** * 拷贝文件 * @param src xx * @param dst xx */ public void copy(File src, File dst) { try { InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE]; while (in.read(buffer) > 0) { out.write(buffer); } } finally { if (null != in) { in.close(); } if (null != out) { out.close(); } } } catch (Exception e) { e.printStackTrace(); } } /** * 上传文件并保存到数据库 * @return xx */ public String execute(){ HttpServletResponse response = ServletActionContext.getResponse(); ServletActionContext.getResponse().setHeader("content-type", "text/html"); PrintWriter out = null; try { out = response.getWriter(); } catch (IOException e1) { e1.printStackTrace(); } //重置错误结果集 List<ErrorObject> errorList = new ArrayList<ErrorObject>(); importDataHandler.setErrorList(errorList); if("".equals(classifyexcelId)){ exceptionstr = importDataHandler.CLASSIFYISNULL; writeJumpUrl(out,"no"); return ERROR; } if(upload == null){ exceptionstr = importDataHandler.FILEISNULL; writeJumpUrl(out,"no"); return ERROR; } storageFileName = fileName; if(fileName.endsWith("xls")||fileName.endsWith("csv")||fileName.endsWith("xlsx")){ String s = File.separator; String uploadpath = ServletActionContext.getServletContext().getRealPath("/")+ "jsp"+s+"customervisit"+s+"excelupload"+s; File storageFile = new File(uploadpath+ storageFileName); copy(upload, storageFile); Search search = new Search(); search.addFilter(Filter.equal("classify.classifyId", classifyexcelId)); List<ClassifyHoldProperty> list = classifyService.findPropertyMateBySearch(search, true); int result = count(); try{ List<GeoinfoExcelModel> gxmlist = new ArrayList<GeoinfoExcelModel>(); String currentAccountId = accountAPI.getAccountId(); String currentAccountName = accountAPI.getAccountName(); if(responseAccountId == null || "".equals(responseAccountId)){ responseAccountId = currentAccountId; } if(responseName == null || "".equals(responseName)){ responseName = currentAccountName; } //处理三种不同文件 if(fileName.endsWith("xls")){ gxmlist = importDataHandler.loadXls(uploadpath+ storageFileName , list, classifyexcelId,geoType,responseAccountId,responseName); }else if(fileName.endsWith("xlsx")){ gxmlist = importDataHandler.loadXlsx(uploadpath+ storageFileName , list, classifyexcelId,geoType,responseAccountId,responseName); }else if(fileName.endsWith("csv")){ gxmlist = importDataHandler.superCsvRead(uploadpath+ storageFileName , list, classifyexcelId,geoType,responseAccountId,responseName); } int maxline = gxmlist.size(); if(maxline == 0){ exceptionstr = importDataHandler.DATAISNULL; writeJumpUrl(out,"no"); return ERROR; } if(gxmlist.size()>0){ if(maxline > 1000){ exceptionstr = importDataHandler.OVERMAXLINE; writeJumpUrl(out,"no"); return ERROR; } if((maxline+result) >10000){ exceptionstr = importDataHandler.OVERMAXDATABASE; writeJumpUrl(out,"no"); return ERROR; } if(importDataHandler.getErrorList().size()>0){ exceptionstr = "{"; for(int i=0;i<importDataHandler.getErrorList().size();i++){ ErrorObject eo = importDataHandler.getErrorList().get(i); exceptionstr = exceptionstr + "第" + eo.getLine() + "行第" +eo.getQueue() + "列; "; } exceptionstr += "}"+importDataHandler.ERRORINPUT; writeJumpUrl(out,"no"); return ERROR; } prepaireHtml(out); for(int i=0;i<gxmlist.size();i++){ GeoinfoExcelModel gxm = gxmlist.get(i); Geoinfo geoinfo = importDataHandler.convertToGeoinfo(gxm); customerService.save(geoinfo); out.write("<script>genTo("+(Math.ceil((i) * 100/gxmlist.size()))+")</script>"); out.flush(); Thread.currentThread().sleep(100); } out.write("<script>genTo('100')</script>"); out.flush(); exceptionstr = gxmlist.size()+importDataHandler.HASIMPORT; writeJumpUrl(out,"yes"); } }catch(Exception e){ e.printStackTrace(); exceptionstr = importDataHandler.UNKNOWNERRORINPUT; writeJumpUrl(out,"no"); return ERROR; } return SUCCESS; }else{ exceptionstr = importDataHandler.FILETYPEISNOTGOOD; writeJumpUrl(out,"no"); return ERROR; } } /** * 写入html的进度条 * @param out xx */ private void prepaireHtml(PrintWriter out){ String html = "<div id='wrap' style='position:relative; margin:30px auto; width:500px; height:20px; text-algin:center; border:1px solid #ccc; background-color:#fff;'>"+ "<div style='position:absolute; background-color:#00FF33; width:1px; height:20px; z-index:1; left:0px; top:0px;' id='progres'></div>"+ "<span id='percent' style='position:absolute; z-index:9;'>0%</span>"+"</div>"; html+="<script>function $$(id){return document.getElementById(id);}</script>"; html+="<script>function genTo(p){$$('percent').innerHTML=p+'%';$$('progres').style.width=parseInt($$('wrap').style.width)/100*p+'px';}</script>"; out.write(html); out.flush(); } /** * 写入跳转链接 * @param out xx * @param flag xx */ private void writeJumpUrl(PrintWriter out, String flag){ HttpServletRequest request = ServletActionContext.getRequest(); String contextPath = request.getContextPath(); if("yes".equals(flag)){ out.write("<script>window.parent.location.href='"+contextPath+"/stationlatlon/!importOk.action?exceptionstr="+exceptionstr+"'</script>"); out.flush(); out.close(); }else if("no".equals(flag)){ out.write("<script>"); out.write("var url = '"+contextPath+"/stationlatlon/!importFail.action';"); out.write("var postForm = document.createElement('form');"); out.write("postForm.target='_parent';"); out.write("postForm.method='post';"); out.write("postForm.action = url;"); out.write("var pam = document.createElement('input');"); out.write("pam.setAttribute('name', 'exceptionstr') ;"); out.write("pam.setAttribute('value', '"+exceptionstr+"');"); out.write("postForm.appendChild(pam);"); out.write("document.body.appendChild(postForm);"); out.write("postForm.submit();"); out.write("</script>"); out.flush(); out.close(); } } public String getExceptionstr() { return exceptionstr; } public void setExceptionstr(String exceptionstr) { this.exceptionstr = exceptionstr; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getUploadFileName() { return fileName; } public void setUploadFileName(String fileName) { this.fileName = fileName; } public String getStorageFileName() { return storageFileName; } public void setStorageFileName(String storageFileName) { this.storageFileName = storageFileName; } public String getUploadContentType() { return contentType; } public void setUploadContentType(String contentType) { this.contentType = contentType; } public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } public CustomerService getCustomerService() { return customerService; } public void setCustomerService(CustomerService customerService) { this.customerService = customerService; } public String getClassifyexcelId() { return classifyexcelId; } public void setClassifyexcelId(String classifyexcelId) { this.classifyexcelId = classifyexcelId; } public ClassifyService getClassifyService() { return classifyService; } public void setClassifyService(ClassifyService classifyService) { this.classifyService = classifyService; } public ImportDataHandler getImportDataHandler() { return importDataHandler; } public void setImportDataHandler(ImportDataHandler importDataHandler) { this.importDataHandler = importDataHandler; } public String getGeoType() { return geoType; } public void setGeoType(String geoType) { this.geoType = geoType; } public String getResponseAccountId() { return responseAccountId; } public void setResponseAccountId(String responseAccountId) { this.responseAccountId = responseAccountId; } public String getResponseName() { return responseName; } public void setResponseName(String responseName) { this.responseName = responseName; } public AccountAPI getAccountAPI() { return accountAPI; } public void setAccountAPI(AccountAPI accountAPI) { this.accountAPI = accountAPI; } }
<form action="<%=contextPath %>/customerexcelupload/!execute.action" method="post" enctype="multipart/form-data" target="load"> <input type="hidden" value="" id="classifyexcelId" name="classifyexcelId"/> <input type="hidden" value="1" name="geoType"/> <input id="responseAccountId" name="responseAccountId" type="hidden"/> <input id="responseName" name = "responseName" type="hidden"/> <table> <tr> <td><input class="inpt" type="file" name="upload"></td> <td><input class="inpt" style="width:70px; height:25px;line-height:25px; margin-left:15px; cursor:pointer; " type="submit" value="上 传"></td> </tr> <tr> </tr> </table> </form> <iframe width="550px" height="85px" framespacng="0" frameborder="0" name="load" ></iframe>
特别注意: 这种一次请求,长时间不关闭响应的处理方式需要关闭nginx的缓存, 否则显示结果将以全部的请求结果一次性返回而看不到进度条效果。 proxy_buffering off;
相关推荐
Java生成密钥、保存密钥的实例源码,通过本源码可以了解到Java如何产生单钥加密的密钥(myKey)、产生双钥的密钥对(keyPair)、如何保存公钥的字节数组、保存私钥到文件privateKey.dat、如何用Java对象序列化保存私钥...
Java生成密钥、保存密钥的实例源码,通过本源码可以了解到Java如何产生单钥加密的密钥(myKey)、产生双钥的密钥对(keyPair)、如何保存公钥的字节数组、保存私钥到文件privateKey.dat、如何用Java对象序列化保存私钥...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...