java response header_Java Response.header方法代码示例

import com.squareup.okhttp.Response; //导入方法依赖的package包/类

/**

* Prepare file for download

*

* @param response An instance of the Response object

* @throws IOException If fail to prepare file for download

* @return Prepared file for the download

*/

public File prepareDownloadFile(Response response) throws IOException {

String filename = null;

String contentDisposition = response.header("Content-Disposition");

if (contentDisposition != null && !"".equals(contentDisposition)) {

// Get filename from the Content-Disposition header.

Pattern pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");

Matcher matcher = pattern.matcher(contentDisposition);

if (matcher.find()) {

filename = sanitizeFilename(matcher.group(1));

}

}

String prefix = null;

String suffix = null;

if (filename == null) {

prefix = "download-";

suffix = "";

} else {

int pos = filename.lastIndexOf(".");

if (pos == -1) {

prefix = filename + "-";

} else {

prefix = filename.substring(0, pos) + "-";

suffix = filename.substring(pos);

}

// File.createTempFile requires the prefix to be at least three characters long

if (prefix.length() < 3)

prefix = "download-";

}

if (tempFolderPath == null)

return File.createTempFile(prefix, suffix);

else

return File.createTempFile(prefix, suffix, new File(tempFolderPath));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值