读取文件并相应一个文件

思路:

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);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值