`
还有也许
  • 浏览: 164292 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

文件上传自动分割信息入库文件读取

    博客分类:
  • java
阅读更多

最近做了一个功能,由于每逢年过节,要求导入客户资料的客户越来越多。现在客户资料超过了800万,查询速度明显下降,甚至无法忍受。既然有些客户只是为了给这些导入的客户发送短信,而不运用其他功能,那就直接给你手机号码的了。

首先要选择给哪个商户导入他提供的手机号码:

 

  页面:

<%@ page contentType="text/html; charset=utf-8"%>
<html>
<head>
<% 
   String departmentSid=String.valueOf(request.getAttribute("departmentSid")); 
   int corpSid=(Integer)request.getAttribute("corpSid");
    
    session.setAttribute("corpSid",corpSid);
    session.setAttribute("departmentSid",departmentSid);
    session.setAttribute("pageContext",pageContext);
    
%>
<script type="text/javascript">
//得到文件路径的js方法,目前没有用到此方法
function getValueIE8(){
    var ip = document.getElementById("filename");
    ip.select();
    alert(document.selection.createRange().text);
    document.getElementById("fileNamePath").value=document.selection.createRange().text;
}
</script>
</2Ftr>

<tr><td><input type="submit" name="btnSubmit"  value=" 上 传 " class="DefaultButton"/></td></tr>
</table>
</form>
%6ȷ手机号码--选择txt文件</caption>
<tr><td><font class="FieldItem">选择txt文件<font class="NotNull">(*必填项)&nbsp;&nbsp;必须上传txt文件,而且每个手机号码占一行,大小不要超过50M</font></font><br/>

<input type="file" name="filename" id="filename" _dataType="string"></td><td></td>
</tr>

<tr><td><input type="submit" name="btnSubmit"  value=" 上 传 " class="DefaultButton"/></td></tr>
</table>
</form>
</body>
</html>
 

 方法:

public ModelAndView selectTxt(HttpServletRequest request
			,HttpServletResponse response) {
    	//String departmentSid =request.getParameter("departmentSid");
    	String departmentSid = StringUtils.varFormat(request.getParameter("departmentSid"));
		Department department=departmentDao.getById(departmentSid);
		
        if(departmentSid == null)
        {
            request.setAttribute("message", MSG_NO_DATA_FOUND);
            return new ModelAndView(failView);
        } else
        {
        	Company company=department.getCompany();
            request.setAttribute("corpSid", company.getCorpSid());
            request.setAttribute("departmentSid", departmentSid);
            return new ModelAndView(viewPage);
        }
	}

 接着是上传文件:

  public ModelAndView uploadTxt(HttpServletRequest request

			,HttpServletResponse response) {
		//PageContext pageContext=null;
		String departmentSid=String.valueOf(request.getSession().getAttribute("departmentSid"));
		
		int corpSid=(Integer)request.getSession().getAttribute("corpSid");
		String account="";
		Company company =companyDao.getById(corpSid);
		Department department =departmentDao.getById(departmentSid);
		if(null==company){
			request.setAttribute("message", MSG_NO_DATA_FOUND);
            return new ModelAndView(failView);
		}else{
			 account=company.getCorpAccount();
		}
		
		//System.out.println("departmentSid="+departmentSid+",corpSid="+corpSid);
		//String departmentSid="";
		//int corpSid=0;
		
		String  fileName = "";
		//String  departmentSid = "";
		//Mysql 连接初始化
    	
		try {
		FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload fileUpload = new ServletFileUpload(factory);
        fileUpload.setSizeMax(50*1024*1024);
        
			List fildItems = fileUpload.parseRequest(request);
			Iterator fildItem = fildItems.iterator();
			int count=0;
		     FileItem fi;
		        while(fildItem.hasNext())
		        {
		        	   
		                fi = (FileItem)fildItem.next();
		                //String name=fi.getFieldName();
			            //普通表单域
			            if(fi.isFormField())
			            {
			            	
			            }
			            //文件表单
			            else if(!fi.isFormField()) 
						{
			            	String fileExcel = (new StringBuilder(String.valueOf(request.getSession().getServletContext().getRealPath("/")))).append("phoneTxt/").append(account+"/").toString();
			            	File  uploadDir = new File(fileExcel);
			            	System.out.println("文件夹名称:"+uploadDir.getName());
			            	long size=uploadDir.length();
			            	//System.out.println("文件大小:"+size);
			                if(!(uploadDir.exists()))
			                {
			                	uploadDir.mkdir();//创建目录!
			                }
			            	String filePath = fi.getName();//获得文件绝对路径
			            	System.out.println("文件名称:"+filePath);
			            	 int a=filePath.lastIndexOf("\\");
			            	 System.out.println("a="+a);
			            	 int b=filePath.lastIndexOf(".");
				             String fn=filePath.substring(b);//获取文件的后缀名
				             if(!fn.equals(".txt")){
				            	 request.setAttribute("message", "上传的文件格式不正确,请上传txt文件!");
				     			return new ModelAndView(this.failView);
				             }
				             
				             System.out.println("filePath="+filePath+",fn="+fn);
				             String f=DateUtils.getDate("yyyyMMddHHmmss");//以日期命名
			            	try {
								fi.write(new File((new StringBuilder(fileExcel)).append(f+fn).toString()));
								fileName=fileExcel+f+fn;//获得文件的相对路径
			            		
								String trueName=filePath.substring(a+1,b);
								System.out.println("取代后缀名的txt:"+trueName);
								boolean istrue=splitFile(10000,fileName,fileExcel,trueName,company,department);
								if(!istrue){
									request.setAttribute("message", "在分割文件或插入数据库记录时出错!");
									return new ModelAndView(this.failView);
								}
							} catch (Exception e) {
								e.printStackTrace();
								return new ModelAndView(this.failView);
							}
						}
	        		count++;
	        
	        	} 
			            	
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			request.setAttribute("message", "文件上传过大,请选择小于50M的文件!");
			return new ModelAndView(this.failView);
		}
		request.setAttribute("message",this.MSG_SUCCESS);
		request.setAttribute("redirectUrl", requestUri+".do?method=list");
		return new ModelAndView(this.successView);
	}
 

在上传的过程中按行分割上传的文件:

   private boolean splitFile(int rows,String sourceFilePath,String targetDirectoryPath,String fileName,Company company,Department department){

    	  File sourceFile = new File(sourceFilePath);
    	  File targetFile = new File(targetDirectoryPath);
    	  boolean isContinue=true;
    	  //获得文件名称
    	  String trueName=fileName;
    	  //获得公司,部门
    	 
    	  int s=0;
    	  if(!sourceFile.exists()||rows<=0||sourceFile.isDirectory()){
    	   System.out.println("源文件不存在或者输入了错误的行数");
    	   return isContinue;
    	  }
    	  if(targetFile.exists()){
    	   if(!targetFile.isDirectory()){
    	    System.out.println("目标文件夹错误,不是一个文件夹");
    	    return isContinue;
    	   }
    	  }else{
    	   targetFile.mkdirs();
    	  }
    	
    	  try{
    	   BufferedReader br = new BufferedReader(new FileReader(sourceFile));
    	   BufferedWriter bw = null;
    	   String str = "";
    	   String tempData = br.readLine();
    	   int i=1;
    	   int count=0;
    	   while(tempData!=null){
    	    str += tempData+"\r\n";
    	    File file=new File(targetFile.getAbsolutePath()+"/"+s+"_"+sourceFile.getName());
    	    if(i%rows==0){
    	          bw = new BufferedWriter(new FileWriter(file));
    	          bw.write(str);
    	          bw.close();
    	          str = "";
    	          s += 1;
    	          count++;
    	    }
    	    i++;
    	    tempData = br.readLine();
    	   }
    	   System.out.println("共循环"+count+",i="+i);
    	   if((i-1)%rows!=0){
    	    File file=new File(targetFile.getAbsolutePath()+"/"+s+"_"+sourceFile.getName());
    	    
    	    bw = new BufferedWriter(new FileWriter(file));
    	    bw.write(str);
    	    bw.close();
    	    br.close();
    	    s += 1;
    	   }
    	   System.out.println("文件分割结束,共分割成了"+s+"个文件");
    	  }catch(Exception e){
    		  e.printStackTrace();
    	  }
    	  boolean isTrue=insertData(trueName,sourceFile.getName(),targetFile.getAbsolutePath(),company,department,s);
    	  if(!isTrue){
    		  isContinue=false;
    	  }
    	  return isContinue;
    }
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics