Here are the examples of the java api org.springframework.http.ContentDisposition taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
10 Examples
19
Source : HeaderAssertions.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
/**
* Expect a "Content-Disposition" header with the given value.
*/
public WebTestClient.ResponseSpec contentDisposition(ContentDisposition contentDisposition) {
return replacedertHeader("Content-Disposition", contentDisposition, getHeaders().getContentDisposition());
}
19
Source : DataSetDownloadController.java
with Apache License 2.0
from kiegroup
with Apache License 2.0
from kiegroup
@GetMapping(value = "/dataset/export")
@ResponseBody
public ResponseEnreplacedy<Resource> exportDataSet() throws IOException {
String dataSet = demoService.exportDataSet();
byte[] dataSetBytes = dataSet.getBytes(StandardCharsets.UTF_8);
try (InputStream is = new ByteArrayInputStream(dataSetBytes)) {
HttpHeaders headers = new HttpHeaders();
ContentDisposition attachment = ContentDisposition.builder("attachment").filename("vrp_data_set.yaml").build();
headers.setContentDisposition(attachment);
return ResponseEnreplacedy.ok().headers(headers).contentLength(dataSetBytes.length).contentType(new MediaType("text", "x-yaml", StandardCharsets.UTF_8)).body(new InputStreamResource(is));
}
}
18
Source : DefaultMultipartMessageReader.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
/**
* Convert the given data buffer into a Part. All data up until the header separator (\r\n\r\n)
* is preplaceded to {@link #toHeaders(DataBuffer)}, the remaining data is considered to be the
* body.
*/
private static Part toPart(DataBuffer dataBuffer) {
int readPosition = dataBuffer.readPosition();
if (dataBuffer.readableByteCount() >= 2) {
if (dataBuffer.getByte(readPosition) == CR && dataBuffer.getByte(readPosition + 1) == LF) {
dataBuffer.readPosition(readPosition + 2);
}
}
if (logger.isTraceEnabled()) {
logger.trace("Part data: " + toString(dataBuffer));
}
int endIdx = HEADER_MATCHER.match(dataBuffer);
HttpHeaders headers;
DataBuffer body;
if (endIdx > 0) {
readPosition = dataBuffer.readPosition();
int headersLength = endIdx + 1 - (readPosition + HEADER_BODY_SEPARATOR.length);
DataBuffer headersBuffer = dataBuffer.retainedSlice(readPosition, headersLength);
int bodyLength = dataBuffer.writePosition() - (1 + endIdx);
body = dataBuffer.retainedSlice(endIdx + 1, bodyLength);
headers = toHeaders(headersBuffer);
} else {
headers = new HttpHeaders();
body = DataBufferUtils.retain(dataBuffer);
}
DataBufferUtils.release(dataBuffer);
ContentDisposition cd = headers.getContentDisposition();
MediaType contentType = headers.getContentType();
if (StringUtils.hasLength(cd.getFilename())) {
return new DefaultFilePart(headers, body);
} else if (StringUtils.hasLength(cd.getName()) && (contentType == null || MediaType.TEXT_PLAIN.isCompatibleWith(contentType))) {
return new DefaultFormPart(headers, body);
} else {
return new DefaultPart(headers, body);
}
}
17
Source : InvestbookReportController.java
with GNU Affero General Public License v3.0
from spacious-team
with GNU Affero General Public License v3.0
from spacious-team
private void sendSuccessHeader(HttpServletResponse response, String fileName) {
ContentDisposition contentDisposition = ContentDisposition.builder("attachment").filename(fileName, StandardCharsets.UTF_8).build();
response.setHeader("Content-disposition", contentDisposition.toString());
response.setContentType("application/vnd.openxmlformats-officedoreplacedent.spreadsheetml.sheet");
}
15
Source : StandardMultipartHttpServletRequest.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
private void parseRequest(HttpServletRequest request) {
try {
Collection<Part> parts = request.getParts();
this.multipartParameterNames = new LinkedHashSet<>(parts.size());
MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<>(parts.size());
for (Part part : parts) {
String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION);
ContentDisposition disposition = ContentDisposition.parse(headerValue);
String filename = disposition.getFilename();
if (filename != null) {
if (filename.startsWith("=?") && filename.endsWith("?=")) {
filename = MimeDelegate.decode(filename);
}
files.add(part.getName(), new StandardMultipartFile(part, filename));
} else {
this.multipartParameterNames.add(part.getName());
}
}
setMultipartFiles(files);
} catch (Throwable ex) {
handleParseFailure(ex);
}
}
15
Source : FileInfoService.java
with MIT License
from lzpeng723
with MIT License
from lzpeng723
/**
* 下载文件
*
* @param fileName 待下载的文件名
* @param inputStream 待下载的流
* @param response 响应
* @return
*/
public void downloadFile(String fileName, InputStream inputStream, HttpServletResponse response) throws IOException {
try (OutputStream outputStream = response.getOutputStream()) {
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
// ContentDisposition contentDisposition = ContentDisposition.builder("inline").filename(fileName).build(); //在线预览
// 下载
ContentDisposition contentDisposition = ContentDisposition.builder("attachment").filename(fileName).build();
response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
IoUtil.copy(inputStream, outputStream);
outputStream.flush();
} catch (IOException e) {
response.setContentType("text/plain;charset=utf-8");
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
response.getWriter().println("下载文件失败,文件: " + fileName);
}
}
15
Source : FileInfoService.java
with MIT License
from lzpeng723
with MIT License
from lzpeng723
/**
* 下载日志文件
*
* @return
*/
public void downloadLogFile(HttpServletResponse response) throws IOException {
File file = Paths.get(logFilePath).toFile();
try (InputStream inputStream = new FileInputStream(file);
OutputStream outputStream = response.getOutputStream()) {
response.setContentType("application/x-download");
// ContentDisposition contentDisposition = ContentDisposition.builder("inline").filename(fileInfo.getOriginalFileName()).build(); //在线预览
// 下载
ContentDisposition contentDisposition = ContentDisposition.builder("attachment").filename(file.getName()).build();
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
IoUtil.copy(inputStream, outputStream);
outputStream.flush();
}
}
13
Source : StandardPortletMultipartResolver.java
with Apache License 2.0
from liferay
with Apache License 2.0
from liferay
/**
* Parse the given portlet actionRequest, resolving its multipart elements.
*
* @param clientDataRequest the request to parse
*
* @return the parsing result
*
* @throws MultipartException if multipart resolution failed.
*/
protected MultipartParsingResult parseRequest(ClientDataRequest clientDataRequest) throws MultipartException {
MultiValueMap<String, MultipartFile> multipartFiles = new LinkedMultiValueMap<>();
Map<String, String[]> multipartParameters = new HashMap<>();
Map<String, String> multipartParameterContentTypes = new HashMap<>();
Collection<Part> parts = null;
try {
parts = clientDataRequest.getParts();
for (Part part : parts) {
String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION);
ContentDisposition disposition = ContentDisposition.parse(headerValue);
String filename = disposition.getFilename();
if (filename != null) {
if (filename.startsWith("=?") && filename.endsWith("?=")) {
filename = filterFilename(filename);
}
multipartFiles.add(part.getName(), new StandardPortletMultipartFile(part, filename));
}
}
} catch (Exception e) {
throw new MultipartException(e.getMessage(), e);
}
return new MultipartParsingResult(multipartFiles, multipartParameters, multipartParameterContentTypes);
}
12
Source : FileInfoService.java
with MIT License
from lzpeng723
with MIT License
from lzpeng723
/**
* 下载文件
*
* @param id
* @return
*/
public void downloadFile(String id, HttpServletResponse response) throws IOException {
Optional<FileInfo> optional = fileInfoRepository.findById(id);
if (optional.isPresent()) {
FileInfo fileInfo = optional.get();
String fileName = String.join(".", fileInfo.getId(), fileInfo.getExtension());
Path path = Paths.get(fileUploadPath, fileName);
try (InputStream inputStream = new FileInputStream(path.toFile());
OutputStream outputStream = response.getOutputStream()) {
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
// ContentDisposition contentDisposition = ContentDisposition.builder("inline").filename(fileInfo.getOriginalFileName()).build(); //在线预览
// 下载
ContentDisposition contentDisposition = ContentDisposition.builder("attachment").filename(fileInfo.getOriginalFileName()).build();
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
IoUtil.copy(inputStream, outputStream);
outputStream.flush();
}
} else {
response.setContentType("text/plain;charset=utf-8");
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
response.getWriter().println("获取文件失败,文件id: " + id);
}
}
12
Source : HouseMemberDocumentController.java
with Apache License 2.0
from jmprathab
with Apache License 2.0
from jmprathab
@Override
public ResponseEnreplacedy<byte[]> getHouseMemberDoreplacedent(@PathVariable String memberId) {
log.trace("Received request to get house member doreplacedents");
Optional<HouseMemberDoreplacedent> houseMemberDoreplacedentOptional = houseMemberDoreplacedentService.findHouseMemberDoreplacedent(memberId);
return houseMemberDoreplacedentOptional.map(doreplacedent -> {
HttpHeaders headers = new HttpHeaders();
byte[] content = doreplacedent.getDoreplacedentContent();
headers.setCacheControl(CacheControl.noCache().getHeaderValue());
headers.setContentType(MediaType.IMAGE_JPEG);
ContentDisposition contentDisposition = ContentDisposition.builder("inline").filename(doreplacedent.getDoreplacedentFilename()).build();
headers.setContentDisposition(contentDisposition);
return new ResponseEnreplacedy<>(content, headers, HttpStatus.OK);
}).orElseGet(() -> ResponseEnreplacedy.status(HttpStatus.NOT_FOUND).build());
}