上传文件时候获取当前目录(上传CSV文件实例)

摘要:上传文件解析使用,在工作中是多用的功能,总结常见问题如:路径问题,乱码问题,日期转换问题,数值计算处理问题。

1. ssm矿建实现方式:(贴代码为以后自己使用,框架配置不多分析):

@RequestMapping(value="uploadData")
    public String uploadData(HttpSession httpSession,@RequestParam MultipartFile file)throws Exception{
        if(!file.isEmpty()){
            String realPath = httpSession.getServletContext().getRealPath("upload");
            String url = realPath+"/"+System.currentTimeMillis()+file.getOriginalFilename();
            System.out.println(url);
            file.transferTo(new File(url));
            //调用解析存库方法:
            readCsvFile(url);
            //excelUtil(url);
        }
        return "success";
    }

//导入csv文件解析存储数据库:
    public void readCsvFile(String path1) throws Exception{
        MultiplePerformanceMonitoringDataTable tables = new MultiplePerformanceMonitoringDataTable();
        SimpleDateFormat fprmat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意分隔符一定要与读取文件日期字符串的分隔符一致。
        DecimalFormat decimalFormat=new DecimalFormat("0.0000000000");
        //String path1="D:\\zkr\\workspace1\\GDDP\\WebRoot\\15个直属台流量\\15个直属台数据.csv";
        File csv=new File(path1);
        BufferedReader br=null;
        br=new BufferedReader(new FileReader(csv));
        String line="";
        String averyLine="";
        while((line=br.readLine())!=null){
            averyLine=line;
            if(averyLine!=null || averyLine!=""){
                String[] strArray=averyLine.split(",");
                //201台:
                if(strArray.length>5 && strArray[0].equals("SJZX-To-ZhiShuTai-NE40/Mp-group4/0/1")){
                    //对象封装,插入数据库
                    tables.setPkId(UUID.randomUUID().toString());
                    tables.setTpname("3146026");
                    //时间处理:
                    //   10/15/2017 08:05:00 UTC+08:00
                    String ymd=strArray[1].substring(0, 10);
                    String hms=strArray[1].substring(11,19);
                    String[] strArrayYMD=ymd.split("/");
                    String strDate=strArrayYMD[2]+"-"+strArrayYMD[0]+"-"+strArrayYMD[1]+" "+hms;
                    Date retrievalTime1 = fprmat.parse(strDate);
                    tables.setRetrievaltime(retrievalTime1);
                    tables.setPmparametername("流入速率");
                    tables.setPmlocation(null);
                    
                    //流入速率转换单位:
                    String strPM=strArray[3];
                    String zimu=strPM.substring(strPM.length()-1, strPM.length());
                    String key="";
                    if("M".equals(zimu) || "m".equals(zimu)){
                        //如果单位是M/s则不用进行单位转换:
                        key=decimalFormat.format(strPM.substring(0, strPM.length()-1)) ;
                        tables.setPmvalue(key);
                    }else {
                        if("K".equals(zimu) || "k".equals(zimu)){
                            //如果单位是K/s则/1024转换为M/s:
                            key=decimalFormat.format((Float.parseFloat(strPM.substring(0, strPM.length()-1)))/1024) ;
                            tables.setPmvalue(key);//注意:如果关于小数的计算,则需要用Float.parseFloat()方法进行转换。
                        }else {
                            //如果没有单位是则/1024/1024转换为M/s:
                            tables.setPmvalue(decimalFormat.format(Float.parseFloat( (strPM.substring(0, strPM.length()-1)))/1024/1024));
                        }
                    }
                    tables.setMeasurementunits("M/s");
                    tables.setRecord2("直属台");
                    
                    tables.setIntervalstatus("PMIS_Valid");
                    tables.setRecord1("201检测台");
                    services.insertObject(tables);
}

注意:如果读出的中文为正确的编码,存入数据库为乱码。 解决办法:修改(我用的是
MySql)数据库的 .ini配置文件让编码一致即可。
(代码不全,供参考)。

 

posted on 2017-10-23 11:30  forever_2h  阅读(1446)  评论(0编辑  收藏  举报

导航