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