博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
文件下载
阅读量:4562 次
发布时间:2019-06-08

本文共 8274 字,大约阅读时间需要 27 分钟。

package com.huawei.iop.service.worldCUP.userCluster;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.commons.lang3.StringUtils;import org.json.JSONException;import org.json.JSONObject;import org.springframework.beans.factory.annotation.Autowired;import com.huawei.iop.bean.TaskInfo;import com.huawei.iop.common.AbstractService;import com.huawei.iop.common.IopConst;import com.huawei.iop.common.IopException;import com.huawei.iop.manager.worldCUP.userCluster.UserDefinedGroupManager;import com.huawei.iop.manager.worldCUP.userCluster.WorldCUPManager;import com.huawei.iop.util.ConfigPropertiesUtil;import com.huawei.iop.util.IOHelperUtil;public class ExpUserClusterFileService extends AbstractService{    static char separator = ',';    private WorldCUPManager worldCUPManager;    @Autowired    private UserDefinedGroupManager userDefinedGroupManager;    public void setWorldCUPManager(WorldCUPManager worldCUPManager)    {        this.worldCUPManager = worldCUPManager;    }    public void setUserDefinedGroupManager(UserDefinedGroupManager userDefinedGroupManager)    {        this.userDefinedGroupManager = userDefinedGroupManager;    }    @Override    public String excute(Map
context) throws IopException { logger.info("enter ExpUserClusterFile ..."); logger.info("params:" + context); // 接收前台数据 String jsonObject = (String) context.get(IopConst.REQUEST_JSON); String execute = IopConst.IOP_RUN_SUCCESS; String savePath = request.getSession().getServletContext().getRealPath("tmp"); String urlPath = request.getServletContext().getContextPath() + "/tmp"; /* * BufferedReader reader = null; BufferedOutputStream bo = null; */ // 封装Json JSONObject object = null; // String pathString = null; FileInputStream fis = null; FileOutputStream fos = null; try { object = new JSONObject(jsonObject); File saveFile = new File(savePath); List
flagList = new ArrayList
(IopConst.DEFAULTNUM); if (!saveFile.exists()) { // 新建 // saveFile.mkdirs(); flagList.add(saveFile.mkdirs()); boolean b = saveFile.mkdirs(); if (b) { logger.info("saveFile.mkdirs():" + b); } } String taskID = object.getString("taskID"); TaskInfo taskInfo = worldCUPManager.selectTaskById(taskID); if (null == taskInfo) { return execute = IopConst.IOP_BEAN_FAILSE; } String fileName =taskInfo.getFileName(); String downloadFileName = "users_group_"+taskID+".csv"; //users_group_201806051010.csv // 獲取當前時間的年月 /* * Date nowDate = new Date(); SimpleDateFormat df = new * SimpleDateFormat("yyyyMM"); String dateTime = df.format(nowDate); */ // String resultPathFromConf // =ConfigPropertiesUtil.readValue("baseClusterUserPath"); // String resultPathFromConf = // ConfigPropertiesUtil.readValue("baseClusterUserPath") + // dateTime;// download/resultgroups String resultPathFromConf = taskInfo.getDownloadPath(); File file = new File(resultPathFromConf, fileName); logger.info("file:" + file); File destFile = new File(savePath, downloadFileName); logger.info("destFile:" + destFile); /* * if (StringUtils.isEmpty(resultPathFromConf)) { resultPathFromConf * = "/home/iop/download/taskresults/" + dateTime; } */ try { fis = new FileInputStream(file); fos = new FileOutputStream(destFile); int nRead = 0; byte[] buffer = new byte[1024]; // 用戶ID String userId = ConfigPropertiesUtil.readValue("column1"); if (StringUtils.isEmpty(userId)) { userId = "UERID"; } // 手机号码 String telephone = ConfigPropertiesUtil.readValue("column2"); if (StringUtils.isEmpty(telephone)) { telephone = "TELEPHONE"; } // 流量值(M) String flow = ConfigPropertiesUtil.readValue("column3"); if (StringUtils.isEmpty(flow)) { flow = "FLOW(MB)"; } fos.write((userId + "," + telephone + "," + flow + "\r\n").getBytes()); while ((nRead = fis.read(buffer, 0, 1024)) > 0) { // bis为网络输入流 fos.write(buffer, 0, nRead); } } catch (Exception e) { logger.error("fis read failed ..."); logger.error("fos read failed ..."); } finally { IOHelperUtil.close(fis, fos); } /* * String separator = System.getProperty("line.separator"); reader = * new BufferedReader(new FileReader(file));// 换成你的文件名 String line = * null; String[] heads = { "用户ID", "手机号", "流量(M)" }; int i = 0; * String[][] rows = new String[3][]; String[] split = null; while * ((line = reader.readLine()) != null) { split = * line.trim().split(","); rows[i++] = split; } byte[] bom = { * (byte) 0xFF, (byte) 0xFE }; bo = new BufferedOutputStream(new * FileOutputStream(destFile)); logger.info("write begin ..."); * bo.write(bom); bo.write(join(heads, "\t").getBytes("utf-16le")); * bo.write(separator.getBytes("utf-16le")); for (String[] row : * rows) { bo.write(join(row, "\t").getBytes("utf-16le")); * bo.write(separator.getBytes("utf-16le")); // int value = * Integer.parseInt(last);//如果是数值,可以转化为 } bo.flush(); */ logger.info("write end ..."); /* * if (file.isFile() || file.exists() || file.canRead()) { */ appendJSON("url", urlPath + "/" + downloadFileName); // execute = IopConst.IOP_RUN_SUCCESS; // } /* * else { logger.info("if-else : else"); } */ logger.info("out ExpActivityService ..."); } /* * execute = IopConst.IOP_RUN_FAILSE; * logger.error("QueryHitContent execute:", e); */ catch (JSONException e) { execute = IopConst.IOP_RUN_FAILSE; logger.error("JSONException: IopConst.IOP_RUN_FAILSE" + e); } /* * catch (UnsupportedEncodingException e) { execute = * IopConst.IOP_RUN_FAILSE; * logger.error("UnsupportedEncodingException: IopConst.IOP_RUN_FAILSE" * + e); } catch (IOException e) { execute = IopConst.IOP_RUN_FAILSE; * logger.error("IOException: IopConst.IOP_RUN_FAILSE" + e); } */ catch (Exception e) { execute = IopConst.IOP_RUN_FAILSE; logger.error("Exception: IopConst.IOP_RUN_FAILSE" + e); } finally { /* * if (reader != null) { try { reader.close(); } catch (IOException * e) { e.printStackTrace(); } } if (bo != null) { try { bo.close(); * } catch (IOException e) { e.printStackTrace(); } } */ logger.info("Executed once ExportDefinedGroupsService "); // 错误日志 } contextMap.put(IopConst.STATUES, execute); return execute; } /* * private String join(String[] strArr, String delim) { StringBuilder sb = * new StringBuilder(); for (String s : strArr) { sb.append(s); * sb.append(delim); } String ret; if (strArr.length > 1) { ret = * sb.substring(0, sb.length() - 1); } else { ret = sb.toString(); } return * ret; } */}

 

转载于:https://www.cnblogs.com/DoubleW127/p/9290041.html

你可能感兴趣的文章
103 Binary Tree Zigzag Level Order Traversal 二叉树的锯齿形层次遍历
查看>>
springboot项目中使用maven resources
查看>>
ubuntu12.04 卸载和安装mysql
查看>>
转:phpStudy for Linux (lnmp+lamp一键安装包)
查看>>
记录项目从无到有
查看>>
博客园文章编辑器【客户端应用程序】V2.0.0发布,命名为51cnblogs
查看>>
C# WPF获取任务栏时间区域的Rectangle
查看>>
DIV+CSS:如何编写代码才能更有效率
查看>>
利用Idea查看类的继承关系图
查看>>
跨进程访问共享内存的权限问题
查看>>
AD管理中心
查看>>
地图定位
查看>>
笑话收集
查看>>
c++相关网站
查看>>
java8-2 多态的概述
查看>>
有符号的整数翻转
查看>>
【转】js中cookie的使用详细分析
查看>>
linux shell学习笔记
查看>>
打印杨辉三角
查看>>
Linux入门配置之一
查看>>