SpringMVC use ResponseEntity for file downloads

Paper mainly ResponseEntity <byte []> achieved under the File
class implements response header file data (stored in bytes), state packaged together to process browser to download a file browser implementation.
ResponseEntity of parameters: ResponseEntity (T body, MultiValueMap < String, String> headers, HttpStatus statusCode)
where ResponseEntity <T> extends HttpEntity <T >, it is clear that inheritance, the HttpEntity is an entity class, the new ResponseEntity <byte [] > (b, headers, statusCode) ; initialization of the phrase, would T body, MultiValueMap <String, String > headers parameter to two parent class, this class stored status code, the source code can look class HttpEntity to:

the HttpEntity public (T body, MultiValueMap <String, String> headers) {
this.body = body;
HttpHeaders tempHeaders new new HttpHeaders = ();
IF (= null headers!) {
tempHeaders.putAll (headers);
}
// the header head into object can only be read, not written over.
= HttpHeaders.readOnlyHttpHeaders this.headers (tempHeaders);
}

HttpHeaders Class Description: The HTTP request and response header, mapping the first name to a list of strings of string values.
Why use HttpHeaders class here, because MultiValueMap interface implementation class is: HttpHeaders, LinkedMultiValueMap and static class MultiValueMapAdapter
word does not say directly on the code:

// download exercises

@GetMapping("/download")
public ResponseEntity<byte[]> download(@RequestParam(name = "id") Long id, HttpServletRequest request) {
TemplateDto byId = getBaseService().findById(id);
HttpHeaders headers = new HttpHeaders();
String fileName = null;
try {
fileName = FileExtraUtils.handleFileName(request, "download.pdf");
} catch (UnsupportedEncodingException e) {
//ignore
log.warn("文件名处理出错", e);
}
headers.setContentDispositionFormData("attachment", fileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); //二进制流(或者字节数组)的形式返回

return new ResponseEntity<>(byId.getTemplaeContent(), headers, HttpStatus.OK);
}

// upload the file and save
@PostMapping("/upload")
public void upload(@Param("file") MultipartFile file, HttpServletResponse response) throws IOException {
File tempFile;
try {
tempFile = File.createTempFile(file.getOriginalFilename(), ".pdf");
file.transferTo(tempFile);
HashMap data = new HashMap();
data.put("fileName", tempFile.getCanonicalPath());
data.put("displayName", file.getOriginalFilename());
ResultDto dto = ResultDtoFactory.toAckData(data);
response.setContentType("text/html; charset=utf-8");
response.getWriter().write(JSON.toJSONString(dto));
} catch (IOException e) {
log.error ( "File save failed", e);
ResultDto dto = ResultDtoFactory.toNack ( "failed files");
the response.setContentType ( "text / HTML; charset = UTF-. 8");
response.getWriter () Write (JSON.toJSONString (DTO));.
}
}

@ override
public ResultDto Create (TemplateDto templateDto) {
String fileName = templateDto.getFileName ();
the try (= the FileInputStream new new FIS the FileInputStream (new new File (fileName))) {
templateDto.setTemplaeContent (IOUtils.toByteArray (FIS));
} the catch (IOException E) {
log.error ( "error reading the contents of the template", E);
return ResultDtoFactory.toNack ( "error reading contents of the template, please re-upload");
}
. getBaseService () Save (templateDto);
return ResultDtoFactory.toAck ();
}


Guess you like

Origin www.cnblogs.com/renjiaqi/p/11457229.html
Recommended