使用httpUrlConnection進行文件的讀取和下載

使用httpUrlConnection進行文件的讀取和下載

一、實現(xiàn)的功能介紹

  • 使用httpUrlConnection進行文件的讀取;
  • word模板數(shù)據(jù)的替換,并使用poi生成新的word文件
  • 新文件上傳到ftp服務(wù)器中,返回httpUrl地址,可根據(jù)此地址下載該word文件

二、pom.xml依賴

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>
        </dependency>
        <!-- word -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
        </dependency>

三、代碼實現(xiàn)

    /**
     * 后臺管理中下載用戶協(xié)議,拿到ftp地址
     * @param id
     * @return
     */
    @GetMapping("/sysprotocol/sysprotocoluser/download")
    public ResponseObj download(Integer id) {
        SysProtocolUser sysProtocolUser = null;
        try {
            try {
                sysProtocolUser = sysProtocolUserService.findOne(id);
                if (sysProtocolUser.getFtpUrl() != null) {
                    return ResponseObj.createSuccessResponse(sysProtocolUser.getFtpUrl());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            //如果是用戶注冊協(xié)議,且第一次下載,獲取協(xié)議模板,并進行模板填充,生成用戶協(xié)議,word上傳到ftp服務(wù)器中
            if (sysProtocolUser.getProtocolId() == 1) {
                SysProtocol sysProtocol = sysProtocolService.selectById(sysProtocolUser.getProtocolId());
                String protocolFtpUrl = sysProtocol.getFtpUrl();
                URL url = new URL(protocolFtpUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");//設(shè)置請求方式
                conn.setConnectTimeout(5000);//設(shè)置連接超時
                conn.setDoInput(true);//是否打開輸入流
                conn.setDoOutput(true);//是否打開輸出流
                conn.connect();
                int code = conn.getResponseCode();
                //連接成功,進行文件讀寫操作
                if (code == 200) {
                    //讀取文件并寫入到outpath中
                    String outpath = "";
                    try {
                        InputStream is = conn.getInputStream();
                        outpath = this.getClass().getResource("").getPath() + DateUtil.dateToDateFullString(DateUtil.getDate()) + ".doc";
                        FileOutputStream fos = new FileOutputStream(outpath);
                        byte[] buffer = new byte[1024 * 8];
                        int len = 0;
                        while ((len = is.read(buffer)) != -1) {
                            try {
                                fos.write(buffer, 0, len);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        fos.close();
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    //協(xié)議模板替換
                    String newpath = "";
                    try {
                        newpath = this.getClass().getResource("").getPath() + DateUtil.dateToDateFullString(DateUtil.getDate()) + ".doc";
                        FileInputStream fis = new FileInputStream(outpath);
                        HWPFDocument doc = new HWPFDocument(fis);
                        Range range = doc.getRange();
                        range.replaceText("{0}", sysProtocolUser.getUserPhone());
                        range.replaceText("{1}", sysProtocolUser.getProtocolUserid());
                        range.replaceText("{2}", DateUtil.dateToDateString(sysProtocolUser.getCreateTime()));
                        FileOutputStream fos = new FileOutputStream(newpath);
                        doc.write(fos);
                        fos.close();
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    File file = new File(newpath);
                    InputStream fis = new FileInputStream(file);
                    String ftpUrl = ftpUtil.uploadFile(fis, doc.doDateFilePath(), doc.getDefaultFileName());
                    if (null != ftpUrl) {
                        sysProtocolUser.setFtpUrl(ftpUrl);
                        sysProtocolUserService.updateOne(sysProtocolUser);
                        new File(newpath).delete();
                        new File(outpath).delete();
                        return ResponseObj.createSuccessResponse(ftpUrl);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseObj.createErrResponse(ErrerMsg.ERRER100);
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容