java 用流读取文件内容

 public String postxml(String str) throws JSONException {
        String jkid=null,uploadUrl=null,filePath=null,resout="上传成功",zzqresout=null,uploadid=null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map tomap;
        JSONObject jo = new JSONObject(str);
        uploadid = jo.getString("uploadId");
        jkid = jo.getString("jkid");
        uploadUrl = jo.getString("uploadUrl");
        filePath = jo.getString("filePath");
        if (StringUtils.isBlank(jkid) || StringUtils.isBlank(uploadUrl) || StringUtils.isBlank(filePath) ){
            return "数据不全请检查";
        }
        try {
            tomap = Tomap(filePath);
            if (tomap==null){
                return "文件转map集合出错";
            }
            logger.info("补传类接口标识 jkid:="+jkid +"时间:="+dateFormat.format(new Date())+"=== URL为:http://"+uploadUrl+" 参数为:"+tomap+"======");
            if("19C21".equals(jkid) ){
                zzqresout = HttpUtil.doPost1("http://"+uploadUrl, tomap);
            }else{
                zzqresout = HttpUtil.doPost("http://"+uploadUrl, tomap);
            }
            logger.info("补传类接口标识 jkid:="+jkid +"时间:="+dateFormat.format(new Date())+"=== 自治区返回内容为:"+zzqresout+"======");
            if ("zzqptwlm".equals(zzqresout)){
                return "与自治区网络不通!";
            }
            messageserver.UpDataCode(jkid,zzqresout,tomap,uploadid);
            resout = zzqresout;
        }catch (Exception e){
            logger.error("补传类接口标识 jkid:="+jkid +"时间:="+dateFormat.format(new Date())+" 调用结果失败======",e);
            resout="数据上传自治区异常,请检查!";
        }
        return resout;
    }













 /**
     * 从文件中读取信息,并转换为相应map
     * @return
     */
    public static Map Tomap(String filePath){ //filepath文件路径
	        File file = new File(filePath);
	        if(!file.exists()){
	            return null;
	        }
	        HashMap map = new HashMap<>();
	        BufferedReader br = null;
	        try {
	            br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
	            StringBuilder sb = new StringBuilder();
	            String line;
	            while((line = br.readLine()) != null){
	                sb.append(line);
	            }
	            map = JSON.parseObject(sb.toString(), HashMap.class);
	            br.close();
	        } catch (UnsupportedEncodingException | FileNotFoundException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return map;
    }

你可能感兴趣的:(java 用流读取文件内容)