读取文件并相应一个文件

来源:互联网 发布:charindex函数 sql 编辑:程序博客网 时间:2024/05/29 07:48

思路:

1::创建文件的备份地址

2:创建相应文件的地址

3:利用Springmvc中的

transferTo备份文件;
4:读取文件并执行业务操作
5:用input流读取要返回的文件
6
IOUtils.copy(input,output)

@RequestMapping(value = "/day/settlement", method = RequestMethod.POST)public void daySettlement(        HttpServletResponse response,        @RequestHeader("serviceNo") String serviceNo,                          @RequestParam("requestFile") MultipartFile file) throws Exception {    BufferedReader br=null;    BufferedWriter writer = null;    OutputStream out = null;    InputStream in = null;    try {        String filename = file.getOriginalFilename();        File parent = new File(rootPath + "/day/settlement");        if (!parent.exists()) {            parent.mkdirs();        }        File parentSend = new File(rootPath + "/day/settlement/send");        if (!parentSend.exists()) {            parentSend.mkdirs();        }        File fileBak = new File(parent, filename);        if (!fileBak.exists()) {            fileBak.createNewFile();            file.transferTo(fileBak);        }else{            return;        }      业务代码可以忽略是为了把错误信息写到文件中返回 // br = new BufferedReader(new InputStreamReader(new FileInputStream(fileBak), "UTF-8"));        File sendFile = new File(parentSend.getAbsolutePath(), filename);        if (sendFile.exists()) sendFile.delete();        sendFile.createNewFile();         writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sendFile), "UTF-8"));        String s = null;        String date = filename.substring(filename.lastIndexOf("_") + 1, filename.lastIndexOf("."));        ArrayList<DaySettlementFile> files = new ArrayList<DaySettlementFile>();        while ((s = br.readLine()) != null) {            DaySettlementFile dayS = new DaySettlementFile();            dayS.setCreateDate(new Date());            dayS.setCreateBy("SYSTEM");            dayS.mapped(s, "\\|");            dayS.setSettlementDate(date);            daySettleService.checkDaySettle(dayS);            daySettleService.insert(dayS);            writer.write(s);            writer.write(dayS.getStatus() + "|");            writer.write(dayS.getMessage() + "|");            writer.newLine();        }
//        writer.flush();        response.addHeader("Content-Length", String.valueOf(sendFile.length()));        in = new FileInputStream(sendFile);        out = response.getOutputStream();        IOUtils.copy(in, out);    }finally {        IOUtils.closeQuietly(br);        IOUtils.closeQuietly(writer);        IOUtils.closeQuietly(in);        IOUtils.closeQuietly(out);    }

阅读全文
0 0
原创粉丝点击