java实现从浏览器读取Csv文件解析成 List<Map>

本文介绍了一种从MultipartFile中读取CSV文件并将其转换为List&lt;Map&lt;String, Object&gt;&gt;的方法,同时展示了如何将读取的数据转化为JSON格式,并通过HTTP POST请求发送到指定的服务端进行进一步处理。

浏览器读取csv文件(也可改成本地地址读取)



import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


public class ReadCSV {
	
	 public static BufferedReader multipartFileToBufferedReader(MultipartFile multipartFile, String charsetName) {
	        if (multipartFile == null || multipartFile.isEmpty()) {
	            return null;
	        }
	        try {
	            InputStream inputStream = multipartFile.getInputStream();
	            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charsetName);
	            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
	            return bufferedReader;
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return null;
	    }

	    public static BufferedReader multipartFileToBufferedReader(MultipartFile multipartFile) {
	        return multipartFileToBufferedReader(multipartFile, "UTF-8");
	    }
	    
	public static List<Map<String, Object>> readCSV(MultipartFile fileS){

		List<Map<String, Object>> queryForList = new ArrayList<Map<String, Object>>();
		List<String> list =new ArrayList<String>();
	 
		
		try
	    {
//			使用本地地址的方式,使用本地地址的方式调用,file为String类型
//	    	DataInputStream in= new DataInputStream(new FileInputStream(new File(file)));
			
//	    	BufferedReader reader= new BufferedReader(new InputStreamReader(in,"GBK"));
			//	使用浏览器打开的方式

	    	BufferedReader reader=  multipartFileToBufferedReader(fileS,"GBK");
//	    	System.err.println(reader.toString());
	    	 
	        boolean sign = false;       //用来跳过第一行的名称
	        while(reader.ready())
	        {
	            String line = reader.readLine();
	            StringTokenizer st = new StringTokenizer(line, ",");

	            if (st.hasMoreTokens() && sign)
	            {
	            	String a=st.nextToken().trim().replace("\"","");
	            	list.add(a);
	            	Map<String, Object> map =new HashMap<String, Object>();
	            	  map.put("deviceID", a);
	            	  map.put("longitude",(int) Math.round(Double.valueOf(st.nextToken().trim().replace("\"",""))*10000000d));
	            	  map.put("latitude", (int) Math.round(Double.valueOf(st.nextToken().trim().replace("\"",""))*10000000d));
	            	  //字符串形式的时间数据转换成时间戳
	            	  // int timeStamp2 = Integer.valueOf(date2TimeStamp(st.nextToken().trim().replace("\"",""), "yyyy-MM-dd HH:mm:ss")) ; 
	            	  map.put("gpstime", Integer.valueOf(st.nextToken().trim().replace("\"","")));
	            	  queryForList.add(map);
//	      			jsonArray.add(new JSONObject(map));
	            }
	            else
	            {
	                sign = true;
	            }
	        }
	        reader.close();

	    }
	    catch (FileNotFoundException e)
	    {

	        e.printStackTrace();
	    }
	    catch (IOException e)
	    {

	        e.printStackTrace();
	    }
		
		
		 


return queryForList;
//		return queryForList;
	}
	/** 
     * 日期格式字符串转换成时间戳 
     * @param date 字符串日期 
     * @param format 如:yyyy-MM-dd HH:mm:ss 
     * @return 
     */  
    public static String date2TimeStamp(String date_str,String format){  
        try {  
            SimpleDateFormat sdf = new SimpleDateFormat(format);  
            return String.valueOf(sdf.parse(date_str).getTime()/1000);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return "";  
    }  
}

controller调用:

@RequestMapping("/Scoring/getRotues")
		public  Object upload(@RequestParam  MultipartFile file, HttpServletRequest request) throws Exception {
			List<Map<String, Object>> list= ReadCSV.readCSV( file);
			JSONObject json;
			JSONArray jsonArray = new JSONArray();
			for (Map<String, Object> map : list) {
				json = new JSONObject(map);
				jsonArray.add(json);
			}
			
//			System.err.println(jsonArray);
//			String url="http://mineservice.minedata.cn/service/lbs/service/search/multi-trajectory?appKey=3b3bad1077b441f8868d2de8d464fca9";
			String  url="http://ms.minedata.cn/service/lbs/service/search/multi-trajectory?appKey=79a7d9ef4ab5494faa5d80ae62e58f16";
		String resonponse=	HttpUtils.sendJsonToPost(url,jsonArray.toString());
		 return	Scoring(resonponse);
//		 return jsonArray.toString();
//			 return resonponse;
		}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值